-
Notifications
You must be signed in to change notification settings - Fork 1k
feat(instrumentation-http): provide http.request.header.<key> at server span creation time
#6396
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
Merged
trentm
merged 6 commits into
open-telemetry:main
from
vitorvasc:instr-http_header-capture
Mar 2, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d9d18ef
chore(instrumentation-http): provide http.request.header at server sp…
vitorvasc 1283b83
chore(experimental): update changelog
vitorvasc 1ee20af
style(instrumentation-http): fix formatting
vitorvasc 3d2d6bd
Merge branch 'main' into instr-http_header-capture
vitorvasc 1761986
chore(changelog): update changelog
vitorvasc 87fbb15
test(http-instrumentation): add http-sampler test to validate Sampler…
vitorvasc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...imental/packages/opentelemetry-instrumentation-http/test/functionals/http-sampler.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { | ||
| Attributes, | ||
| Context, | ||
| ContextManager, | ||
| Link, | ||
| SpanKind, | ||
| context, | ||
| } from '@opentelemetry/api'; | ||
| import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; | ||
| import { | ||
| InMemorySpanExporter, | ||
| Sampler, | ||
| SamplingDecision, | ||
| SamplingResult, | ||
| SimpleSpanProcessor, | ||
| } from '@opentelemetry/sdk-trace-base'; | ||
| import * as assert from 'assert'; | ||
| import { HttpInstrumentation } from '../../src/http'; | ||
| import { httpRequest } from '../utils/httpRequest'; | ||
|
|
||
| class CapturingSampler implements Sampler { | ||
| public capturedAttributes: Attributes | undefined; | ||
|
|
||
| shouldSample( | ||
| _context: Context, | ||
| _traceId: string, | ||
| _spanName: string, | ||
| _spanKind: SpanKind, | ||
| attributes: Attributes, | ||
| _links: Link[] | ||
| ): SamplingResult { | ||
| this.capturedAttributes = attributes; | ||
| return { decision: SamplingDecision.RECORD_AND_SAMPLED }; | ||
| } | ||
|
|
||
| toString(): string { | ||
| return 'CapturingSampler'; | ||
| } | ||
| } | ||
|
|
||
| const sampler = new CapturingSampler(); | ||
|
|
||
| const instrumentation = new HttpInstrumentation({ | ||
| headersToSpanAttributes: { | ||
| server: { requestHeaders: ['x-custom-header'] }, | ||
| }, | ||
| }); | ||
| instrumentation.enable(); | ||
| instrumentation.disable(); | ||
|
|
||
| import * as http from 'http'; | ||
| import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; | ||
|
|
||
| const memoryExporter = new InMemorySpanExporter(); | ||
| const provider = new NodeTracerProvider({ | ||
| sampler, | ||
| spanProcessors: [new SimpleSpanProcessor(memoryExporter)], | ||
| }); | ||
| instrumentation.setTracerProvider(provider); | ||
|
|
||
| describe('HttpInstrumentation sampler integration', () => { | ||
| const PORT = 22399; | ||
| let server: http.Server; | ||
| let contextManager: ContextManager; | ||
|
|
||
| before(async () => { | ||
| instrumentation.enable(); | ||
| server = http.createServer((_req, res) => { | ||
| res.writeHead(200); | ||
| res.end(); | ||
| }); | ||
| await new Promise<void>(resolve => server.listen(PORT, resolve)); | ||
| }); | ||
|
|
||
| after(done => { | ||
| instrumentation.disable(); | ||
| server.close(done); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| contextManager = new AsyncHooksContextManager(); | ||
| context.setGlobalContextManager(contextManager); | ||
| memoryExporter.reset(); | ||
| sampler.capturedAttributes = undefined; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| context.disable(); | ||
| }); | ||
|
|
||
| it('provides http.request.header.* attributes to shouldSample', async () => { | ||
| await httpRequest.get(`http://localhost:${PORT}/`, { | ||
| headers: { 'x-custom-header': 'test-value' }, | ||
| }); | ||
|
|
||
| assert.deepStrictEqual( | ||
| sampler.capturedAttributes?.['http.request.header.x_custom_header'], | ||
| ['test-value'] | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New behavior moves server request header capture into the span creation attributes so sampling can use
http.request.header.*. There isn't a test that verifies these header attributes are visible to the sampler atstartSpantime (e.g., via a custom Sampler'sshouldSamplereceiving them). Consider adding a functional/unit test that installs a Sampler capturing theattributesargument and asserts the configured header keys/values are present when the server span is created.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added:
87fbb15