DataDog / dd-trace-js

JavaScript APM Tracer
https://docs.datadoghq.com/tracing/
Other
647 stars 306 forks source link

[Mocha] TypeError: this.tracer._exporter.getItrConfiguration is not a function #2709

Closed JohnDaly closed 1 year ago

JohnDaly commented 1 year ago

Expected behaviour dd-trace should allow a Mocha test suite to run when using the standard AgentExporter

Actual behaviour When trying to run a Mocha test suite, I get the following error:

TypeError: this.tracer._exporter.getItrConfiguration is not a function
    at /node_modules/dd-trace/packages/dd-trace/src/plugins/ci_plugin.js:19:29
    at Subscription._handler (/node_modules/dd-trace/packages/dd-trace/src/plugins/plugin.js:14:9)
    at Channel.publish (node:diagnostics_channel:56:9)
    at /node_modules/dd-trace/packages/datadog-instrumentations/src/mocha.js:351:26
    at exports.AsyncResource.runInAsyncScope (node:async_hooks:203:9)
    at Mocha.run (/node_modules/dd-trace/packages/datadog-instrumentations/src/mocha.js:350:27)

Steps to reproduce

As far as I can tell, here is what seems to be happening:

The Exporter for the tracer is being set to AgentExporter when config.experimental.exporter is not provided: https://github.com/DataDog/dd-trace-js/blob/a96389bc5de239da74769f6fb8cfdfbcefb8e386/packages/dd-trace/src/opentracing/tracer.js#L23

https://github.com/DataDog/dd-trace-js/blob/a96389bc5de239da74769f6fb8cfdfbcefb8e386/packages/dd-trace/src/exporter.js#L25

ITR Configuration Channel is set up here: https://github.com/DataDog/dd-trace-js/blob/a96389bc5de239da74769f6fb8cfdfbcefb8e386/packages/datadog-instrumentations/src/mocha.js#L20

Event is published to that channel here, when Mocha starts: https://github.com/DataDog/dd-trace-js/blob/a96389bc5de239da74769f6fb8cfdfbcefb8e386/packages/datadog-instrumentations/src/mocha.js#L358

Published event is handled is called here: https://github.com/DataDog/dd-trace-js/blob/a96389bc5de239da74769f6fb8cfdfbcefb8e386/packages/dd-trace/src/plugins/ci_plugin.js#L19

Handler fails because the _exporter (AgentExporter) on the tracer was never set up with a getItrConfiguration function.

Environment

JohnDaly commented 1 year ago

After some more investigation, the problem seems to occur when Mocha is loaded after the tracer is initialized.

Here's a minimal repro:

// Initialize dd-trace
const { tracer } = require('dd-trace');
tracer.init();

// Simulate 'mocha' being loaded in another module
require('mocha');

// Tests
const assert = require('assert');

describe('Sample', function () {
  it('should pass', function () {
    assert.equal(1 + 1, 2);
  });
});
juan-fernandez commented 1 year ago

hi @JohnDaly ! Thanks for the detailed description and the reproducible case.

As a note, keep in mind that the way to initialise dd-trace to use CI Visibility (where the mocha plugin reports) is through dd-trace/ci/init: https://docs.datadoghq.com/continuous_integration/tests/javascript/?tab=onpremisesciproviderdatadogagent#instrument-your-tests. The ci prefix in the init makes sure that the correct exporter is used.

Whatever the case, this error should not happen with any exporter so I'll get to fixing this.

juan-fernandez commented 1 year ago

https://github.com/DataDog/dd-trace-js/pull/2711 should do the trick 😄