Skip to content

Commit 400c989

Browse files
authored
init: remove legacy span creation (#290)
1 parent d383560 commit 400c989

File tree

4 files changed

+46
-90
lines changed

4 files changed

+46
-90
lines changed

packages/opentelemetry/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ export {
2525
} from "@opentelemetry/api";
2626

2727
export { InstrumentationOption } from "@opentelemetry/instrumentation";
28+
29+
export { Span as ApiSpan } from "@opentelemetry/api";

packages/opentelemetry/src/run-in-span.ts

Lines changed: 24 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
22
Attributes,
33
Context,
4-
SpanContext,
4+
Span,
55
SpanKind,
66
SpanStatusCode,
77
Tracer,
8+
trace,
89
} from "@opentelemetry/api";
9-
import { RandomIdGenerator, Span } from "@opentelemetry/sdk-trace-base";
1010

1111
export type RunInChildSpanOptions = {
1212
name: string;
@@ -16,79 +16,35 @@ export type RunInChildSpanOptions = {
1616
attributes?: Attributes;
1717
};
1818

19-
export function createLegacySpan({
20-
options,
21-
parentContext,
22-
}: {
23-
options: RunInChildSpanOptions;
24-
parentContext?: SpanContext;
25-
}) {
26-
if (parentContext) {
27-
return new Span(
28-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
29-
// @ts-ignore
30-
options.tracer,
31-
options.context,
32-
options.name,
33-
{
34-
traceId: parentContext.traceId,
35-
spanId: new RandomIdGenerator().generateSpanId(),
36-
traceFlags: 1,
37-
},
38-
SpanKind.INTERNAL,
39-
parentContext.spanId,
40-
);
41-
} else {
42-
return options.tracer.startActiveSpan(
43-
options.name,
44-
{ attributes: options.attributes },
45-
options.context,
46-
(span): Span => {
47-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
48-
// @ts-ignore
49-
return span;
50-
},
51-
);
52-
}
53-
}
54-
19+
/**
20+
* Simplified runInSpan function to reduce complexity and improve performance.
21+
* Wraps a callback within a span, setting attributes and handling errors.
22+
*/
5523
export async function runInSpan<R>(
5624
options: RunInChildSpanOptions,
5725
cb: (span: Span) => R | Promise<R>,
58-
) {
59-
if (options.parentSpan) {
60-
const parentContext = options.parentSpan.spanContext();
26+
): Promise<R> {
27+
const { tracer, name, context, attributes } = options;
6128

62-
const span = createLegacySpan({ options, parentContext });
63-
64-
Object.entries(options.attributes || {}).forEach(([key, value]) => {
65-
if (value) {
66-
span.setAttribute(key, value);
67-
}
68-
});
29+
const parentSpan = options.parentSpan;
30+
const traceCTX = context;
31+
const ctx = parentSpan ? trace.setSpan(traceCTX, parentSpan) : traceCTX;
6932

70-
try {
71-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
72-
// @ts-ignore
73-
return await cb(span);
74-
} catch (error) {
75-
const e = error as Error;
76-
span.setStatus({ code: SpanStatusCode.ERROR, message: e.message });
77-
span.recordException(e);
78-
throw error;
79-
} finally {
80-
span.end();
81-
}
82-
}
83-
84-
return options.tracer.startActiveSpan(
85-
options.name,
86-
{ attributes: options.attributes },
87-
options.context,
33+
return tracer.startActiveSpan(
34+
name,
35+
{
36+
attributes,
37+
kind: SpanKind.INTERNAL,
38+
},
39+
ctx,
8840
async (span) => {
8941
try {
90-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
91-
// @ts-ignore
42+
if (attributes) {
43+
for (const [key, value] of Object.entries(attributes)) {
44+
span.setAttribute(key, value as any);
45+
}
46+
}
47+
9248
return await cb(span);
9349
} catch (error) {
9450
const e = error as Error;

packages/trace-directive/src/context.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2+
ApiSpan,
23
Context,
3-
Span,
44
Tracer,
55
context,
66
getTracer,
@@ -24,7 +24,7 @@ export interface GraphQLDebuggerContextOptions {
2424
export class GraphQLDebuggerContext {
2525
private context?: Context;
2626
public tracer: Tracer;
27-
private rootSpan?: Span;
27+
private rootSpan?: ApiSpan;
2828
public includeContext?: boolean;
2929
public includeVariables?: boolean;
3030
public includeResult?: boolean;
@@ -48,11 +48,11 @@ export class GraphQLDebuggerContext {
4848
return this.context;
4949
}
5050

51-
setRootSpan(span: Span) {
51+
setRootSpan(span: ApiSpan) {
5252
this.rootSpan = span;
5353
}
5454

55-
getRootSpan(): Span | undefined {
55+
getRootSpan(): ApiSpan | undefined {
5656
return this.rootSpan;
5757
}
5858

@@ -80,7 +80,7 @@ export class GraphQLDebuggerContext {
8080
const traceCTX: Context = parentContext || context.active();
8181
internalCtx.setContext(traceCTX);
8282

83-
const currentSpan = input.graphqlContext.currentSpan as Span | undefined;
83+
const currentSpan = input.graphqlContext.currentSpan as ApiSpan | undefined;
8484

8585
return runInSpan(
8686
{

plugins/apollo/src/index.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { BaseAdapter } from "@graphql-debugger/adapter-base";
22
import { ProxyAdapter } from "@graphql-debugger/adapter-proxy";
33
import {
4+
ApiSpan,
45
SetupOtelInput,
5-
Span,
66
SpanStatusCode,
7-
createLegacySpan,
87
infoToAttributes,
98
infoToSpanName,
109
context as otelContext,
1110
setupOtel,
11+
trace,
1212
} from "@graphql-debugger/opentelemetry";
1313
import {
1414
GraphQLDebuggerContext,
@@ -20,8 +20,8 @@ import { Path } from "graphql/jsutils/Path";
2020

2121
type GraphQLContext = {
2222
GraphQLDebuggerContext?: GraphQLDebuggerContext;
23-
parentSpan?: Span | undefined;
24-
currentSpan?: Span | undefined;
23+
parentSpan?: ApiSpan | undefined;
24+
currentSpan?: ApiSpan | undefined;
2525
};
2626

2727
function generatePathString(path: Path | undefined): string {
@@ -62,7 +62,7 @@ export const graphqlDebuggerPlugin = ({
6262
setupOtel({ exporterConfig, instrumentations });
6363
},
6464
requestDidStart: async () => {
65-
const spanMap = new Map<string, Span>();
65+
const spanMap = new Map<string, ApiSpan>();
6666

6767
return {
6868
async executionDidStart(requestContext) {
@@ -82,7 +82,11 @@ export const graphqlDebuggerPlugin = ({
8282
? spanMap.get(parentPath)
8383
: undefined;
8484

85-
const traceCTX = otelContext.active();
85+
const currentContext = otelContext.active();
86+
87+
const ctx = parentSpan
88+
? trace.setSpan(currentContext, parentSpan)
89+
: currentContext;
8690

8791
const attributes = infoToAttributes({
8892
info: fieldCtx.info,
@@ -95,19 +99,13 @@ export const graphqlDebuggerPlugin = ({
9599
info: fieldCtx.info,
96100
});
97101

98-
const span = createLegacySpan({
99-
options: {
100-
name: spanName,
101-
context: traceCTX,
102-
tracer: internalCtx.tracer,
102+
const span = internalCtx.tracer.startSpan(
103+
spanName,
104+
{
103105
attributes,
104106
},
105-
...(parentSpan
106-
? {
107-
parentContext: parentSpan?.spanContext(),
108-
}
109-
: {}),
110-
});
107+
ctx,
108+
);
111109

112110
const currentPathString = generatePathString(fieldCtx.info.path);
113111

0 commit comments

Comments
 (0)