From 67228b763f8ad5a2653d1a3e4958594ab732b0ca Mon Sep 17 00:00:00 2001 From: Ivan Santos Date: Sat, 11 Dec 2021 17:55:01 -0600 Subject: [PATCH] chore: update documentation for logger with instrumentation fixes #60 --- README.md | 18 ++++++++++++++++-- src/tracing/trace.service.ts | 7 +++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d36a72d..9fdc679 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ For questions and support please use the official [Discord channel](https://disc ## Why -Setting up observability metrics with nestjs requires multiple libraries and patterns. OpenTelemetry has support for multiple exporters and types of metrics such as Prometheus Metrics. +Setting up observability metrics with nestjs requires multiple libraries and patterns. OpenTelemetry has support for multiple exporters and types of metrics such as Prometheus Metrics. ## Observability @@ -288,7 +288,21 @@ When `metricExporter` is defined in otel SDK with a `PrometheusExporter`it will ## Using with a logger -### Pino +### Pino with instrumentation + +This approach uses otel instrumentation to automatically inject spanId and traceId. + +```ts +import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino'; + +const otelSDK = new NodeSDK({ + instrumentations: [new PinoInstrumentation()], +}); +``` + +### Pino with custom formatter + +This approach uses the global trace context for injecting SpanId and traceId as a property of your structured log. ```ts import Pino, { Logger } from 'pino'; diff --git a/src/tracing/trace.service.ts b/src/tracing/trace.service.ts index 671da85..3e0c757 100644 --- a/src/tracing/trace.service.ts +++ b/src/tracing/trace.service.ts @@ -3,12 +3,15 @@ import { Injectable } from '@nestjs/common'; @Injectable() export class TraceService { + public getTracer() { + return trace.getTracer('default'); + } + public getSpan(): Span { return trace.getSpan(context.active()); } public startSpan(name: string): Span { - const tracer = trace.getTracer('default'); - return tracer.startSpan(name); + return this.getTracer().startSpan(name); } }