fix: fixed the bug that old trace instances may be fetched

This commit is contained in:
chenquan
2023-02-22 17:21:58 +08:00
committed by Kevin Wan
parent 133c40ac1c
commit 3bc40d9eaf
6 changed files with 77 additions and 12 deletions

View File

@@ -6,8 +6,10 @@ import (
"strings"
ztrace "github.com/zeromicro/go-zero/internal/trace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"google.golang.org/grpc/peer"
)
@@ -75,3 +77,14 @@ func PeerAttr(addr string) []attribute.KeyValue {
semconv.NetPeerPortKey.String(port),
}
}
// TracerFromContext returns a tracer in ctx, otherwise returns a global tracer.
func TracerFromContext(ctx context.Context) (tracer trace.Tracer) {
if span := trace.SpanFromContext(ctx); span.SpanContext().IsValid() {
tracer = span.TracerProvider().Tracer(TraceName)
} else {
tracer = otel.Tracer(TraceName)
}
return
}