Skip to content

Commit

Permalink
fix: enable log-trace correlation in datadog (#2203)
Browse files Browse the repository at this point in the history
This enables correlation of logs and traces in datadog.

Note that both logs and traces must be enabled for this to work - in
datadog and kuberpult.

Ref: SRX-42QN2C
  • Loading branch information
sven-urbanski-freiheit-com authored Jan 17, 2025
1 parent d308914 commit 673af89
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package logger
import (
"context"
"fmt"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"os"

"github.com/blendle/zapdriver"
Expand All @@ -35,7 +36,21 @@ import (
)

func FromContext(ctx context.Context) *zap.Logger {
return ctxzap.Extract(ctx)
l := ctxzap.Extract(ctx)
span, ok := tracer.SpanFromContext(ctx)
if ok {
env := os.Getenv("DD_ENV")
service := os.Getenv("DD_SERVICE")
version := os.Getenv("DD_VERSION")
return l.With(
zap.Uint64("dd.trace_id", span.Context().TraceID()),
zap.Uint64("dd.span_id", span.Context().SpanID()),
zap.String("dd.env", env),
zap.String("dd.service", service),
zap.String("dd.version", version),
)
}
return l
}

func WithLogger(ctx context.Context, logger *zap.Logger) context.Context {
Expand Down

0 comments on commit 673af89

Please sign in to comment.