From 992fcb7940e7549e982a21132f7bd059992c1abc Mon Sep 17 00:00:00 2001 From: Filip Gora Date: Mon, 13 May 2024 13:36:45 +0200 Subject: [PATCH] fix invalid span name --- example/main.go | 4 ++-- go.mod | 3 ++- go.sum | 2 ++ pkg/tracer/middleware.go | 10 +++++----- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/example/main.go b/example/main.go index 8ec9706..532431e 100644 --- a/example/main.go +++ b/example/main.go @@ -70,7 +70,7 @@ func main() { } func Serv(ctx context.Context) *fiber.Error { - ctx, span := tracer.Service(ctx, "serv", "name of the span") + ctx, span := tracer.Service(ctx, "name of the span") defer span.End() for range []int{1, 2, 3} { @@ -86,7 +86,7 @@ func Serv(ctx context.Context) *fiber.Error { } func Repo(ctx context.Context) error { - ctx, span := tracer.Repository(ctx, "serv", "name of the span") + ctx, span := tracer.Repository(ctx, "name of the span") defer span.End() for range []int{1, 2, 3} { diff --git a/go.mod b/go.mod index 5ca6798..a2f42ac 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ -module git.ma-al.com/gora_filip/observer +module git.ma-al.com/gora_filip go 1.21.0 require ( + git.ma-al.com/gora_filip/observer v0.0.0-20240430124205-be03e0ce4205 github.com/gofiber/fiber/v2 v2.52.4 github.com/psmarcin/fiber-opentelemetry v1.2.0 go.opentelemetry.io/otel v1.26.0 diff --git a/go.sum b/go.sum index a9d2022..2ab7a31 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +git.ma-al.com/gora_filip/observer v0.0.0-20240430124205-be03e0ce4205 h1:tuJ7e4EAWx/IbVESEO6l3yNoGiUAoaXWssMc26Ft3Hs= +git.ma-al.com/gora_filip/observer v0.0.0-20240430124205-be03e0ce4205/go.mod h1:NLwhsfm3SE3YwR+3z4DbH82OBjRPcE+M/GPGEc7DPUM= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= diff --git a/pkg/tracer/middleware.go b/pkg/tracer/middleware.go index d246cb2..07efa4a 100644 --- a/pkg/tracer/middleware.go +++ b/pkg/tracer/middleware.go @@ -155,7 +155,9 @@ func ShutdownTracer() { } func Handler(fc *fiber.Ctx) (context.Context, trace.Span) { - simpleCtx, span := fiberOpentelemetry.Tracer.Start(fc.UserContext(), fc.OriginalURL()) + spanName := fmt.Sprint(fc.OriginalURL()) + simpleCtx, span := fiberOpentelemetry.Tracer.Start(fc.UserContext(), spanName) + fc.SetUserContext(simpleCtx) _, file, line, _ := runtime.Caller(1) @@ -168,14 +170,13 @@ func Handler(fc *fiber.Ctx) (context.Context, trace.Span) { return simpleCtx, span } -func Service(c context.Context, serviceName string, spanName string) (context.Context, trace.Span) { +func Service(c context.Context, spanName string) (context.Context, trace.Span) { simpleCtx, span := fiberOpentelemetry.Tracer.Start(c, spanName) var attribs []attribute.KeyValue _, file, line, _ := runtime.Caller(1) attribs = append( attribs, - attribute.String("service.name", serviceName), attribute.String("service.layer", "service"), attribute.String("file", file), attribute.String("line", fmt.Sprintf("%d", line)), @@ -186,13 +187,12 @@ func Service(c context.Context, serviceName string, spanName string) (context.Co return simpleCtx, span } -func Repository(c context.Context, repositoryName string, spanName string) (context.Context, trace.Span) { +func Repository(c context.Context, spanName string) (context.Context, trace.Span) { ctx2, span := fiberOpentelemetry.Tracer.Start(c, spanName) var attribs []attribute.KeyValue _, file, line, _ := runtime.Caller(1) attribs = append(attribs, - attribute.String("service.repository", repositoryName), attribute.String("file", file), attribute.String("line", fmt.Sprintf("%d", line)), )