observer/pkg/tracer/middleware.go

59 lines
1.5 KiB
Go
Raw Normal View History

2024-04-26 13:09:54 +00:00
package tracer
import (
"context"
2024-04-30 10:41:05 +00:00
"fmt"
2024-04-26 13:09:54 +00:00
"github.com/gofiber/fiber/v2"
fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel"
"go.opentelemetry.io/otel/attribute"
trace "go.opentelemetry.io/otel/trace"
"runtime"
2024-04-26 13:09:54 +00:00
)
func Handler(fc *fiber.Ctx) (context.Context, trace.Span) {
2024-05-13 11:36:45 +00:00
spanName := fmt.Sprint(fc.OriginalURL())
simpleCtx, span := fiberOpentelemetry.Tracer.Start(fc.UserContext(), spanName)
2024-04-26 13:09:54 +00:00
fc.SetUserContext(simpleCtx)
2024-04-30 10:41:05 +00:00
_, file, line, _ := runtime.Caller(1)
span.SetAttributes(
attribute.String("service.layer", "handler"),
attribute.String("file", file),
attribute.String("line", fmt.Sprintf("%d", line)),
)
return simpleCtx, span
}
2024-05-13 11:36:45 +00:00
func Service(c context.Context, spanName string) (context.Context, trace.Span) {
2024-04-30 10:41:05 +00:00
simpleCtx, span := fiberOpentelemetry.Tracer.Start(c, spanName)
var attribs []attribute.KeyValue
_, file, line, _ := runtime.Caller(1)
attribs = append(
attribs,
attribute.String("service.layer", "service"),
attribute.String("file", file),
attribute.String("line", fmt.Sprintf("%d", line)),
)
span.SetAttributes(attribs...)
2024-04-26 13:09:54 +00:00
return simpleCtx, span
}
2024-04-30 10:41:05 +00:00
2024-05-13 11:36:45 +00:00
func Repository(c context.Context, spanName string) (context.Context, trace.Span) {
2024-04-30 10:41:05 +00:00
ctx2, span := fiberOpentelemetry.Tracer.Start(c, spanName)
var attribs []attribute.KeyValue
_, file, line, _ := runtime.Caller(1)
attribs = append(attribs,
attribute.String("file", file),
attribute.String("line", fmt.Sprintf("%d", line)),
)
span.SetAttributes(attribs...)
return ctx2, span
}