From 8d9e99039c40d8b447315701f4d04a229db254b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Thu, 23 Jun 2022 18:24:56 +0200 Subject: [PATCH] fix(api): send trace context to sentry This helps associating errors in sentry with the full trace context --- src/main.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index cf7f8f0..bf92554 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,15 +10,14 @@ import { RavenInterceptor } from "nest-raven"; import Pino from "pino"; import { AppModule } from "./app.module"; import { Logger } from "nestjs-pino"; +import { Scope } from "@sentry/node"; const logger = Pino({ formatters: { log(object) { const span = trace.getSpan(context.active()); if (!span) return { ...object }; - const { spanId, traceId } = trace - .getSpan(context.active()) - ?.spanContext(); + const { spanId, traceId } = span.spanContext(); 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() {