adjust tracing middleware for practical use

This commit is contained in:
Natalia Goc 2024-05-27 07:37:54 +02:00
parent d7b45a1439
commit 3024e888c7
2 changed files with 8 additions and 9 deletions

View File

@ -24,8 +24,7 @@ type Config struct {
Version string Version string
// Name of an organization providing the service // Name of an organization providing the service
ServiceProvider string ServiceProvider string
Exporters []exporters.TraceExporter
Exporters []exporters.TraceExporter
} }
func newResource(config Config) *resource.Resource { func newResource(config Config) *resource.Resource {

View File

@ -5,6 +5,7 @@ package fiber_tracing
// into the package. // into the package.
import ( import (
"git.ma-al.com/maal-libraries/observer/pkg/attr"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"go.opentelemetry.io/otel" "go.opentelemetry.io/otel"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0" semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
@ -50,28 +51,27 @@ func new(config ...middlewareConfig) fiber.Handler {
// Return new handler // Return new handler
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
spanStartAttributes := trace.WithAttributes( spanStartAttributes := []attr.KeyValue{
semconv.HTTPMethod(c.Method()), semconv.HTTPMethod(c.Method()),
semconv.HTTPTarget(string(c.Request().RequestURI())), semconv.HTTPTarget(string(c.Request().RequestURI())),
semconv.HTTPRoute(c.Route().Path), semconv.HTTPRoute(c.Route().Path),
semconv.HTTPURL(c.OriginalURL()), semconv.HTTPURL(c.OriginalURL()),
semconv.HostIP(c.IP()),
semconv.HTTPUserAgent(string(c.Request().Header.UserAgent())), semconv.HTTPUserAgent(string(c.Request().Header.UserAgent())),
semconv.HTTPRequestContentLength(c.Request().Header.ContentLength()), semconv.HTTPRequestContentLength(c.Request().Header.ContentLength()),
semconv.HTTPScheme(c.Protocol()), semconv.HTTPScheme(c.Protocol()),
semconv.NetTransportTCP, semconv.NetTransportTCP,
) }
spanKind := trace.WithSpanKind(trace.SpanKindServer) spanStartAttributes = append(spanStartAttributes, attr.ProcessStart()...)
opts := []trace.SpanStartOption{ opts := []trace.SpanStartOption{
spanStartAttributes, trace.WithAttributes(spanStartAttributes...),
spanKind, trace.WithSpanKind(trace.SpanKindServer),
} }
opts = append(opts, cfg.TracerStartAttributes...) opts = append(opts, cfg.TracerStartAttributes...)
otelCtx, span := Tracer.Start( otelCtx, span := Tracer.Start(
c.UserContext(), c.UserContext(),
c.Method()+" "+c.Route().Path, c.Method()+" "+c.OriginalURL(),
opts..., opts...,
) )