diff --git a/src/telemetry.ts b/src/telemetry.ts index 30bb180..555f5f1 100644 --- a/src/telemetry.ts +++ b/src/telemetry.ts @@ -15,9 +15,7 @@ class Span { ?.getTransaction() ?.startChild(data); - this.otelSpan = opentelemetry.trace.getTracer('biothings-explorer-thread').startSpan((data as any).description ?? "", undefined, Telemetry.getOtelContext()); - // if spans should be nested, use the following line - //Telemetry.setOtelContext(opentelemetry.trace.setSpan(opentelemetry.context.active(), this.otelSpan)); + this.otelSpan = opentelemetry.trace.getTracer('biothings-explorer-thread').startSpan((data as any).description ?? "", undefined, opentelemetry.trace.setSpan(opentelemetry.context.active(), Telemetry.getOtelSpan())); } catch (error) { debug(`Sentry span start error. ${reassurance}`); debug(error); @@ -37,7 +35,6 @@ class Span { try { this.span?.finish(); this.otelSpan?.end(); - Telemetry.removeOtelContext(); } catch (error) { debug(`Sentry finish error. ${reassurance}`); debug(error); @@ -46,8 +43,6 @@ class Span { } export class Telemetry { - static otelCtx: OtelContext[] = []; - static startSpan(data: unknown) { return new Span(data); } @@ -62,13 +57,13 @@ export class Telemetry { debug(error); } } - static setOtelContext(newCtx: OtelContext) { - this.otelCtx.push(newCtx); + static setOtelSpan(newOtelSpan: OtelSpan) { + global.otelSpan = newOtelSpan; } - static getOtelContext() { - return this.otelCtx.length > 0 ? this.otelCtx[this.otelCtx.length-1] : undefined; + static getOtelSpan(): OtelSpan { + return global.otelSpan; } - static removeOtelContext() { - this.otelCtx.pop(); + static removeOtelSpan() { + global.otelSpan = undefined; } }