Skip to content

Commit cf892cc

Browse files
authored
Include send-data missing headers and organize telemetry config variables (#3055)
* Include send-data missing headers * Rename DD_TELEMETRY_DEBUG_ENABLED as DD_TELEMETRY_DIAGNOSTIC_LOG_COLLECTION_ENABLED * Move DD_TELEMETRY_HEARTBEAT_INTERVAL to config.js * Remove DD_TELEMETRY_DIAGNOSTIC_LOG_COLLECTION_ENABLED
1 parent 09bee57 commit cf892cc

File tree

9 files changed

+102
-141
lines changed

9 files changed

+102
-141
lines changed

packages/dd-trace/src/appsec/iast/telemetry/logs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function sendLogs () {
2828
}
2929

3030
function isLevelEnabled (level) {
31-
return isLogCollectionEnabled(config) && (level !== 'DEBUG' || config.telemetry.debug)
31+
return isLogCollectionEnabled(config) && level !== 'DEBUG'
3232
}
3333

3434
function isLogCollectionEnabled (config) {

packages/dd-trace/src/config.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,11 @@ class Config {
216216
process.env.DD_TRACE_TELEMETRY_ENABLED,
217217
!inServerlessEnvironment
218218
)
219-
const DD_TELEMETRY_DEBUG_ENABLED = coalesce(
220-
process.env.DD_TELEMETRY_DEBUG_ENABLED,
219+
const DD_TELEMETRY_HEARTBEAT_INTERVAL = process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL
220+
? parseInt(process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL) * 1000
221+
: 60000
222+
const DD_TELEMETRY_DEBUG = coalesce(
223+
process.env.DD_TELEMETRY_DEBUG,
221224
false
222225
)
223226
const DD_TRACE_AGENT_PROTOCOL_VERSION = coalesce(
@@ -513,8 +516,9 @@ ken|consumer_?(?:id|key|secret)|sign(?:ed|ature)?|auth(?:entication|orization)?)
513516
// Disabled for CI Visibility's agentless
514517
this.telemetry = {
515518
enabled: DD_TRACE_EXPORTER !== 'datadog' && isTrue(DD_TRACE_TELEMETRY_ENABLED),
519+
heartbeatInterval: DD_TELEMETRY_HEARTBEAT_INTERVAL,
516520
logCollection: isTrue(DD_TELEMETRY_LOG_COLLECTION_ENABLED),
517-
debug: isTrue(DD_TELEMETRY_DEBUG_ENABLED)
521+
debug: isTrue(DD_TELEMETRY_DEBUG)
518522
}
519523
this.protocolVersion = DD_TRACE_AGENT_PROTOCOL_VERSION
520524
this.tagsHeaderMaxLength = parseInt(DD_TRACE_X_DATADOG_TAGS_MAX_LENGTH)

packages/dd-trace/src/telemetry/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ const os = require('os')
66
const dependencies = require('./dependencies')
77
const { sendData } = require('./send-data')
88

9-
const HEARTBEAT_INTERVAL = process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL
10-
? Number(process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL) * 1000
11-
: 60000
12-
139
const telemetryStartChannel = dc.channel('datadog:telemetry:start')
1410
const telemetryStopChannel = dc.channel('datadog:telemetry:stop')
1511

@@ -19,6 +15,7 @@ let pluginManager
1915
let application
2016
let host
2117
let interval
18+
let heartbeatInterval
2219
const sentIntegrations = new Set()
2320

2421
function getIntegrations () {
@@ -108,7 +105,7 @@ function createHostObject () {
108105
}
109106

110107
function getTelemetryData () {
111-
return { config, application, host, heartbeatInterval: HEARTBEAT_INTERVAL }
108+
return { config, application, host, heartbeatInterval }
112109
}
113110

114111
function start (aConfig, thePluginManager) {
@@ -119,11 +116,13 @@ function start (aConfig, thePluginManager) {
119116
pluginManager = thePluginManager
120117
application = createAppObject()
121118
host = createHostObject()
119+
heartbeatInterval = config.telemetry.heartbeatInterval
120+
122121
dependencies.start(config, application, host)
123122
sendData(config, application, host, 'app-started', appStarted())
124123
interval = setInterval(() => {
125124
sendData(config, application, host, 'app-heartbeat')
126-
}, HEARTBEAT_INTERVAL)
125+
}, heartbeatInterval)
127126
interval.unref()
128127
process.on('beforeExit', onBeforeExit)
129128

packages/dd-trace/src/telemetry/send-data.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
const request = require('../exporters/common/request')
2+
3+
function getHeaders (config, application, reqType) {
4+
const headers = {
5+
'content-type': 'application/json',
6+
'dd-telemetry-api-version': 'v1',
7+
'dd-telemetry-request-type': reqType,
8+
'dd-client-library-language': application.language_name,
9+
'dd-client-library-version': application.tracer_version
10+
}
11+
const debug = config.telemetry && config.telemetry.debug
12+
if (debug) {
13+
headers['dd-telemetry-debug-enabled'] = 'true'
14+
}
15+
return headers
16+
}
17+
218
let seqId = 0
319

420
function getPayload (payload) {
@@ -25,11 +41,7 @@ function sendData (config, application, host, reqType, payload = {}) {
2541
port,
2642
method: 'POST',
2743
path: '/telemetry/proxy/api/v2/apmtelemetry',
28-
headers: {
29-
'content-type': 'application/json',
30-
'dd-telemetry-api-version': 'v1',
31-
'dd-telemetry-request-type': reqType
32-
}
44+
headers: getHeaders(config, application, reqType)
3345
}
3446
const data = JSON.stringify({
3547
api_version: 'v1',

packages/dd-trace/test/appsec/iast/iast-log.spec.js

+1-86
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@ const ddBasePath = calculateDDBasePath(__dirname)
66
const EOL = '\n'
77

88
describe('IAST log', () => {
9-
const telemetryDebugConfig = {
10-
config: {
11-
telemetry: {
12-
logCollection: true,
13-
debug: true
14-
}
15-
}
16-
}
17-
189
const telemetryDefaultConfig = {
1910
config: {
2011
telemetry: {
21-
logCollection: true,
22-
debug: false
12+
logCollection: true
2313
}
2414
}
2515
}
@@ -77,81 +67,6 @@ describe('IAST log', () => {
7767
telemetryLogs.stop()
7868
})
7969

80-
describe('debug', () => {
81-
it('should call log.debug', () => {
82-
iastLog.debug('debug')
83-
84-
expect(log.debug).to.be.calledOnceWith('debug')
85-
})
86-
87-
it('should call log.debug and not publish msg via telemetry', () => {
88-
iastLog.debugAndPublish('debug')
89-
90-
expect(log.debug).to.be.calledOnceWith('debug')
91-
expect(telemetryLogs.publish).to.not.be.called
92-
})
93-
94-
it('should call log.debug and publish msg via telemetry', () => {
95-
onTelemetryStart(telemetryDebugConfig)
96-
97-
iastLog.debugAndPublish('debug')
98-
99-
expect(log.debug).to.be.calledOnceWith('debug')
100-
expect(telemetryLogs.publish).to.be.calledOnceWith({ message: 'debug', level: 'DEBUG' })
101-
})
102-
103-
it('should chain multiple debug calls', () => {
104-
onTelemetryStart(telemetryDebugConfig)
105-
106-
iastLog.debug('debug').debugAndPublish('debugAndPublish').debug('debug2')
107-
108-
expect(log.debug).to.be.calledThrice
109-
expect(log.debug.getCall(0).args[0]).to.be.eq('debug')
110-
expect(log.debug.getCall(1).args[0]).to.be.eq('debugAndPublish')
111-
expect(log.debug.getCall(2).args[0]).to.be.eq('debug2')
112-
expect(telemetryLogs.publish).to.be.calledOnceWith({ message: 'debugAndPublish', level: 'DEBUG' })
113-
})
114-
115-
it('should chain multiple debug calls', () => {
116-
onTelemetryStart(telemetryDebugConfig)
117-
118-
iastLog.debug('debug')
119-
120-
telemetryLogs.stop()
121-
122-
iastLog.debugAndPublish('debugAndPublish').debug('debug2')
123-
})
124-
})
125-
126-
describe('info', () => {
127-
it('should call log.info', () => {
128-
iastLog.info('info')
129-
130-
expect(log.info).to.be.calledOnceWith('info')
131-
})
132-
133-
it('should call log.info and publish msg via telemetry', () => {
134-
onTelemetryStart(telemetryDebugConfig)
135-
136-
iastLog.infoAndPublish('info')
137-
138-
expect(log.info).to.be.calledOnceWith('info')
139-
expect(telemetryLogs.publish).to.be.calledOnceWith({ message: 'info', level: 'DEBUG' })
140-
})
141-
142-
it('should chain multiple info calls', () => {
143-
onTelemetryStart(telemetryDebugConfig)
144-
145-
iastLog.info('info').infoAndPublish('infoAndPublish').info('info2')
146-
147-
expect(log.info).to.be.calledThrice
148-
expect(log.info.getCall(0).args[0]).to.be.eq('info')
149-
expect(log.info.getCall(1).args[0]).to.be.eq('infoAndPublish')
150-
expect(log.info.getCall(2).args[0]).to.be.eq('info2')
151-
expect(telemetryLogs.publish).to.be.calledOnceWith({ message: 'infoAndPublish', level: 'DEBUG' })
152-
})
153-
})
154-
15570
describe('warn', () => {
15671
it('should call log.warn', () => {
15772
iastLog.warn('warn')

packages/dd-trace/test/appsec/iast/telemetry/logs.spec.js

+1-26
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,7 @@ describe('telemetry logs', () => {
131131
})
132132

133133
describe('sendData', () => {
134-
const app = {}
135-
const host = {}
136-
137-
it('should be called with DEBUG level and error if config.telemetry.debug = true', () => {
138-
const logCollectorAdd = sinon.stub()
139-
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
140-
'../../../../../diagnostics_channel': dc,
141-
'./log_collector': {
142-
add: logCollectorAdd
143-
}
144-
})
145-
logs.start()
146-
147-
onTelemetryStartMsg.config.telemetry.debug = true
148-
onTelemetryStartMsg.application = app
149-
onTelemetryStartMsg.host = host
150-
onTelemetryStart()(onTelemetryStartMsg)
151-
152-
const error = new Error('test')
153-
const stack = error.stack
154-
logs.publish({ message: error.message, stack_trace: stack, level: 'DEBUG' })
155-
156-
expect(logCollectorAdd).to.be.calledOnceWith(match({ message: 'test', level: 'DEBUG', stack_trace: stack }))
157-
})
158-
159-
it('should be not called with DEBUG level if config.telemetry.debug = false', () => {
134+
it('should be not called with DEBUG level', () => {
160135
const logCollectorAdd = sinon.stub()
161136
const logs = proxyquire('../../../../src/appsec/iast/telemetry/logs', {
162137
'../../../../../diagnostics_channel': dc,

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

+16-4
Original file line numberDiff line numberDiff line change
@@ -822,10 +822,22 @@ describe('Config', () => {
822822

823823
expect(config.telemetry).to.not.be.undefined
824824
expect(config.telemetry.enabled).to.be.true
825+
expect(config.telemetry.heartbeatInterval).to.eq(60000)
825826
expect(config.telemetry.logCollection).to.be.false
826827
expect(config.telemetry.debug).to.be.false
827828
})
828829

830+
it('should set DD_TELEMETRY_HEARTBEAT_INTERVAL', () => {
831+
const origTelemetryHeartbeatIntervalValue = process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL
832+
process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL = '42'
833+
834+
const config = new Config()
835+
836+
expect(config.telemetry.heartbeatInterval).to.eq(42000)
837+
838+
process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL = origTelemetryHeartbeatIntervalValue
839+
})
840+
829841
it('should not set DD_TRACE_TELEMETRY_ENABLED', () => {
830842
const origTraceTelemetryValue = process.env.DD_TRACE_TELEMETRY_ENABLED
831843
process.env.DD_TRACE_TELEMETRY_ENABLED = 'false'
@@ -859,15 +871,15 @@ describe('Config', () => {
859871
process.env.DD_IAST_ENABLED = origIastEnabledValue
860872
})
861873

862-
it('should set DD_TELEMETRY_DEBUG_ENABLED', () => {
863-
const origTelemetryDebugValue = process.env.DD_TELEMETRY_DEBUG_ENABLED
864-
process.env.DD_TELEMETRY_DEBUG_ENABLED = 'true'
874+
it('should set DD_TELEMETRY_DEBUG', () => {
875+
const origTelemetryDebugValue = process.env.DD_TELEMETRY_DEBUG
876+
process.env.DD_TELEMETRY_DEBUG = 'true'
865877

866878
const config = new Config()
867879

868880
expect(config.telemetry.debug).to.be.true
869881

870-
process.env.DD_TELEMETRY_DEBUG_ENABLED = origTelemetryDebugValue
882+
process.env.DD_TELEMETRY_DEBUG = origTelemetryDebugValue
871883
})
872884

873885
it('should not set DD_REMOTE_CONFIGURATION_ENABLED if AWS_LAMBDA_FUNCTION_NAME is present', () => {

packages/dd-trace/test/telemetry/index.spec.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('telemetry', () => {
6464
circularObject.child.parent = circularObject
6565

6666
telemetry.start({
67-
telemetry: { enabled: true },
67+
telemetry: { enabled: true, heartbeatInterval: 60000 },
6868
hostname: 'localhost',
6969
port: traceAgent.address().port,
7070
service: 'test service',
@@ -154,7 +154,7 @@ describe('telemetry', () => {
154154
expect.fail('server should not be called')
155155
}).listen(0, () => {
156156
telemetry.start({
157-
telemetry: { enabled: false },
157+
telemetry: { enabled: false, heartbeatInterval: 60000 },
158158
hostname: 'localhost',
159159
port: server.address().port
160160
})
@@ -169,8 +169,6 @@ describe('telemetry', () => {
169169

170170
describe('telemetry with interval change', () => {
171171
it('should set the interval correctly', (done) => {
172-
process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL = 12345
173-
174172
const telemetry = proxyquire('../../src/telemetry', {
175173
'../exporters/common/docker': {
176174
id () {
@@ -190,7 +188,7 @@ describe('telemetry with interval change', () => {
190188
}
191189

192190
telemetry.start({
193-
telemetry: { enabled: true },
191+
telemetry: { enabled: true, heartbeatInterval: 12345000 },
194192
hostname: 'localhost',
195193
port: 8126,
196194
service: 'test service',
@@ -205,7 +203,6 @@ describe('telemetry with interval change', () => {
205203

206204
process.nextTick(() => {
207205
expect(intervalSetCorrectly).to.be.true
208-
delete process.env.DD_TELEMETRY_HEARTBEAT_INTERVAL
209206
done()
210207
})
211208
})

0 commit comments

Comments
 (0)