I am trying to integrate datadog log monitoring with strapi, logging works but the injection doesnt work

System Information
  • Strapi Version: Strapi V3 Beta 9
  • Operating System: Mac OS
  • Database: Postgres
  • Node Version: 14.17.5
  • NPM Version:
  • Yarn Version:

I am trying to integrate datadog log monitoring with strapi, logging works but the injection doesnt work.

Here is the doc from datadog. https://docs.datadoghq.com/tracing/connect_logs_and_traces/nodejs/

I think I’m not initiating the tracer in proper place, where is the entry point file in this version?

Thank you

1 Like

Event I am trying to get this running. Any help from strapi devs would be appreciated.

I am trying to do the same - very disappointed by the lack of logging documentation provided by Strapi.

I mean for one, that isn’t our docs and that’s from the v3-beta like 3 years ago lol…

v4 Docs: https://docs.strapi.io
v3 Docs: https://docs-v3.strapi.io

While I can’t speak for v3 nor the actual configuration of the dd-trace package. For the v4 you should be able to do something like this:

// path: `./src/index.js`

module.exports = {
  /**
   * An asynchronous register function that runs before
   * your application is initialized.
   *
   * This gives you an opportunity to extend code.
   */
  register(/*{ strapi }*/) {
    const tracer = require('dd-trace').init({
      debug: true,
      runtimeMetrics: true,
      logInjection: true
    });
  },

  /**
   * An asynchronous bootstrap function that runs before
   * your application gets started.
   *
   * This gives you an opportunity to set up your data model,
   * run jobs, or perform some special logic.
   */
  bootstrap(/*{ strapi }*/) {},
};

At least this is dumping a bunch of debug messages about it being unable to connect to 127.0.0.1:8126 so I assume there is some config you need to do to point it at DataDog

Has anyone else been able to get dd-trace to work for APM metrics in Strapi 4?

In Strapi 3 I had it working with a simple require(“dd-trace”).init(); at the top of the bootstrap.js file, but the code above in src/index.js in register() in Strapi 4 doesn’t seem to be working :cry:

Have you required calling it within the register or bootstrap functions?

Here’s my src/index.js code in Strapi 4.

‘use strict’;

module.exports = {
/**

  • An asynchronous register function that runs before
  • your application is initialized.
  • This gives you an opportunity to extend code.
    /
    register(/
    { strapi }*/) {
    const tracer = require(‘dd-trace’).init({
    debug: true,
    runtimeMetrics: true,
    logInjection: true
    });

},

/**

  • An asynchronous bootstrap function that runs before
  • your application gets started.
  • This gives you an opportunity to set up your data model,
  • run jobs, or perform some special logic.
    /
    bootstrap(/
    { strapi }*/) {},
    };

And here’s my code in Strapi 3 (where it’s working):

“use strict”;

if (
process.env.NODE_ENV === “production” ||
process.env.NODE_ENV === “staging”
) {
require(“dd-trace”).init();
}

/**

  • An asynchronous bootstrap function that runs before
  • your application gets started.
  • This gives you an opportunity to set up your data model,
  • run jobs, or perform some special logic.
  • See more details here: Configurations | Strapi Documentation
    */

module.exports = () => {};

Any ideas?

Make a server.js file in your root dir. and put this in it.

const tracer = require('dd-trace').init({
  appsec: true
  // any options you need
})
const strapi = require('@strapi/strapi');
const app = strapi({ distDir: './dist' });
app.start();

Start strapi with:

node server.js

Don’t forget to install dd-trace.
(Edit:) I suggest that you install @strapi-logger and setup Winston (a guide is on the Strapi docs)

Here’s the working stats in Datadog:

I was facing the same problem here and I’ve found a way to make it work using strapi v4.

No need to insert the init() function inside src/index.js → register(), we can place it as a new file following the Datadog docs.

I wasn’t able to check the tracer information on the Datadog APM tab because I didn’t have a Datalog Agent running.

image

When we use the dd-trace library, it assumes by default that we’re running this agent on localhost:8125 (We can modify it inside the init() function).
All I had to do was follow the steps to install this agent listed on the Datadog guide(Datadog: Log In) and after setting up this agent I was able to check the APM metrics.