Skip to content

Commit c9a9656

Browse files
feat: use specific W3CTraceContextPropagator for OTel (#2036)
* feat: Use specific W3CTraceContextPropagator for OTel Replaces the use of the global OpenTelemetry propagator (propagation) with a specific instance of W3CTraceContextPropagator within the telemetry-tracing.ts module. This ensures that trace context propagation for Pub/Sub messages always uses the W3C format via the googclient_traceparent attribute, regardless of the global propagator configured by user code. This maintains consistent tracing behaviour as it moves between library instances as a Pub/Sub attribute. * chore: linting fixes * build: move @opentelemetry/core to deps to get the trace propagator --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 4108a69 commit c9a9656

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@google-cloud/projectify": "^5.0.0",
5454
"@google-cloud/promisify": "^5.0.0",
5555
"@opentelemetry/api": "~1.9.0",
56+
"@opentelemetry/core": "^1.30.1",
5657
"@opentelemetry/semantic-conventions": "~1.32.0",
5758
"arrify": "^2.0.0",
5859
"extend": "^3.0.2",
@@ -65,7 +66,6 @@
6566
},
6667
"devDependencies": {
6768
"@grpc/proto-loader": "^0.7.13",
68-
"@opentelemetry/core": "^1.17.0",
6969
"@opentelemetry/sdk-trace-base": "^1.17.0",
7070
"@types/duplexify": "^3.6.4",
7171
"@types/extend": "^3.0.4",

src/telemetry-tracing.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ import {
1919
Span,
2020
context,
2121
trace,
22-
propagation,
2322
SpanKind,
2423
TextMapGetter,
2524
TextMapSetter,
2625
ROOT_CONTEXT,
2726
Context,
2827
Link,
2928
} from '@opentelemetry/api';
29+
import {W3CTraceContextPropagator} from '@opentelemetry/core';
3030
import {Attributes, PubsubMessage} from './publisher/pubsub-message';
3131
import {Duration} from './temporal';
3232

@@ -70,6 +70,14 @@ export enum OpenTelemetryLevel {
7070
Modern = 2,
7171
}
7272

73+
/**
74+
* The W3C trace context propagator, used for injecting/extracting trace context.
75+
*
76+
* @private
77+
* @internal
78+
*/
79+
const w3cTraceContextPropagator = new W3CTraceContextPropagator();
80+
7381
// True if user code elsewhere wants to enable OpenTelemetry support.
7482
let globallyEnabled = false;
7583

@@ -749,7 +757,7 @@ export function injectSpan(span: Span, message: MessageWithAttributes): void {
749757

750758
// Always do propagation injection with the trace context.
751759
const context = trace.setSpanContext(ROOT_CONTEXT, span.spanContext());
752-
propagation.inject(context, message, pubsubSetter);
760+
w3cTraceContextPropagator.inject(context, message, pubsubSetter);
753761

754762
// Also put the direct reference to the Span object for while we're
755763
// passing it around in the client library.
@@ -802,7 +810,11 @@ export function extractSpan(
802810
let context: Context | undefined;
803811

804812
if (keys.includes(modernAttributeName)) {
805-
context = propagation.extract(ROOT_CONTEXT, message, pubsubGetter);
813+
context = w3cTraceContextPropagator.extract(
814+
ROOT_CONTEXT,
815+
message,
816+
pubsubGetter,
817+
);
806818
}
807819

808820
const span = PubsubSpans.createReceiveSpan(

0 commit comments

Comments
 (0)