mirror of
https://gitee.com/milvus-io/milvus.git
synced 2024-11-30 10:59:32 +08:00
0c7a96b48d
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>
28 lines
807 B
Go
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()))
|
|
}
|