Skip to content

Commit a89b832

Browse files
authored
chore: align debugger and dogstatsd with normalized config (#7911)
Follow up on the generated config by updating config related code that still depended on former assumptions. This aligns debugger worker startup, proxy selection, and custom metrics clients with the actual configuration.
1 parent 6f2e2ca commit a89b832

8 files changed

Lines changed: 180 additions & 63 deletions

File tree

integration-tests/aiguard/index.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe('AIGuard SDK integration tests', () => {
3939
env: {
4040
DD_SERVICE: 'ai_guard_integration_test',
4141
DD_ENV: 'test',
42-
DD_TRACING_ENABLED: 'true',
43-
DD_TRACE_AGENT_PORT: agent.port,
42+
DD_TRACE_ENABLED: 'true',
43+
DD_TRACE_AGENT_PORT: String(agent.port),
4444
DD_AI_GUARD_ENABLED: 'true',
4545
DD_AI_GUARD_BLOCK: 'true',
4646
DD_AI_GUARD_ENDPOINT: `http://localhost:${api.address().port}`,

integration-tests/debugger/tracing-integration.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,45 @@ const assert = require('assert')
44
const { setup, testBasicInput, testBasicInputWithoutDD } = require('./utils')
55

66
describe('Dynamic Instrumentation', function () {
7+
describe('DD_TRACE_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=true', function () {
8+
const t = setup({
9+
testApp: 'target-app/basic.js',
10+
env: { DD_TRACE_ENABLED: 'true', DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED: true },
11+
dependencies: ['fastify'],
12+
})
13+
14+
describe('input messages', function () {
15+
it('should capture and send expected payload when a log line probe is triggered', testBasicInput.bind(null, t))
16+
})
17+
})
18+
19+
describe('DD_TRACE_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=false', function () {
20+
const t = setup({
21+
testApp: 'target-app/basic.js',
22+
env: { DD_TRACE_ENABLED: 'true', DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED: false },
23+
dependencies: ['fastify'],
24+
})
25+
26+
describe('input messages', function () {
27+
it('should capture and send expected payload when a log line probe is triggered', testBasicInput.bind(null, t))
28+
})
29+
})
30+
31+
describe('DD_TRACE_ENABLED=false', function () {
32+
const t = setup({
33+
testApp: 'target-app/basic.js',
34+
env: { DD_TRACE_ENABLED: 'false' },
35+
dependencies: ['fastify'],
36+
})
37+
38+
describe('input messages', function () {
39+
it(
40+
'should capture and send expected payload when a log line probe is triggered',
41+
testBasicInputWithoutDD.bind(null, t)
42+
)
43+
})
44+
})
45+
746
describe('DD_TRACING_ENABLED=true, DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED=true', function () {
847
const t = setup({
948
testApp: 'target-app/basic.js',

integration-tests/debugger/utils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,15 @@ function setupAssertionListeners (t, done, probe) {
304304
let traceId, spanId, dd
305305

306306
const messageListener = ({ payload }) => {
307-
const span = payload.find((arr) => arr[0].name === 'fastify.request')?.[0]
307+
const span = payload
308+
.flat()
309+
.find((span) => span.name === 'fastify.request' && (!dd || span.span_id.toString() === dd.span_id))
310+
308311
if (!span) return
309312

310313
traceId = span.trace_id.toString()
311314
spanId = span.span_id.toString()
312315

313-
t.agent.removeListener('message', messageListener)
314-
315316
assertDD()
316317
}
317318

@@ -336,6 +337,7 @@ function setupAssertionListeners (t, done, probe) {
336337
if (!traceId || !spanId || !dd) return
337338
assert.strictEqual(dd.trace_id, traceId)
338339
assert.strictEqual(dd.span_id, spanId)
340+
t.agent.removeListener('message', messageListener)
339341
done()
340342
}
341343
}

packages/datadog-plugin-openai/src/services.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports.init = function (tracerConfig) {
2020
`env:${tracerConfig.tags.env}`,
2121
`version:${tracerConfig.tags.version}`,
2222
],
23+
lookup: tracerConfig.lookup,
2324
})
2425
: new NoopDogStatsDClient()
2526

packages/datadog-plugin-openai/test/services.spec.js

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,60 @@
11
'use strict'
22

3+
const sinon = require('sinon')
4+
const proxyquire = require('proxyquire')
5+
36
const services = require('../src/services')
47
const { getConfigFresh } = require('../../dd-trace/test/helpers/config')
58

69
describe('Plugin', () => {
710
describe('openai services', () => {
8-
describe('when unconfigured', () => {
9-
afterEach(() => {
10-
services.shutdown()
11+
afterEach(() => {
12+
services.shutdown()
13+
})
14+
15+
it('should initialize DogStatsDClient with explicit config values', () => {
16+
const flush = sinon.stub()
17+
const DogStatsDClient = sinon.stub().returns({
18+
flush,
19+
})
20+
const ExternalLogger = sinon.stub().returns({
21+
log: sinon.stub(),
22+
})
23+
const NoopDogStatsDClient = sinon.stub()
24+
const NoopExternalLogger = sinon.stub()
25+
const proxiedServices = proxyquire('../src/services', {
26+
'../../dd-trace/src/dogstatsd': { DogStatsDClient },
27+
'../../dd-trace/src/noop/dogstatsd': NoopDogStatsDClient,
28+
'../../dd-trace/src/external-logger/src': {
29+
ExternalLogger,
30+
NoopExternalLogger,
31+
},
32+
})
33+
const config = getConfigFresh({
34+
env: 'prod',
35+
hostname: 'foo',
36+
service: 'bar',
37+
version: '1.2.3',
1138
})
1239

40+
proxiedServices.init(config)
41+
42+
sinon.assert.calledOnceWithExactly(DogStatsDClient, {
43+
host: config.dogstatsd.hostname,
44+
lookup: config.lookup,
45+
port: config.dogstatsd.port,
46+
tags: [
47+
'service:bar',
48+
'env:prod',
49+
'version:1.2.3',
50+
],
51+
})
52+
sinon.assert.notCalled(NoopDogStatsDClient)
53+
54+
proxiedServices.shutdown()
55+
})
56+
57+
describe('when unconfigured', () => {
1358
it('dogstatsd does not throw when missing .dogstatsd', () => {
1459
const service = services.init(getConfigFresh({
1560
hostname: 'foo',

packages/dd-trace/src/ci-visibility/dynamic-instrumentation/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const probeIdToResolveBreakpointSet = new Map()
1111
const probeIdToResolveBreakpointRemove = new Map()
1212

1313
class TestVisDynamicInstrumentation {
14+
/**
15+
* @param {import('../../config/config-base')} config - Tracer configuration
16+
*/
1417
constructor (config) {
1518
this._config = config
1619
this.worker = null
@@ -83,7 +86,6 @@ class TestVisDynamicInstrumentation {
8386
DD_TRACE_ENABLED: 'false',
8487
DD_TEST_FAILED_TEST_REPLAY_ENABLED: 'false',
8588
DD_CIVISIBILITY_MANUAL_API_ENABLED: 'false',
86-
DD_TRACING_ENABLED: 'false',
8789
DD_INSTRUMENTATION_TELEMETRY_ENABLED: 'false',
8890
},
8991
workerData: {
@@ -150,6 +152,9 @@ class TestVisDynamicInstrumentation {
150152

151153
let dynamicInstrumentation
152154

155+
/**
156+
* @param {import('../../config/config-base')} config - Tracer configuration
157+
*/
153158
module.exports = function createAndGetTestVisDynamicInstrumentation (config) {
154159
if (dynamicInstrumentation) {
155160
return dynamicInstrumentation

packages/dd-trace/src/dogstatsd.js

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

3-
const lookup = require('dns').lookup // cache to avoid instrumentation
43
const dgram = require('dgram')
54
const isIP = require('net').isIP
65

76
const request = require('./exporters/common/request')
87
const log = require('./log')
98
const Histogram = require('./histogram')
10-
const { defaults } = require('./config/defaults')
119
const { getAgentUrl } = require('./agent/url')
1210
const { entityId } = require('./exporters/common/docker')
1311

@@ -23,7 +21,9 @@ const TYPE_HISTOGRAM = 'h'
2321
* @implements {DogStatsD}
2422
*/
2523
class DogStatsDClient {
26-
constructor (options = {}) {
24+
#lookup
25+
constructor (options) {
26+
this.#lookup = options.lookup
2727
if (options.metricsProxyUrl) {
2828
this._httpOptions = {
2929
method: 'POST',
@@ -32,11 +32,10 @@ class DogStatsDClient {
3232
}
3333
}
3434

35-
this._host = options.host || defaults['dogstatsd.hostname']
35+
this._host = options.host
3636
this._family = isIP(this._host)
37-
this._port = options.port || defaults['dogstatsd.port']
38-
this._prefix = options.prefix || ''
39-
this._tags = options.tags || []
37+
this._port = options.port
38+
this._tags = options.tags
4039
this._queue = []
4140
this._buffer = ''
4241
this._offset = 0
@@ -99,7 +98,7 @@ class DogStatsDClient {
9998

10099
_sendUdp (queue) {
101100
if (this._family === 0) {
102-
lookup(this._host, (err, address, family) => {
101+
this.#lookup(this._host, (err, address, family) => {
103102
if (err) return log.error('DogStatsDClient: Host not found', err)
104103
this._sendUdpFromQueue(queue, address, family)
105104
})
@@ -118,7 +117,7 @@ class DogStatsDClient {
118117
}
119118

120119
_add (stat, value, type, tags) {
121-
let message = `${this._prefix + stat}:${value}|${type}`
120+
let message = `${stat}:${value}|${type}`
122121

123122
// Don't manipulate this._tags as it is still used
124123
tags = tags ? [...this._tags, ...tags] : this._tags
@@ -164,6 +163,9 @@ class DogStatsDClient {
164163
return socket
165164
}
166165

166+
/**
167+
* @param {import('./config/config-base')} config - Tracer configuration
168+
*/
167169
static generateClientConfig (config) {
168170
const tags = []
169171

@@ -183,6 +185,7 @@ class DogStatsDClient {
183185
host: config.dogstatsd.hostname,
184186
port: config.dogstatsd.port,
185187
tags,
188+
lookup: config.lookup,
186189
}
187190

188191
if (config.url || config.port) {

0 commit comments

Comments
 (0)