Skip to content

Commit

Permalink
fix error in http2 plugin when it was enabled after request start (#3714
Browse files Browse the repository at this point in the history
)

* fix error in http2 plugin when it was enabled after request start
  • Loading branch information
rochdev authored and sabrenner committed Feb 24, 2025
1 parent 2967596 commit 0730b7f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/datadog-plugin-http2/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class Http2ClientPlugin extends ClientPlugin {
}

bindAsyncStart ({ eventName, eventData, currentStore, parentStore }) {
// Plugin wasn't enabled when the request started.
if (!currentStore) return storage('legacy').getStore()

switch (eventName) {
case 'response':
this._onResponse(currentStore, eventData)
Expand Down
46 changes: 46 additions & 0 deletions packages/datadog-plugin-http2/test/client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,52 @@ describe('Plugin', () => {
})
})

describe('with late plugin initialization and an external subscriber', () => {
let ch
let sub

beforeEach(() => {
return agent.load('http2', { server: false })
.then(() => {
ch = require('dc-polyfill').channel('apm:http2:client:request:start')
sub = () => {}
tracer = require('../../dd-trace')
http2 = require('http2')
})
})

afterEach(() => {
ch.unsubscribe(sub)
})

it('should not crash', done => {
const app = (stream, headers) => {
stream.respond({
':status': 200
})
stream.end()
}

appListener = server(app, port => {
ch.subscribe(sub)

const client = http2
.connect(`${protocol}://localhost:${port}`)
.on('error', done)

tracer.use('http2', false)

const req = client.request({ ':path': '/user', ':method': 'GET' })
req.on('error', done)
req.on('response', () => done())

tracer.use('http2', true)

req.end()
})
})
})

describe('with validateStatus configuration', () => {
let config

Expand Down

0 comments on commit 0730b7f

Please sign in to comment.