feat: add span id

This commit is contained in:
franklin 2023-08-14 08:08:21 +02:00
parent 773e7eea79
commit 3b38ea311b
3 changed files with 9 additions and 3 deletions

View File

@ -5,9 +5,14 @@ import (
)
const (
SpanID = "span_id"
TraceID = "trace_id"
)
func FSpanID(traceID string) zap.Field {
return zap.String(SpanID, traceID)
}
func FTraceID(traceID string) zap.Field {
return zap.String(TraceID, traceID)
}

View File

@ -29,8 +29,8 @@ func WithServiceName(l *zap.Logger, name string) *zap.Logger {
}
func WithTraceID(l *zap.Logger, ctx context.Context) *zap.Logger {
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() {
l = With(l, FTraceID(spanCtx.TraceID().String()))
if spanCtx := trace.SpanContextFromContext(ctx); spanCtx.IsValid() && spanCtx.IsSampled() {
l = With(l, FTraceID(spanCtx.TraceID().String()), FSpanID(spanCtx.SpanID().String()))
}
return l
}

View File

@ -72,8 +72,9 @@ func TelemetryWithOptions(opts TelemetryOptions) Middleware {
}
if labeler, ok := httplog.LabelerFromRequest(r); ok {
if spanCtx := trace.SpanContextFromContext(r.Context()); spanCtx.IsValid() {
if spanCtx := trace.SpanContextFromContext(r.Context()); spanCtx.IsValid() && spanCtx.IsSampled() {
labeler.Add(log.FTraceID(spanCtx.TraceID().String()))
labeler.Add(log.FSpanID(spanCtx.SpanID().String()))
}
}