diff --git a/packages/datadog-plugin-express/test/integration-test/client.spec.js b/packages/datadog-plugin-express/test/integration-test/client.spec.js index c13a4249892..68df57c255c 100644 --- a/packages/datadog-plugin-express/test/integration-test/client.spec.js +++ b/packages/datadog-plugin-express/test/integration-test/client.spec.js @@ -36,6 +36,7 @@ describe('esm', () => { }) it('is instrumented', async () => { + process.env.DD_TRACE_MIDDLEWARE_ENABLED = true proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port) const numberOfSpans = semver.intersects(version, '<5.0.0') ? 4 : 3 @@ -49,5 +50,20 @@ describe('esm', () => { assert.propertyVal(payload[0][1], 'name', 'express.middleware') }) }).timeout(50000) + + it('disables middleware spans when config.middleware is set to false through environment variable', async () => { + process.env.DD_TRACE_MIDDLEWARE_ENABLED = false + proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port) + const numberOfSpans = 1 + + return curlAndAssertMessage(agent, proc, ({ headers, payload }) => { + assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`) + assert.isArray(payload) + assert.strictEqual(payload.length, 1) + assert.isArray(payload[0]) + assert.strictEqual(payload[0].length, numberOfSpans) + assert.propertyVal(payload[0][0], 'name', 'express.request') + }) + }).timeout(50000) }) }) diff --git a/packages/datadog-plugin-web/src/index.js b/packages/datadog-plugin-web/src/index.js index 688d5dc2781..21996c952d6 100644 --- a/packages/datadog-plugin-web/src/index.js +++ b/packages/datadog-plugin-web/src/index.js @@ -9,7 +9,7 @@ class WebPlugin extends Plugin { } configure (config) { - return super.configure(web.normalizeConfig(config)) + return super.configure(web.normalizeConfig(config, this._tracerConfig)) } setFramework (req, name, config) { diff --git a/packages/dd-trace/src/config.js b/packages/dd-trace/src/config.js index 8dd63cccdf6..08ae47bb63a 100644 --- a/packages/dd-trace/src/config.js +++ b/packages/dd-trace/src/config.js @@ -520,6 +520,7 @@ class Config { this._setValue(defaults, 'lookup', undefined) this._setValue(defaults, 'inferredProxyServicesEnabled', false) this._setValue(defaults, 'memcachedCommandEnabled', false) + this._setValue(defaults, 'middleware', true) this._setValue(defaults, 'openAiLogsEnabled', false) this._setValue(defaults, 'openaiSpanCharLimit', 128) this._setValue(defaults, 'peerServiceMapping', {}) @@ -670,6 +671,7 @@ class Config { DD_TRACE_HEADER_TAGS, DD_TRACE_LEGACY_BAGGAGE_ENABLED, DD_TRACE_MEMCACHED_COMMAND_ENABLED, + DD_TRACE_MIDDLEWARE_ENABLED, DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP, DD_TRACE_PARTIAL_FLUSH_MIN_SPANS, DD_TRACE_PEER_SERVICE_MAPPING, @@ -801,6 +803,7 @@ class Config { this._setBoolean(env, 'logInjection', DD_LOGS_INJECTION) // Requires an accompanying DD_APM_OBFUSCATION_MEMCACHED_KEEP_COMMAND=true in the agent this._setBoolean(env, 'memcachedCommandEnabled', DD_TRACE_MEMCACHED_COMMAND_ENABLED) + this._setBoolean(env, 'middleware', DD_TRACE_MIDDLEWARE_ENABLED) this._setBoolean(env, 'openAiLogsEnabled', DD_OPENAI_LOGS_ENABLED) this._setValue(env, 'openaiSpanCharLimit', maybeInt(DD_OPENAI_SPAN_CHAR_LIMIT)) this._envUnprocessed.openaiSpanCharLimit = DD_OPENAI_SPAN_CHAR_LIMIT @@ -982,6 +985,7 @@ class Config { this._setString(opts, 'llmobs.mlApp', options.llmobs?.mlApp) this._setBoolean(opts, 'logInjection', options.logInjection) this._setString(opts, 'lookup', options.lookup) + this._setBoolean(opts, 'middleware', options.middleware) this._setBoolean(opts, 'openAiLogsEnabled', options.openAiLogsEnabled) this._setValue(opts, 'peerServiceMapping', options.peerServiceMapping) this._setBoolean(opts, 'plugins', options.plugins) diff --git a/packages/dd-trace/src/plugins/util/web.js b/packages/dd-trace/src/plugins/util/web.js index 2d92c74ea91..7c5f48459a6 100644 --- a/packages/dd-trace/src/plugins/util/web.js +++ b/packages/dd-trace/src/plugins/util/web.js @@ -40,12 +40,12 @@ const web = { TYPE: WEB, // Ensure the configuration has the correct structure and defaults. - normalizeConfig (config) { + normalizeConfig (config, tracerConfig) { const headers = getHeadersToRecord(config) const validateStatus = getStatusValidator(config) const hooks = getHooks(config) const filter = urlFilter.getFilter(config) - const middleware = getMiddlewareSetting(config) + const middleware = getMiddlewareSetting(config, tracerConfig) const queryStringObfuscation = getQsObfuscator(config) return { @@ -570,11 +570,16 @@ function getHooks (config) { return { request } } -function getMiddlewareSetting (config) { - if (config && typeof config.middleware === 'boolean') { - return config.middleware - } else if (config && config.hasOwnProperty('middleware')) { - log.error('Expected `middleware` to be a boolean.') +function getMiddlewareSetting (config, tracerConfig) { + if (config) { + if (typeof config.middleware === 'boolean') { + return config.middleware + } else if (config.hasOwnProperty('middleware')) { + log.error('Expected `middleware` to be a boolean.') + } + } + if (tracerConfig && tracerConfig.middleware === false) { + return false } return true