Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

env var to disable all middleware spans #5044

Merged
merged 15 commits into from
Jan 30, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,45 @@ describe('esm', () => {
await agent.stop()
})

it('is instrumented', async () => {
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port)
const numberOfSpans = semver.intersects(version, '<5.0.0') ? 4 : 3

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')
assert.propertyVal(payload[0][1], 'name', 'express.middleware')
describe('with DD_TRACE_MIDDLEWARE_TRACING_ENABLED unset', () => {
it('is instrumented', async () => {
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port)
const numberOfSpans = semver.intersects(version, '<5.0.0') ? 4 : 3

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')
assert.propertyVal(payload[0][1], 'name', 'express.middleware')
})
}).timeout(50000)
})

describe('with DD_TRACE_MIDDLEWARE_TRACING_ENABLED=true', () => {
before(() => {
process.env.DD_TRACE_MIDDLEWARE_TRACING_ENABLED = false
})

after(() => {
delete process.env.DD_TRACE_MIDDLEWARE_TRACING_ENABLED
})
}).timeout(50000)

it('disables middleware spans when config.middlewareTracingEnabled is false via env var', async () => {
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)
})
})
})
4 changes: 4 additions & 0 deletions packages/dd-trace/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ class Config {
this._setValue(defaults, 'lookup', undefined)
this._setValue(defaults, 'inferredProxyServicesEnabled', false)
this._setValue(defaults, 'memcachedCommandEnabled', false)
this._setValue(defaults, 'middlewareTracingEnabled', true)
this._setValue(defaults, 'openAiLogsEnabled', false)
this._setValue(defaults, 'openai.spanCharLimit', 128)
this._setValue(defaults, 'peerServiceMapping', {})
Expand Down Expand Up @@ -674,6 +675,7 @@ class Config {
DD_TRACE_HEADER_TAGS,
DD_TRACE_LEGACY_BAGGAGE_ENABLED,
DD_TRACE_MEMCACHED_COMMAND_ENABLED,
DD_TRACE_MIDDLEWARE_TRACING_ENABLED,
DD_TRACE_OBFUSCATION_QUERY_STRING_REGEXP,
DD_TRACE_PARTIAL_FLUSH_MIN_SPANS,
DD_TRACE_PEER_SERVICE_MAPPING,
Expand Down Expand Up @@ -806,6 +808,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, 'middlewareTracingEnabled', DD_TRACE_MIDDLEWARE_TRACING_ENABLED)
this._setBoolean(env, 'openAiLogsEnabled', DD_OPENAI_LOGS_ENABLED)
this._setValue(env, 'openai.spanCharLimit', maybeInt(DD_OPENAI_SPAN_CHAR_LIMIT))
this._envUnprocessed.openaiSpanCharLimit = DD_OPENAI_SPAN_CHAR_LIMIT
Expand Down Expand Up @@ -989,6 +992,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, 'middlewareTracingEnabled', options.middlewareTracingEnabled)
this._setBoolean(opts, 'openAiLogsEnabled', options.openAiLogsEnabled)
this._setValue(opts, 'peerServiceMapping', options.peerServiceMapping)
this._setBoolean(opts, 'plugins', options.plugins)
Expand Down
10 changes: 9 additions & 1 deletion packages/dd-trace/src/plugin_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ module.exports = class PluginManager {
memcachedCommandEnabled,
ciVisibilityTestSessionName,
ciVisAgentlessLogSubmissionEnabled,
isTestDynamicInstrumentationEnabled
isTestDynamicInstrumentationEnabled,
middlewareTracingEnabled
} = this._tracerConfig

