Skip to content

Commit

Permalink
feat(logrusx): add WithSpanFromContext helper (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-jonas authored May 27, 2024
1 parent d0c9e7a commit b1133d6
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion logrusx/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (l *Logger) WithRequest(r *http.Request) *Logger {
_, _, spanCtx = otelhttptrace.Extract(r.Context(), r, opts)
}
if spanCtx.IsValid() {
traces := map[string]string{}
traces := make(map[string]string, 2)
if spanCtx.HasTraceID() {
traces["trace_id"] = spanCtx.TraceID().String()
}
Expand All @@ -107,6 +107,22 @@ func (l *Logger) WithRequest(r *http.Request) *Logger {
return ll
}

func (l *Logger) WithSpanFromContext(ctx context.Context) *Logger {
spanCtx := trace.SpanContextFromContext(ctx)
if !spanCtx.IsValid() {
return l
}

traces := make(map[string]string, 2)
if spanCtx.HasTraceID() {
traces["trace_id"] = spanCtx.TraceID().String()
}
if spanCtx.HasSpanID() {
traces["span_id"] = spanCtx.SpanID().String()
}
return l.WithField("otel", traces)
}

func (l *Logger) Logf(level logrus.Level, format string, args ...interface{}) {
if !l.leakSensitive {
for i, arg := range args {
Expand Down

0 comments on commit b1133d6

Please sign in to comment.