Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing: fix http client synchronous errors causing spans to not finish #5294

Merged
merged 4 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/datadog-instrumentations/src/http/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ function patch (http, methodName) {
} catch (e) {
ctx.error = e
errorChannel.publish(ctx)
// if the initial request failed, ctx.req will be unset, we must close the span here
// fix for: https://github.com/DataDog/dd-trace-js/issues/5016
if (!ctx.req) {
finish()
}
throw e
} finally {
endChannel.publish(ctx)
Expand Down
4 changes: 3 additions & 1 deletion packages/datadog-plugin-http/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ class HttpClientPlugin extends ClientPlugin {
addResponseHeaders(res, span, this.config)
}

addRequestHeaders(req, span, this.config)
if (req) {
addRequestHeaders(req, span, this.config)
}

this.config.hooks.request(span, req, res)

Expand Down
18 changes: 18 additions & 0 deletions packages/datadog-plugin-http/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,24 @@ describe('Plugin', () => {
})
})
}

it('should record unfinished http requests as error spans', done => {
agent
.use(traces => {
expect(traces[0][0]).to.have.property('error', 1)
expect(traces[0][0].meta).to.not.have.property('http.status_code')
})
.then(done)
.catch(done)

try {
http.request('http://httpbin.org/get', { headers: { BadHeader: 'a\nb' } }, res => {
res.on('data', () => { })
})
} catch {
// expected to throw error
}
})
})

describe('with late plugin initialization and an external subscriber', () => {
Expand Down
Loading