Skip to content

Commit

Permalink
fix(lambda): gate timeout spans and add missing clear (#4446)
Browse files Browse the repository at this point in the history
* add missing `clearTimeout`

* gate timeout spans under `lambda`

* add unit tests

* add comment on when we can disable it
  • Loading branch information
duncanista authored and bengl committed Aug 29, 2024
1 parent e9611c8 commit 8217c9e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/dd-trace/src/lambda/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ exports.datadog = function datadog (lambdaHandler) {
return res
})
}
clearTimeout(__lambdaTimeout)
return result
}
}
13 changes: 12 additions & 1 deletion packages/dd-trace/src/lambda/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,15 @@

const { registerLambdaHook } = require('./runtime/ritm')

registerLambdaHook()
/**
* It is safe to do it this way, since customers will never be expected to disable
* this specific instrumentation through the init config object.
*/
const _DD_TRACE_DISABLED_INSTRUMENTATIONS = process.env.DD_TRACE_DISABLED_INSTRUMENTATIONS || ''
const _disabledInstrumentations = new Set(
_DD_TRACE_DISABLED_INSTRUMENTATIONS ? _DD_TRACE_DISABLED_INSTRUMENTATIONS.split(',') : []
)

if (!_disabledInstrumentations.has('lambda')) {
registerLambdaHook()
}
20 changes: 18 additions & 2 deletions packages/dd-trace/test/lambda/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const path = require('path')

const ritm = require('../../src/lambda/runtime/ritm')
const agent = require('../plugins/agent')

const oldEnv = process.env
Expand Down Expand Up @@ -33,7 +32,7 @@ const restoreEnv = () => {
*/
const loadAgent = ({ exporter = 'agent' } = {}) => {
// Make sure the hook is re-registered
ritm.registerLambdaHook()
require('../../src/lambda')
return agent.load(null, [], {
experimental: {
exporter
Expand All @@ -50,6 +49,8 @@ const closeAgent = () => {
// In testing, the patch needs to be deleted from the require cache,
// in order to allow multiple handlers being patched properly.
delete require.cache[require.resolve('../../src/lambda/runtime/patch.js')]
delete require.cache[require.resolve('../../src/lambda')]

agent.close({ ritmReset: true })
}

Expand Down Expand Up @@ -199,6 +200,21 @@ describe('lambda', () => {
})
await checkTraces
})

it('doesnt patch lambda when instrumentation is disabled', async () => {
const _handlerPath = path.resolve(__dirname, './fixtures/handler.js')
const handlerBefore = require(_handlerPath).handler

// Set the desired handler to patch
process.env.DD_TRACE_DISABLED_INSTRUMENTATIONS = 'lambda'
process.env.DD_LAMBDA_HANDLER = 'handler.handler'
// Register hook for patching
await loadAgent()

// Mock `datadog-lambda` handler resolve and import.
const handlerAfter = require(_handlerPath).handler
expect(handlerBefore).to.equal(handlerAfter)
})
})

describe('timeout spans', () => {
Expand Down

0 comments on commit 8217c9e

Please sign in to comment.