Skip to content

Commit e6acf12

Browse files
[serverless] add support for datadog extension (#1265)
* [serverless] add support for datadog extension * [serverless] pr feedback
1 parent ebec8d6 commit e6acf12

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

packages/dd-trace/src/config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ class Config {
114114
rateLimit: coalesce(ingestion.rateLimit, sampler.rateLimit, process.env.DD_TRACE_RATE_LIMIT)
115115
})
116116

117+
const inAWSLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined
118+
const defaultFlushInterval = inAWSLambda ? 0 : 2000
119+
117120
this.enabled = isTrue(DD_TRACE_ENABLED)
118121
this.debug = isTrue(DD_TRACE_DEBUG)
119122
this.logInjection = isTrue(DD_LOGS_INJECTION)
@@ -122,7 +125,7 @@ class Config {
122125
this.site = coalesce(options.site, process.env.DD_SITE, 'datadoghq.com')
123126
this.hostname = DD_AGENT_HOST || (this.url && this.url.hostname)
124127
this.port = String(DD_TRACE_AGENT_PORT || (this.url && this.url.port))
125-
this.flushInterval = coalesce(parseInt(options.flushInterval, 10), 2000)
128+
this.flushInterval = coalesce(parseInt(options.flushInterval, 10), defaultFlushInterval)
126129
this.sampleRate = coalesce(Math.min(Math.max(options.sampleRate, 0), 1), 1)
127130
this.logger = options.logger
128131
this.plugins = !!coalesce(options.plugins, true)

packages/dd-trace/src/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module.exports = {
99
REFERENCE_NOOP: 'noop',
1010
SAMPLING_RULE_DECISION: '_dd.rule_psr',
1111
SAMPLING_LIMIT_DECISION: '_dd.limit_psr',
12-
SAMPLING_AGENT_DECISION: '_dd.agent_psr'
12+
SAMPLING_AGENT_DECISION: '_dd.agent_psr',
13+
DATADOG_LAMBDA_EXTENSION_PATH: '/opt/extensions/datadog-agent'
1314
}

packages/dd-trace/src/exporter.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
33
const AgentExporter = require('./exporters/agent')
44
const LogExporter = require('./exporters/log')
55
const exporters = require('../../../ext/exporters')
6+
const fs = require('fs')
7+
const constants = require('./constants')
68

79
module.exports = name => {
810
const inAWSLambda = process.env.AWS_LAMBDA_FUNCTION_NAME !== undefined
11+
const usingLambdaExtension = inAWSLambda && fs.existsSync(constants.DATADOG_LAMBDA_EXTENSION_PATH)
912

1013
switch (name) {
1114
case exporters.LOG:
1215
return LogExporter
1316
case exporters.AGENT:
1417
return AgentExporter
1518
default:
16-
return inAWSLambda ? LogExporter : AgentExporter
19+
return inAWSLambda && !usingLambdaExtension ? LogExporter : AgentExporter
1720
}
1821
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const fs = require('fs')
23

34
const AgentExporter = require('../src/exporters/agent')
45
const LogExporter = require('../src/exporters/log')
@@ -29,6 +30,17 @@ describe('exporter', () => {
2930
expect(Exporter).to.be.equal(LogExporter)
3031
})
3132

33+
it('should create an AgentExporter when in Lambda environment with an extension', () => {
34+
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-func'
35+
const stub = sinon.stub(fs, 'existsSync')
36+
stub.withArgs('/opt/extensions/datadog-agent').returns(true)
37+
38+
const Exporter = require('../src/exporter')()
39+
40+
expect(Exporter).to.be.equal(AgentExporter)
41+
stub.restore()
42+
})
43+
3244
it('should allow configuring the exporter', () => {
3345
const Exporter = require('../src/exporter')('log')
3446

0 commit comments

Comments
 (0)