Skip to content

Commit

Permalink
fix otel inner spans
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Dec 27, 2023
1 parent 461f331 commit cd750dd
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -37,7 +35,6 @@ class Span {
try {
this.span?.finish();
this.otelSpan?.end();
Telemetry.removeOtelContext();
} catch (error) {
debug(`Sentry finish error. ${reassurance}`);
debug(error);
Expand All @@ -46,8 +43,6 @@ class Span {
}

export class Telemetry {
static otelCtx: OtelContext[] = [];

static startSpan(data: unknown) {
return new Span(data);
}
Expand All @@ -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;
}
}

0 comments on commit cd750dd

Please sign in to comment.