Skip to content

Commit 205a001

Browse files
authored
Enable log injection by default for structured loggers (#5859)
1 parent fc288e7 commit 205a001

File tree

14 files changed

+106
-19
lines changed

14 files changed

+106
-19
lines changed

integration-tests/automatic-log-submission.spec.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ describe('test visibility automatic log submission', () => {
174174
)
175175
childProcess.on('exit', () => {
176176
assert.include(testOutput, 'Hello simple log!')
177-
assert.notInclude(testOutput, 'span_id')
178-
done()
177+
assert.include(testOutput, 'span_id')
178+
logsPromise.then(() => {
179+
done()
180+
}).catch(done)
179181
})
180182

181183
childProcess.stdout.on('data', (chunk) => {
@@ -184,6 +186,13 @@ describe('test visibility automatic log submission', () => {
184186
childProcess.stderr.on('data', (chunk) => {
185187
testOutput += chunk.toString()
186188
})
189+
190+
const logsPromise = receiver
191+
.gatherPayloadsMaxTimeout(({ url }) => url.includes('/api/v2/logs'), payloads => {
192+
if (payloads.length > 0) {
193+
throw new Error('Unexpected logs')
194+
}
195+
}, 5000)
187196
})
188197

189198
it('does not submit logs when DD_AGENTLESS_LOG_SUBMISSION_ENABLED is set but DD_API_KEY is not', (done) => {

integration-tests/pino.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { FakeAgent, spawnProc, createSandbox, curl } = require('./helpers')
3+
const { FakeAgent, spawnProc, createSandbox, curl, assertObjectContains } = require('./helpers')
44
const path = require('path')
55
const { assert } = require('chai')
66
const { once } = require('events')
@@ -32,6 +32,27 @@ describe('pino test', () => {
3232
await agent.stop()
3333
})
3434

35+
it('Log injection enabled by default', async () => {
36+
proc = await spawnProc(startupTestFile, {
37+
cwd,
38+
env: {
39+
AGENT_PORT: agent.port
40+
},
41+
stdio: 'pipe',
42+
})
43+
const [data] = await Promise.all([once(proc.stdout, 'data'), curl(proc)])
44+
const stdoutData = JSON.parse(data.toString())
45+
assertObjectContains(stdoutData, {
46+
dd: {
47+
trace_id: stdoutData.custom.trace_id,
48+
span_id: stdoutData.custom.span_id
49+
},
50+
custom: {
51+
trace_id: stdoutData.dd.trace_id,
52+
span_id: stdoutData.dd.span_id
53+
}
54+
})
55+
})
3556
it('Log injection enabled', async () => {
3657
proc = await spawnProc(startupTestFile, {
3758
cwd,
@@ -60,6 +81,7 @@ describe('pino test', () => {
6081
cwd,
6182
env: {
6283
AGENT_PORT: agent.port,
84+
lOG_INJECTION: false
6385
},
6486
stdio: 'pipe',
6587
})

integration-tests/telemetry.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('telemetry', () => {
6666
await agent.assertTelemetryReceived(msg => {
6767
const { configuration } = msg.payload.payload
6868
assertObjectContains(configuration, [
69-
{ name: 'DD_LOG_INJECTION', value: false, origin: 'default' },
69+
{ name: 'DD_LOG_INJECTION', value: 'structured', origin: 'default' },
7070
{ name: 'DD_LOG_INJECTION', value: true, origin: 'env_var' },
7171
{ name: 'DD_LOG_INJECTION', value: false, origin: 'code' }
7272
])

packages/datadog-plugin-bunyan/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const LogPlugin = require('../../dd-trace/src/plugins/log_plugin')
3+
const StructuredLogPlugin = require('../../dd-trace/src/plugins/structured_log_plugin')
44

5-
class BunyanPlugin extends LogPlugin {
5+
class BunyanPlugin extends StructuredLogPlugin {
66
static get id () {
77
return 'bunyan'
88
}

packages/datadog-plugin-bunyan/test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('Plugin', () => {
4949

5050
const record = JSON.parse(stream.write.firstCall.args[0].toString())
5151

52-
expect(record).to.not.have.property('dd')
52+
expect(record).to.have.property('dd')
5353
})
5454
})
5555
})

packages/datadog-plugin-pino/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const LogPlugin = require('../../dd-trace/src/plugins/log_plugin')
3+
const StructuredLogPlugin = require('../../dd-trace/src/plugins/structured_log_plugin')
44

5-
class PinoPlugin extends LogPlugin {
5+
class PinoPlugin extends StructuredLogPlugin {
66
static get id () {
77
return 'pino'
88
}

packages/datadog-plugin-pino/test/index.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Plugin', () => {
6464

6565
const record = JSON.parse(stream.write.firstCall.args[0].toString())
6666

67-
expect(record).to.not.have.property('dd')
67+
expect(record).to.have.property('dd')
6868
expect(record).to.have.deep.property('msg', 'message')
6969
})
7070
})
@@ -80,8 +80,8 @@ describe('Plugin', () => {
8080

8181
const record = stream.write.firstCall.args[0].toString()
8282

83-
expect(record).to.not.include('trace_id')
84-
expect(record).to.not.include('span_id')
83+
expect(record).to.include('trace_id')
84+
expect(record).to.include('span_id')
8585
expect(record).to.include('message')
8686
})
8787
})

packages/datadog-plugin-winston/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const LogPlugin = require('../../dd-trace/src/plugins/log_plugin')
3+
const StructuredLogPlugin = require('../../dd-trace/src/plugins/structured_log_plugin')
44

5-
class WinstonPlugin extends LogPlugin {
5+
class WinstonPlugin extends StructuredLogPlugin {
66
static get id () {
77
return 'winston'
88
}

packages/datadog-plugin-winston/test/index.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('Plugin', () => {
131131
tracer.scope().activate(span, () => {
132132
winston.info('message')
133133

134-
expect(spy).to.not.have.been.calledWithMatch(meta.dd)
134+
expect(spy).to.have.been.calledWithMatch(meta.dd)
135135
})
136136
})
137137
})
@@ -336,6 +336,7 @@ describe('Plugin', () => {
336336
expect(await logServer.logPromise).to.include(meta.dd)
337337
})
338338
})
339+
339340
// Only run this test with Winston v3.17.0+ since it uses newer format functions
340341
if (semver.intersects(version, '>=3.17.0')) {
341342
describe('with error formatting matching temp.js example', () => {

packages/dd-trace/src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ class Config {
545545
defaults.testManagementAttemptToFixRetries = 20
546546
defaults.isTestManagementEnabled = false
547547
defaults.isImpactedTestsEnabled = false
548-
defaults.logInjection = false
548+
defaults.logInjection = 'structured'
549549
defaults.lookup = undefined
550550
defaults.inferredProxyServicesEnabled = false
551551
defaults.memcachedCommandEnabled = false

packages/dd-trace/src/plugins/log_plugin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ module.exports = class LogPlugin extends Plugin {
4545
})
4646
}
4747

48+
_isEnabled (config) {
49+
return config.enabled && (config.logInjection === true || config.ciVisAgentlessLogSubmissionEnabled)
50+
}
51+
4852
configure (config) {
4953
return super.configure({
5054
...config,
51-
enabled: config.enabled && (config.logInjection || config.ciVisAgentlessLogSubmissionEnabled)
55+
enabled: this._isEnabled(config)
5256
})
5357
}
5458
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict'
2+
3+
const LogPlugin = require('./log_plugin')
4+
5+
module.exports = class StructuredLogPlugin extends LogPlugin {
6+
_isEnabled (config) {
7+
return super._isEnabled(config) || (config.enabled && config.logInjection === 'structured')
8+
}
9+
}

packages/dd-trace/test/config.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,10 @@ describe('Config', () => {
427427
{ name: 'langchain.spanPromptCompletionSampleRate', value: 1.0, origin: 'default' },
428428
{ name: 'llmobs.agentlessEnabled', value: undefined, origin: 'default' },
429429
{ name: 'llmobs.mlApp', value: undefined, origin: 'default' },
430-
{ name: 'logInjection', value: false, origin: 'default' },
430+
{ name: 'ciVisibilityTestSessionName', value: '', origin: 'default' },
431+
{ name: 'ciVisAgentlessLogSubmissionEnabled', value: false, origin: 'default' },
432+
{ name: 'isTestDynamicInstrumentationEnabled', value: false, origin: 'default' },
433+
{ name: 'logInjection', value: 'structured', origin: 'default' },
431434
{ name: 'lookup', value: undefined, origin: 'default' },
432435
{ name: 'middlewareTracingEnabled', value: true, origin: 'default' },
433436
{ name: 'openai.spanCharLimit', value: 128, origin: 'default' },

packages/dd-trace/test/plugins/log_plugin.spec.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require('../setup/tap')
44

55
const LogPlugin = require('../../src/plugins/log_plugin')
6+
const BunyanPlugin = require('../../../datadog-plugin-bunyan/src/index')
67
const Tracer = require('../../src/tracer')
78
const Config = require('../../src/config')
89

@@ -29,7 +30,7 @@ const tracer = new Tracer(new Config({
2930
...config
3031
}))
3132

32-
const plugin = new TestLog({
33+
let plugin = new TestLog({
3334
_tracer: tracer
3435
})
3536
plugin.configure({
@@ -65,4 +66,42 @@ describe('LogPlugin', () => {
6566
expect(message.dd).to.have.property('span_id', span.context().toSpanId())
6667
})
6768
})
69+
70+
it('should inject logs for only structured loggers when logInjection is structured', () => {
71+
plugin.configure({
72+
logInjection: 'structured',
73+
enabled: true
74+
})
75+
const unstructuredLoggerSpan = tracer.startSpan('unstructured logger')
76+
77+
tracer.scope().activate(unstructuredLoggerSpan, () => {
78+
const data = { message: {} }
79+
testLogChannel.publish(data)
80+
const { message } = data
81+
82+
expect(message.dd).to.be.undefined
83+
})
84+
85+
plugin = new BunyanPlugin({
86+
_tracer: tracer
87+
})
88+
plugin.configure({
89+
logInjection: 'structured',
90+
enabled: true
91+
})
92+
93+
const structuredLoggerSpan = tracer.startSpan('structured logger')
94+
const structuredLogChannel = channel('apm:bunyan:log')
95+
96+
tracer.scope().activate(structuredLoggerSpan, () => {
97+
const data = { message: {} }
98+
structuredLogChannel.publish(data)
99+
const { message } = data
100+
101+
expect(message.dd).to.contain(config)
102+
103+
expect(message.dd).to.have.property('trace_id', structuredLoggerSpan.context().toTraceId(true))
104+
expect(message.dd).to.have.property('span_id', structuredLoggerSpan.context().toSpanId())
105+
})
106+
})
68107
})

0 commit comments

Comments
 (0)