mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-28 09:42:05 +01:00
Merge pull request #15790 from stonezdj/21oct15_change_trace_name
Change the span name to <method>_<request uri>
This commit is contained in:
commit
2020bf383b
@ -16,6 +16,7 @@ package http
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"github.com/goharbor/harbor/src/lib/trace"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@ -45,8 +46,8 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddTracingWithGlobalTransport() {
|
func AddTracingWithGlobalTransport() {
|
||||||
insecureHTTPTransport = otelhttp.NewTransport(insecureHTTPTransport)
|
insecureHTTPTransport = otelhttp.NewTransport(insecureHTTPTransport, trace.HarborHTTPTraceOptions...)
|
||||||
secureHTTPTransport = otelhttp.NewTransport(secureHTTPTransport)
|
secureHTTPTransport = otelhttp.NewTransport(secureHTTPTransport, trace.HarborHTTPTraceOptions...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use this instead of Default Transport in library because it sets ForceAttemptHTTP2 to true
|
// Use this instead of Default Transport in library because it sets ForceAttemptHTTP2 to true
|
||||||
|
@ -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)
|
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 {
|
func SpanFromHTTPRequest(req *http.Request) oteltrace.Span {
|
||||||
ctx := req.Context()
|
ctx := req.Context()
|
||||||
return oteltrace.SpanFromContext(ctx)
|
return oteltrace.SpanFromContext(ctx)
|
||||||
@ -49,16 +49,27 @@ func RecordError(span oteltrace.Span, err error, description string) {
|
|||||||
span.SetStatus(codes.Error, description)
|
span.SetStatus(codes.Error, description)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler returns a handler that wraps the given handler with tracing.
|
// HarborSpanNameFormatter common span name formatter in Harbor
|
||||||
func NewHandler(h http.Handler, operation string) http.Handler {
|
func HarborSpanNameFormatter(operation string, r *http.Request) string {
|
||||||
httpOptions := []otelhttp.Option{
|
if len(r.URL.Path) != 0 {
|
||||||
otelhttp.WithTracerProvider(otel.GetTracerProvider()),
|
return r.Method + "_" + r.URL.Path
|
||||||
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
|
|
||||||
}
|
}
|
||||||
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) {
|
func StartTrace(ctx context.Context, tracerName string, spanName string, opts ...oteltrace.SpanStartOption) (context.Context, oteltrace.Span) {
|
||||||
return otel.Tracer(tracerName).Start(ctx, spanName, opts...)
|
return otel.Tracer(tracerName).Start(ctx, spanName, opts...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user