这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: use specific W3CTraceContextPropagator for OTel #2036

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
merged 3 commits into from
May 8, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@google-cloud/projectify": "^5.0.0",
"@google-cloud/promisify": "^5.0.0",
"@opentelemetry/api": "~1.9.0",
"@opentelemetry/core": "^1.30.1",
"@opentelemetry/semantic-conventions": "~1.32.0",
"arrify": "^2.0.0",
"extend": "^3.0.2",
Expand All @@ -65,7 +66,6 @@
},
"devDependencies": {
"@grpc/proto-loader": "^0.7.13",
"@opentelemetry/core": "^1.17.0",
"@opentelemetry/sdk-trace-base": "^1.17.0",
"@types/duplexify": "^3.6.4",
"@types/extend": "^3.0.4",
Expand Down
18 changes: 15 additions & 3 deletions src/telemetry-tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import {
Span,
context,
trace,
propagation,
SpanKind,
TextMapGetter,
TextMapSetter,
ROOT_CONTEXT,
Context,
Link,
} from '@opentelemetry/api';
import {W3CTraceContextPropagator} from '@opentelemetry/core';
import {Attributes, PubsubMessage} from './publisher/pubsub-message';
import {Duration} from './temporal';

Expand Down Expand Up @@ -70,6 +70,14 @@ export enum OpenTelemetryLevel {
Modern = 2,
}

/**
* The W3C trace context propagator, used for injecting/extracting trace context.
*
* @private
* @internal
*/
const w3cTraceContextPropagator = new W3CTraceContextPropagator();

// True if user code elsewhere wants to enable OpenTelemetry support.
let globallyEnabled = false;

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

// Always do propagation injection with the trace context.
const context = trace.setSpanContext(ROOT_CONTEXT, span.spanContext());
propagation.inject(context, message, pubsubSetter);
w3cTraceContextPropagator.inject(context, message, pubsubSetter);

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

if (keys.includes(modernAttributeName)) {
context = propagation.extract(ROOT_CONTEXT, message, pubsubGetter);
context = w3cTraceContextPropagator.extract(
ROOT_CONTEXT,
message,
pubsubGetter,
);
}

const span = PubsubSpans.createReceiveSpan(
Expand Down
Loading