milvus/pkg/tracer/util.go
congqixia 0c7a96b48d
enhance: Make compaction log has traceID (#30338)
See also #30167

After support open telemetry tracing, we want to have traceID as well,
this PR adds util functions to set traceID with span & propagate traceID
between different context.

---------

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
2024-01-30 10:09:03 +08:00

28 lines
807 B
Go

package tracer
import (
"context"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/milvus-io/milvus/pkg/log"
)
// SetupSpan add span into ctx values.
// Also setup logger in context with tracerID field.
func SetupSpan(ctx context.Context, span trace.Span) context.Context {
ctx = trace.ContextWithSpan(ctx, span)
ctx = log.WithFields(ctx, zap.Stringer("traceID", span.SpanContext().TraceID()))
return ctx
}
// Propagate passes span context into a new ctx with different lifetime.
// Also setup logger in new context with traceID field.
func Propagate(ctx, newRoot context.Context) context.Context {
spanCtx := trace.SpanContextFromContext(ctx)
newCtx := trace.ContextWithSpanContext(newRoot, spanCtx)
return log.WithFields(newCtx, zap.Stringer("traceID", spanCtx.TraceID()))
}