Skip to content

Commit 2015eee

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

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

packages/plugins/opentelemetry/src/plugin.ts

Lines changed: 35 additions & 3 deletions
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?: any) => Context | undefined;
223+
executionRequestContext: (
224+
ExecutionRequest: ExecutionRequest,
225+
) => Context | undefined;
221226
};
222227
};
223228

@@ -231,12 +236,17 @@ type State = Partial<
231236

232237
export type OpenTelemetryPlugin =
233238
GatewayPlugin<OpenTelemetryContextExtension> & {
234-
getOtelContext: (payload: {
239+
getActiveContext: (payload: {
235240
request?: Request;
236241
context?: any;
237242
executionRequest?: ExecutionRequest;
238243
}) => Context;
239244
getTracer(): Tracer;
245+
getHttpContext: (request: Request) => Context | undefined;
246+
getOperationContext: (context: any) => Context | undefined;
247+
getExecutionRequestContext: (
248+
ExecutionRequest: ExecutionRequest,
249+
) => Context | undefined;
240250
};
241251

242252
export function useOpenTelemetry(
@@ -321,7 +331,16 @@ export function useOpenTelemetry(
321331
OtelState
322332
>((getState) => ({
323333
getTracer: () => tracer,
324-
getOtelContext: ({ state }) => getContext(state),
334+
getActiveContext: ({ state }) => getContext(state),
335+
getHttpContext: (request) => {
336+
return getState({ request }).forRequest.otel?.root;
337+
},
338+
getOperationContext: (context) => {
339+
return getState({ context }).forOperation.otel?.root;
340+
},
341+
getExecutionRequestContext: (executionRequest) => {
342+
return getState({ executionRequest }).forSubgraphExecution.otel?.root;
343+
},
325344
instrumentation: {
326345
request({ state: { forRequest }, request }, wrapped) {
327346
if (!shouldTrace(traces.spans?.http, { request })) {
@@ -706,7 +725,20 @@ export function useOpenTelemetry(
706725
extendContext({
707726
openTelemetry: {
708727
tracer,
709-
activeContext: () => getContext(state),
728+
httpContext: (request) => {
729+
const { forRequest } = request ? getState({ request }) : state;
730+
return forRequest.otel?.root;
731+
},
732+
operationContext: (context) => {
733+
const { forOperation } = context ? getState({ context }) : state;
734+
return forOperation.otel?.root;
735+
},
736+
executionRequestContext: (executionRequest) => {
737+
return getState({ executionRequest }).forSubgraphExecution.otel
738+
?.root;
739+
},
740+
activeContext: (contextMatcher?: Parameters<typeof getState>[0]) =>
741+
getContext(contextMatcher ? getState(contextMatcher) : state),
710742
},
711743
});
712744
},

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)