From 3b2ac066390f6e55d2601c10f23d9d2e6c1988f2 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Wed, 13 Oct 2021 17:25:17 +0800 Subject: [PATCH] Change the span name to _ Use the same trace option for HTTPTransport fixes #15726 Signed-off-by: stonezdj --- src/common/http/transport.go | 5 +++-- src/lib/trace/helper.go | 27 +++++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/common/http/transport.go b/src/common/http/transport.go index 381aff4e6..f6a60a58f 100644 --- a/src/common/http/transport.go +++ b/src/common/http/transport.go @@ -16,6 +16,7 @@ package http import ( "crypto/tls" + "github.com/goharbor/harbor/src/lib/trace" "net" "net/http" "time" @@ -45,8 +46,8 @@ func init() { } func AddTracingWithGlobalTransport() { - insecureHTTPTransport = otelhttp.NewTransport(insecureHTTPTransport) - secureHTTPTransport = otelhttp.NewTransport(secureHTTPTransport) + insecureHTTPTransport = otelhttp.NewTransport(insecureHTTPTransport, trace.HarborHTTPTraceOptions...) + secureHTTPTransport = otelhttp.NewTransport(secureHTTPTransport, trace.HarborHTTPTraceOptions...) } // Use this instead of Default Transport in library because it sets ForceAttemptHTTP2 to true diff --git a/src/lib/trace/helper.go b/src/lib/trace/helper.go index b8d2df045..bd947d490 100644 --- a/src/lib/trace/helper.go +++ b/src/lib/trace/helper.go @@ -34,7 +34,7 @@ func StartSpan(ctx context.Context, name string) (context.Context, oteltrace.Spa return otel.Tracer("goharbor/harbor/src/lib/trace").Start(ctx, name) } -// SpanFromContext returns the span from the context. +// SpanFromHTTPRequest returns the span from the context. func SpanFromHTTPRequest(req *http.Request) oteltrace.Span { ctx := req.Context() return oteltrace.SpanFromContext(ctx) @@ -49,16 +49,27 @@ func RecordError(span oteltrace.Span, err error, description string) { span.SetStatus(codes.Error, description) } -// NewHandler returns a handler that wraps the given handler with tracing. -func NewHandler(h http.Handler, operation string) http.Handler { - httpOptions := []otelhttp.Option{ - otelhttp.WithTracerProvider(otel.GetTracerProvider()), - otelhttp.WithPropagators(otel.GetTextMapPropagator()), +// HarborSpanNameFormatter common span name formatter in Harbor +func HarborSpanNameFormatter(operation string, r *http.Request) string { + if len(r.URL.Path) != 0 { + return r.Method + "_" + r.URL.Path } - return otelhttp.NewHandler(h, operation, httpOptions...) + return operation } -// StarTrace returns a new span with the given name. +// HarborHTTPTraceOptions common trace options +var HarborHTTPTraceOptions = []otelhttp.Option{ + otelhttp.WithTracerProvider(otel.GetTracerProvider()), + otelhttp.WithPropagators(otel.GetTextMapPropagator()), + otelhttp.WithSpanNameFormatter(HarborSpanNameFormatter), +} + +// NewHandler returns a handler that wraps the given handler with tracing. +func NewHandler(h http.Handler, operation string) http.Handler { + return otelhttp.NewHandler(h, operation, HarborHTTPTraceOptions...) +} + +// StartTrace returns a new span with the given name. func StartTrace(ctx context.Context, tracerName string, spanName string, opts ...oteltrace.SpanStartOption) (context.Context, oteltrace.Span) { return otel.Tracer(tracerName).Start(ctx, spanName, opts...) }