Skip to content

Commit

Permalink
feat: add spanId and traceId to logger if possible
Browse files Browse the repository at this point in the history
Signed-off-by: iverly <github@iverly.net>
  • Loading branch information
iverly committed Nov 28, 2023
1 parent 3e6a3ad commit ac60a49
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@nestjs/platform-express": "^10.2.10",
"@nestjs/platform-fastify": "^10.2.10",
"@nestjs/swagger": "^7.1.16",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/context-async-hooks": "^1.18.1",
"@opentelemetry/core": "^1.18.1",
"@opentelemetry/exporter-jaeger": "^1.18.1",
Expand Down
10 changes: 10 additions & 0 deletions packages/opentelemetry/src/lib/opentelemetry.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigModule } from '@nestjs/config';
import { LoggerModule } from 'nestjs-pino';
import { config } from './opentelemetry.config';
import { OpenTelemetryModule as NestOpenTelemetryModule } from 'nestjs-otel';
import { trace, context } from '@opentelemetry/api';

@Module({})
export class OpenTelemetryModule {
Expand All @@ -22,6 +23,15 @@ export class OpenTelemetryModule {
base: null,
formatters: {
level: (label: string) => ({ level: label }),
log(object) {
const span = trace.getSpan(context.active());
if (!span) {
return { ...object };
}

const { spanId, traceId } = span.spanContext();
return { ...object, spanId, traceId };
},
},
timestamp: () => `,"time":"${new Date().toISOString()}"`,
redact: {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ac60a49

Please sign in to comment.