const sharedConfig = {
Expand Down Expand Up @@ -170,6 +171,13 @@ module.exports = class PluginManager {
sharedConfig.clientIpEnabled = clientIpEnabled
}

// For the global setting, we use the name `middlewareTracingEnabled`, but
// for the plugin-specific setting, we use `middleware`. They mean the same
// to an individual plugin, so we normalize them here.
if (middlewareTracingEnabled !== undefined) {
sharedConfig.middleware = middlewareTracingEnabled
}

return sharedConfig
}
}
11 changes: 11 additions & 0 deletions packages/dd-trace/test/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ describe('Config', () => {
expect(config).to.have.property('queryStringObfuscation').with.length(626)
expect(config).to.have.property('clientIpEnabled', false)
expect(config).to.have.property('clientIpHeader', null)
expect(config).to.have.property('middlewareTracingEnabled', true)
expect(config).to.have.nested.property('crashtracking.enabled', true)
expect(config).to.have.property('sampleRate', undefined)
expect(config).to.have.property('runtimeMetrics', false)
Expand Down Expand Up @@ -350,6 +351,7 @@ describe('Config', () => {
{ name: 'isTestDynamicInstrumentationEnabled', value: false, origin: 'default' },
{ name: 'logInjection', value: false, origin: 'default' },
{ name: 'lookup', value: undefined, origin: 'default' },
{ name: 'middlewareTracingEnabled', value: true, origin: 'default' },
{ name: 'openAiLogsEnabled', value: false, origin: 'default' },
{ name: 'openai.spanCharLimit', value: 128, origin: 'default' },
{ name: 'peerServiceMapping', value: {}, origin: 'default' },
Expand Down Expand Up @@ -478,6 +480,7 @@ describe('Config', () => {
process.env.DD_TRACE_EXPERIMENTAL_EXPORTER = 'log'
process.env.DD_TRACE_EXPERIMENTAL_GET_RUM_DATA_ENABLED = 'true'
process.env.DD_TRACE_EXPERIMENTAL_INTERNAL_ERRORS_ENABLED = 'true'
process.env.DD_TRACE_MIDDLEWARE_TRACING_ENABLED = 'false'
process.env.DD_TRACE_SPAN_ATTRIBUTE_SCHEMA = 'v1'
process.env.DD_TRACE_PEER_SERVICE_DEFAULTS_ENABLED = 'true'
process.env.DD_TRACE_REMOVE_INTEGRATION_SERVICE_NAMES_ENABLED = 'true'
Expand Down Expand Up @@ -549,6 +552,7 @@ describe('Config', () => {
expect(config).to.have.nested.property('crashtracking.enabled', false)
expect(config.grpc.client.error.statuses).to.deep.equal([3, 13, 400, 401, 402, 403])
expect(config.grpc.server.error.statuses).to.deep.equal([3, 13, 400, 401, 402, 403])
expect(config).to.have.property('middlewareTracingEnabled', false)
expect(config).to.have.property('runtimeMetrics', true)
expect(config).to.have.property('reportHostname', true)
expect(config).to.have.nested.property('codeOriginForSpans.enabled', true)
Expand Down Expand Up @@ -682,6 +686,7 @@ describe('Config', () => {
{ name: 'instrumentation_config_id', value: 'abcdef123', origin: 'env_var' },
{ name: 'injectionEnabled', value: ['profiler'], origin: 'env_var' },
{ name: 'isGCPFunction', value: false, origin: 'env_var' },
{ name: 'middlewareTracingEnabled', value: false, origin: 'env_var' },
{ name: 'peerServiceMapping', value: process.env.DD_TRACE_PEER_SERVICE_MAPPING, origin: 'env_var' },
{ name: 'port', value: '6218', origin: 'env_var' },
{ name: 'profiling.enabled', value: 'true', origin: 'env_var' },
Expand Down Expand Up @@ -847,6 +852,7 @@ describe('Config', () => {
tags,
flushInterval: 5000,
flushMinSpans: 500,
middlewareTracingEnabled: false,
runtimeMetrics: true,
reportHostname: true,
plugins: false,
Expand Down Expand Up @@ -923,6 +929,7 @@ describe('Config', () => {
expect(config).to.have.property('clientIpHeader', 'x-true-client-ip')
expect(config).to.have.property('flushInterval', 5000)
expect(config).to.have.property('flushMinSpans', 500)
expect(config).to.have.property('middlewareTracingEnabled', false)
expect(config).to.have.property('runtimeMetrics', true)
expect(config).to.have.property('reportHostname', true)
expect(config).to.have.property('plugins', false)
Expand Down Expand Up @@ -1012,6 +1019,7 @@ describe('Config', () => {
{ name: 'iast.requestSampling', value: 50, origin: 'code' },
{ name: 'iast.telemetryVerbosity', value: 'DEBUG', origin: 'code' },
{ name: 'iast.stackTrace.enabled', value: false, origin: 'code' },
{ name: 'middlewareTracingEnabled', value: false, origin: 'code' },
{ name: 'peerServiceMapping', value: { d: 'dd' }, origin: 'code' },
{ name: 'plugins', value: false, origin: 'code' },
{ name: 'port', value: '6218', origin: 'code' },
Expand Down Expand Up @@ -1212,6 +1220,7 @@ describe('Config', () => {
process.env.DD_TRACE_EXPERIMENTAL_EXPORTER = 'log'
process.env.DD_TRACE_EXPERIMENTAL_GET_RUM_DATA_ENABLED = 'true'
process.env.DD_TRACE_EXPERIMENTAL_INTERNAL_ERRORS_ENABLED = 'true'
process.env.DD_TRACE_MIDDLEWARE_TRACING_ENABLED = 'false'
process.env.DD_APPSEC_ENABLED = 'false'
process.env.DD_APPSEC_MAX_STACK_TRACES = '11'
process.env.DD_APPSEC_MAX_STACK_TRACE_DEPTH = '11'
Expand Down Expand Up @@ -1260,6 +1269,7 @@ describe('Config', () => {
tags: {
foo: 'foo'
},
middlewareTracingEnabled: true,
serviceMapping: {
b: 'bb'
},
Expand Down Expand Up @@ -1341,6 +1351,7 @@ describe('Config', () => {
expect(config).to.have.nested.property('dogstatsd.hostname', 'server')
expect(config).to.have.nested.property('dogstatsd.port', '8888')
expect(config).to.have.property('site', 'datadoghq.com')
expect(config).to.have.property('middlewareTracingEnabled', true)
expect(config).to.have.property('runtimeMetrics', false)
expect(config).to.have.property('reportHostname', false)
expect(config).to.have.property('flushMinSpans', 500)
Expand Down
Loading