From 92b813c9ed2932284d89b8727ded80073a1eaa47 Mon Sep 17 00:00:00 2001 From: Jon Lundy Date: Sun, 20 Nov 2022 10:22:15 -0700 Subject: [PATCH] chore: add named span --- internal/lg/tracer.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/lg/tracer.go b/internal/lg/tracer.go index a5c9639..d88ff99 100644 --- a/internal/lg/tracer.go +++ b/internal/lg/tracer.go @@ -44,7 +44,6 @@ func attrs(ctx context.Context) (string, []attribute.KeyValue) { attribute.String("pc", fmt.Sprintf("%v", pc)), attribute.String("file", file), attribute.Int("line", line), - attribute.String("name", name), ) } return name, attrs @@ -52,6 +51,15 @@ func attrs(ctx context.Context) (string, []attribute.KeyValue) { func Span(ctx context.Context, opts ...trace.SpanStartOption) (context.Context, trace.Span) { name, attrs := attrs(ctx) + attrs = append(attrs, attribute.String("name", name)) + ctx, span := Tracer(ctx).Start(ctx, name, opts...) + span.SetAttributes(attrs...) + + return ctx, span +} +func NamedSpan(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { + _, attrs := attrs(ctx) + attrs = append(attrs, attribute.String("name", name)) ctx, span := Tracer(ctx).Start(ctx, name, opts...) span.SetAttributes(attrs...) @@ -151,7 +159,9 @@ func reverse[T any](s []T) { } func Htrace(h http.Handler, name string) http.Handler { - return otelhttp.NewHandler(h, name) + return otelhttp.NewHandler(h, name, otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string { + return fmt.Sprintf("%s: %s", operation, r.RequestURI) + })) } func toContext[K comparable, V any](ctx context.Context, key K, value V) context.Context {