Skip to content

Commit cfc7f06

Browse files
committed
feat(opentelemetry): Add getters for root spans contexts
1 parent f7196ba commit cfc7f06

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

packages/plugins/opentelemetry/src/plugin.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ export type OpenTelemetryContextExtension = {
218218
openTelemetry: {
219219
tracer: Tracer;
220220
activeContext: () => Context;
221+
httpContext: (request?: Request) => Context | undefined;
222+
operationContext: (context?: unknown) => Context | undefined;
223+
executionRequestContext: (
224+
ExecutionRequest: ExecutionRequest,
225+
) => Context | undefined;
221226
};
222227
};
223228

@@ -706,7 +711,20 @@ export function useOpenTelemetry(
706711
extendContext({
707712
openTelemetry: {
708713
tracer,
709-
activeContext: () => getContext(state),
714+
httpContext: (request) => {
715+
const { forRequest } = request ? getState({ request }) : state;
716+
return forRequest.otel?.root;
717+
},
718+
operationContext: (context) => {
719+
const { forOperation } = context ? getState({ context }) : state;
720+
return forOperation.otel?.root;
721+
},
722+
executionRequestContext: (executionRequest) => {
723+
return getState({ executionRequest }).forSubgraphExecution.otel
724+
?.root;
725+
},
726+
activeContext: (contextMatcher?: Parameters<typeof getState>[0]) =>
727+
getContext(contextMatcher ? getState(contextMatcher) : state),
710728
},
711729
});
712730
},

packages/plugins/opentelemetry/tests/useOpenTelemetry.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,11 @@ describe('useOpenTelemetry', () => {
481481
const createSpan =
482482
(name: string) =>
483483
(
484-
matcher: Parameters<(typeof otelPlugin)['getOtelContext']>[0],
484+
matcher: Parameters<(typeof otelPlugin)['getActiveContext']>[0],
485485
) =>
486486
otelPlugin
487487
.getTracer()
488-
.startSpan(name, {}, otelPlugin.getOtelContext(matcher))
488+
.startSpan(name, {}, otelPlugin.getActiveContext(matcher))
489489
.end();
490490

491491
return [

packages/plugins/opentelemetry/tests/yoga.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ describe('useOpenTelemetry', () => {
114114
const createSpan =
115115
(name: string) =>
116116
(
117-
matcher: Parameters<(typeof otelPlugin)['getOtelContext']>[0],
117+
matcher: Parameters<(typeof otelPlugin)['getActiveContext']>[0],
118118
) =>
119119
otelPlugin
120120
.getTracer()
121-
.startSpan(name, {}, otelPlugin.getOtelContext(matcher))
121+
.startSpan(name, {}, otelPlugin.getActiveContext(matcher))
122122
.end();
123123

124124
return [

0 commit comments

Comments
 (0)