fix(api): send trace context to sentry

This helps associating errors in sentry with the full trace context
This commit is contained in:
Julian Tölle 2022-06-23 18:24:56 +02:00
parent 3528a6d205
commit 8d9e99039c

View file

@ -10,15 +10,14 @@ import { RavenInterceptor } from "nest-raven";
import Pino from "pino"; import Pino from "pino";
import { AppModule } from "./app.module"; import { AppModule } from "./app.module";
import { Logger } from "nestjs-pino"; import { Logger } from "nestjs-pino";
import { Scope } from "@sentry/node";
const logger = Pino({ const logger = Pino({
formatters: { formatters: {
log(object) { log(object) {
const span = trace.getSpan(context.active()); const span = trace.getSpan(context.active());
if (!span) return { ...object }; if (!span) return { ...object };
const { spanId, traceId } = trace const { spanId, traceId } = span.spanContext();
.getSpan(context.active())
?.spanContext();
return { ...object, spanId, traceId }; return { ...object, spanId, traceId };
}, },
}, },
@ -39,7 +38,19 @@ function setupSentry(
], ],
}); });
app.useGlobalInterceptors(new RavenInterceptor()); app.useGlobalInterceptors(
new RavenInterceptor({
transformers: [
(scope: Scope) => {
const span = trace.getSpan(context.active());
if (span) {
const { spanId, traceId } = span.spanContext();
scope.setContext("trace", { span_id: spanId, trace_id: traceId });
}
},
],
})
);
} }
async function bootstrap() { async function bootstrap() {