-
Notifications
You must be signed in to change notification settings - Fork 20
feat(ARSN-572): Propagate W3C trace context to MongoDB metadata writes #2611
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
Open
delthas
wants to merge
1
commit into
development/8.3
Choose a base branch
from
improvement/ARSN-572/trace-context
base: development/8.3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { context, propagation, trace } from '@opentelemetry/api'; | ||
|
|
||
| export interface TraceContextCarrier { | ||
| traceparent?: string; | ||
| tracestate?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Set or clear the `traceContext` field on a metadata-shaped object. | ||
| * Used by every site that stamps trace context onto a write — both raw | ||
| * ObjectMDData manipulation and the ObjectMD model setter — so the | ||
| * set/clear semantics live in exactly one place. Always reflects the | ||
| * current write: passing undefined (or a value without `traceparent`) | ||
| * clears any previously-set context so a stale value is not carried | ||
| * forward from a loaded ObjectMD. | ||
| * | ||
| * Generic over `data` so callers can use either an ObjectMD's `_data` | ||
| * or a raw `ObjectMDData` without type gymnastics. | ||
| */ | ||
| export function applyTraceContext( | ||
| data: { traceContext?: TraceContextCarrier }, | ||
| tc?: TraceContextCarrier, | ||
| ): void { | ||
| if (tc && tc.traceparent) { | ||
| data.traceContext = tc; | ||
| } else { | ||
| delete data.traceContext; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Capture the currently-active OTEL trace context as a plain object | ||
| * suitable for storing alongside metadata (ends up in the MongoDB oplog). | ||
| * Returns undefined when no trace is active (e.g., OTEL not enabled, | ||
| * or called outside a traced request). | ||
| */ | ||
| export function captureCurrentTraceContext(): | ||
| { traceparent: string; tracestate?: string } | undefined { | ||
| const ctx = context.active(); | ||
| const span = trace.getSpan(ctx); | ||
| if (!span) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const carrier: Record<string, string> = {}; | ||
| propagation.inject(ctx, carrier, { | ||
| set: (c, k, v) => { c[k] = v; }, | ||
| }); | ||
| if (!carrier.traceparent) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const out: { traceparent: string; tracestate?: string } = { | ||
| traceparent: carrier.traceparent, | ||
| }; | ||
| if (carrier.tracestate) { | ||
| out.tracestate = carrier.tracestate; | ||
| } | ||
| return out; | ||
| } | ||
|
delthas marked this conversation as resolved.
delthas marked this conversation as resolved.
delthas marked this conversation as resolved.
|
||
|
delthas marked this conversation as resolved.
|
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
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,83 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
|
|
||
| // Mock @opentelemetry/api so we can drive captureCurrentTraceContext | ||
| // without needing a registered SDK or propagator. The api package is a | ||
| // runtime dep of arsenal but we can stub the small surface it consumes. | ||
| const mockActive = jest.fn(); | ||
| const mockGetSpan = jest.fn(); | ||
| const mockInject = jest.fn(); | ||
|
|
||
| jest.mock('@opentelemetry/api', () => ({ | ||
| context: { active: mockActive }, | ||
| trace: { getSpan: mockGetSpan }, | ||
| propagation: { inject: mockInject }, | ||
| })); | ||
|
|
||
| const { | ||
| captureCurrentTraceContext, | ||
| } = require('../../../../lib/storage/metadata/captureTraceContext'); | ||
|
|
||
| describe('captureCurrentTraceContext', () => { | ||
| beforeEach(() => { | ||
| mockActive.mockReset(); | ||
| mockGetSpan.mockReset(); | ||
| mockInject.mockReset(); | ||
| mockActive.mockReturnValue({ tag: 'mock-active-context' }); | ||
| }); | ||
|
|
||
| it('returns undefined when no span is active', () => { | ||
| mockGetSpan.mockReturnValue(undefined); | ||
| assert.strictEqual(captureCurrentTraceContext(), undefined); | ||
| // No injection should be attempted when there is no active span. | ||
| assert.strictEqual(mockInject.mock.calls.length, 0); | ||
| }); | ||
|
|
||
| it('returns { traceparent } when active span yields a traceparent', () => { | ||
| mockGetSpan.mockReturnValue({ /* opaque span object */ }); | ||
| mockInject.mockImplementation((ctx, carrier, setter) => { | ||
| setter.set(carrier, 'traceparent', | ||
| '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'); | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(captureCurrentTraceContext(), { | ||
| traceparent: | ||
| '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01', | ||
| }); | ||
|
|
||
| // Verify inject was given the active context so propagation | ||
| // sees the right state, not a freshly-constructed empty one. | ||
| assert.strictEqual(mockInject.mock.calls.length, 1); | ||
| const [ctxArg, carrierArg] = mockInject.mock.calls[0]; | ||
| assert.deepStrictEqual(ctxArg, { tag: 'mock-active-context' }); | ||
| assert.strictEqual(typeof carrierArg, 'object'); | ||
| }); | ||
|
|
||
| it('returns both traceparent and tracestate when both are present', () => { | ||
| mockGetSpan.mockReturnValue({}); | ||
| mockInject.mockImplementation((ctx, carrier, setter) => { | ||
| setter.set(carrier, 'traceparent', | ||
| '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01'); | ||
| setter.set(carrier, 'tracestate', 'rojo=00f067aa0ba902b7'); | ||
| }); | ||
|
|
||
| assert.deepStrictEqual(captureCurrentTraceContext(), { | ||
| traceparent: | ||
| '00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01', | ||
| tracestate: 'rojo=00f067aa0ba902b7', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns undefined when propagation injects no traceparent', () => { | ||
| // Defensive case: a misbehaving propagator that exposes only | ||
| // unrelated headers. We must not return a partial / invalid | ||
| // trace context. | ||
| mockGetSpan.mockReturnValue({}); | ||
| mockInject.mockImplementation((ctx, carrier, setter) => { | ||
| setter.set(carrier, 'baggage', 'unrelated=true'); | ||
| }); | ||
|
|
||
| assert.strictEqual(captureCurrentTraceContext(), undefined); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
State is a string or a list of possible value ?
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.
Those names are standard, come from a W3C spec: https://www.w3.org/TR/trace-context/
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.
@delthas indeed, I saw that we use them in several place in the code but when stored in mongo, under the traceContext structure, it don't make sense anymore to keep
traceThere 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.
I'd heavily lean towards keeping the standard names, as they have specific formats, with meanings specified in the specification. Specifically,
traceparentis not a plainparentIdat all, it is a carefull formatted string, such astraceparent: 00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01, which carries a version, a trace ID, a span ID, and flags, including the standard "is sampled" flag.parentIdwould be outright wrong, but even if we gave it a better name, it would be less precise and more confusing thantraceparent, which is exactly the format the consumer would use. For example, see my backbeat code that recreates an OTEL span from saved trace context: tracecontext and traceparent are the field names used by the library.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.
If you prefer to keep it, then let's go. It still don't make sense for me but I'm fine to move forward