Skip to content

Commit

Permalink
init: remove legacy span creation (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
danstarns authored Apr 28, 2024
1 parent d383560 commit 400c989
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 90 deletions.
2 changes: 2 additions & 0 deletions packages/opentelemetry/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ export {
} from "@opentelemetry/api";

export { InstrumentationOption } from "@opentelemetry/instrumentation";

export { Span as ApiSpan } from "@opentelemetry/api";
92 changes: 24 additions & 68 deletions packages/opentelemetry/src/run-in-span.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
Attributes,
Context,
SpanContext,
Span,
SpanKind,
SpanStatusCode,
Tracer,
trace,
} from "@opentelemetry/api";
import { RandomIdGenerator, Span } from "@opentelemetry/sdk-trace-base";

export type RunInChildSpanOptions = {
name: string;
Expand All @@ -16,79 +16,35 @@ export type RunInChildSpanOptions = {
attributes?: Attributes;
};

export function createLegacySpan({
options,
parentContext,
}: {
options: RunInChildSpanOptions;
parentContext?: SpanContext;
}) {
if (parentContext) {
return new Span(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.tracer,
options.context,
options.name,
{
traceId: parentContext.traceId,
spanId: new RandomIdGenerator().generateSpanId(),
traceFlags: 1,
},
SpanKind.INTERNAL,
parentContext.spanId,
);
} else {
return options.tracer.startActiveSpan(
options.name,
{ attributes: options.attributes },
options.context,
(span): Span => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return span;
},
);
}
}

/**
* Simplified runInSpan function to reduce complexity and improve performance.
* Wraps a callback within a span, setting attributes and handling errors.
*/
export async function runInSpan<R>(
options: RunInChildSpanOptions,
cb: (span: Span) => R | Promise<R>,
) {
if (options.parentSpan) {
const parentContext = options.parentSpan.spanContext();
): Promise<R> {
const { tracer, name, context, attributes } = options;

const span = createLegacySpan({ options, parentContext });

Object.entries(options.attributes || {}).forEach(([key, value]) => {
if (value) {
span.setAttribute(key, value);
}
});
const parentSpan = options.parentSpan;
const traceCTX = context;
const ctx = parentSpan ? trace.setSpan(traceCTX, parentSpan) : traceCTX;

try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return await cb(span);
} catch (error) {
const e = error as Error;
span.setStatus({ code: SpanStatusCode.ERROR, message: e.message });
span.recordException(e);
throw error;
} finally {
span.end();
}
}

return options.tracer.startActiveSpan(
options.name,
{ attributes: options.attributes },
options.context,
return tracer.startActiveSpan(
name,
{
attributes,
kind: SpanKind.INTERNAL,
},
ctx,
async (span) => {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (attributes) {
for (const [key, value] of Object.entries(attributes)) {
span.setAttribute(key, value as any);
}
}

return await cb(span);
} catch (error) {
const e = error as Error;
Expand Down
10 changes: 5 additions & 5 deletions packages/trace-directive/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ApiSpan,
Context,
Span,
Tracer,
context,
getTracer,
Expand All @@ -24,7 +24,7 @@ export interface GraphQLDebuggerContextOptions {
export class GraphQLDebuggerContext {
private context?: Context;
public tracer: Tracer;
private rootSpan?: Span;
private rootSpan?: ApiSpan;
public includeContext?: boolean;
public includeVariables?: boolean;
public includeResult?: boolean;
Expand All @@ -48,11 +48,11 @@ export class GraphQLDebuggerContext {
return this.context;
}

setRootSpan(span: Span) {
setRootSpan(span: ApiSpan) {
this.rootSpan = span;
}

getRootSpan(): Span | undefined {
getRootSpan(): ApiSpan | undefined {
return this.rootSpan;
}

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

const currentSpan = input.graphqlContext.currentSpan as Span | undefined;
const currentSpan = input.graphqlContext.currentSpan as ApiSpan | undefined;

return runInSpan(
{
Expand Down
32 changes: 15 additions & 17 deletions plugins/apollo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseAdapter } from "@graphql-debugger/adapter-base";
import { ProxyAdapter } from "@graphql-debugger/adapter-proxy";
import {
ApiSpan,
SetupOtelInput,
Span,
SpanStatusCode,
createLegacySpan,
infoToAttributes,
infoToSpanName,
context as otelContext,
setupOtel,
trace,
} from "@graphql-debugger/opentelemetry";
import {
GraphQLDebuggerContext,
Expand All @@ -20,8 +20,8 @@ import { Path } from "graphql/jsutils/Path";

type GraphQLContext = {
GraphQLDebuggerContext?: GraphQLDebuggerContext;
parentSpan?: Span | undefined;
currentSpan?: Span | undefined;
parentSpan?: ApiSpan | undefined;
currentSpan?: ApiSpan | undefined;
};

function generatePathString(path: Path | undefined): string {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const graphqlDebuggerPlugin = ({
setupOtel({ exporterConfig, instrumentations });
},
requestDidStart: async () => {
const spanMap = new Map<string, Span>();
const spanMap = new Map<string, ApiSpan>();

return {
async executionDidStart(requestContext) {
Expand All @@ -82,7 +82,11 @@ export const graphqlDebuggerPlugin = ({
? spanMap.get(parentPath)
: undefined;

const traceCTX = otelContext.active();
const currentContext = otelContext.active();

const ctx = parentSpan
? trace.setSpan(currentContext, parentSpan)
: currentContext;

const attributes = infoToAttributes({
info: fieldCtx.info,
Expand All @@ -95,19 +99,13 @@ export const graphqlDebuggerPlugin = ({
info: fieldCtx.info,
});

const span = createLegacySpan({
options: {
name: spanName,
context: traceCTX,
tracer: internalCtx.tracer,
const span = internalCtx.tracer.startSpan(
spanName,
{
attributes,
},
...(parentSpan
? {
parentContext: parentSpan?.spanContext(),
}
: {}),
});
ctx,
);

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

Expand Down

0 comments on commit 400c989

Please sign in to comment.