Skip to content

Commit

Permalink
fix: Ensure opts.headers is defined in http instrumentation (#1926)
Browse files Browse the repository at this point in the history
  • Loading branch information
OuranosSkia committed Dec 21, 2023
1 parent bd0a5dc commit 7ea31a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/instrumentation/core/http-outbound.js
Expand Up @@ -138,6 +138,8 @@ function instrumentRequest(agent, opts, makeRequest, hostname, segment) {
const transaction = segment.transaction
const outboundHeaders = Object.create(null)

opts.headers = opts.headers || {}

synthetics.assignHeadersToOutgoingRequest(agent.config, transaction, outboundHeaders)
maybeAddDtCatHeaders(agent, transaction, outboundHeaders, opts?.headers)
opts.headers = assignOutgoingHeaders(opts.headers, outboundHeaders)
Expand Down
18 changes: 18 additions & 0 deletions test/unit/instrumentation/http/outbound.test.js
Expand Up @@ -272,6 +272,24 @@ tap.test('instrumentOutbound', (t) => {
}
t.end()
})

t.test('should not crash when req.headers is null', (t) => {
const req = new events.EventEmitter()
helper.runInTransaction(agent, function () {
const path = '/asdf'

instrumentOutbound(agent, { headers: null, host: HOSTNAME, port: PORT }, makeFakeRequest)

function makeFakeRequest(opts) {
t.ok(opts.headers, 'should assign headers when null')
t.ok(opts.headers.traceparent, 'traceparent should exist')
req.path = path
return req
}
})
t.end()
})

t.end()
})

Expand Down

0 comments on commit 7ea31a3

Please sign in to comment.