fix invalid span name

This commit is contained in:
Filip Góra 2024-05-13 13:36:45 +02:00
parent be03e0ce42
commit 992fcb7940
4 changed files with 11 additions and 8 deletions

View File

@ -70,7 +70,7 @@ func main() {
} }
func Serv(ctx context.Context) *fiber.Error { 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() defer span.End()
for range []int{1, 2, 3} { for range []int{1, 2, 3} {
@ -86,7 +86,7 @@ func Serv(ctx context.Context) *fiber.Error {
} }
func Repo(ctx context.Context) 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() defer span.End()
for range []int{1, 2, 3} { for range []int{1, 2, 3} {

3
go.mod
View File

@ -1,8 +1,9 @@
module git.ma-al.com/gora_filip/observer module git.ma-al.com/gora_filip
go 1.21.0 go 1.21.0
require ( require (
git.ma-al.com/gora_filip/observer v0.0.0-20240430124205-be03e0ce4205
github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/fiber/v2 v2.52.4
github.com/psmarcin/fiber-opentelemetry v1.2.0 github.com/psmarcin/fiber-opentelemetry v1.2.0
go.opentelemetry.io/otel v1.26.0 go.opentelemetry.io/otel v1.26.0

2
go.sum
View File

@ -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.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=

View File

@ -155,7 +155,9 @@ func ShutdownTracer() {
} }
func Handler(fc *fiber.Ctx) (context.Context, trace.Span) { 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) fc.SetUserContext(simpleCtx)
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
@ -168,14 +170,13 @@ func Handler(fc *fiber.Ctx) (context.Context, trace.Span) {
return simpleCtx, 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) simpleCtx, span := fiberOpentelemetry.Tracer.Start(c, spanName)
var attribs []attribute.KeyValue var attribs []attribute.KeyValue
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
attribs = append( attribs = append(
attribs, attribs,
attribute.String("service.name", serviceName),
attribute.String("service.layer", "service"), attribute.String("service.layer", "service"),
attribute.String("file", file), attribute.String("file", file),
attribute.String("line", fmt.Sprintf("%d", line)), 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 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) ctx2, span := fiberOpentelemetry.Tracer.Start(c, spanName)
var attribs []attribute.KeyValue var attribs []attribute.KeyValue
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
attribs = append(attribs, attribs = append(attribs,
attribute.String("service.repository", repositoryName),
attribute.String("file", file), attribute.String("file", file),
attribute.String("line", fmt.Sprintf("%d", line)), attribute.String("line", fmt.Sprintf("%d", line)),
) )