diff --git a/example/requests.hurl b/example/requests.hurl index 9d4d059..8610182 100644 --- a/example/requests.hurl +++ b/example/requests.hurl @@ -1,5 +1,5 @@ GET http://127.0.0.1:3344/ HTTP 500 -GET http://127.0.0.1:3344/just/some/more/complex/path/:with/params +GET http://127.0.0.1:3344/just/some/more/complex/path/jjj/params HTTP 500 diff --git a/pkg/fiber_tracing/fiber_tracing.go b/pkg/fiber_tracing/fiber_tracing.go index 375865f..57ba08c 100644 --- a/pkg/fiber_tracing/fiber_tracing.go +++ b/pkg/fiber_tracing/fiber_tracing.go @@ -6,7 +6,6 @@ import ( "git.ma-al.com/maal-libraries/observer/pkg/exporters" "github.com/gofiber/fiber/v2" - fiberOpentelemetry "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/sdk/resource" @@ -25,7 +24,8 @@ type Config struct { Version string // Name of an organization providing the service ServiceProvider string - Exporters []exporters.TraceExporter + + Exporters []exporters.TraceExporter } func newResource(config Config) *resource.Resource { @@ -40,22 +40,23 @@ func newResource(config Config) *resource.Resource { // NOTE: You can use [trace.WithAttributes] as a parameter to opts argument func Start(ctx context.Context, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { - return fiberOpentelemetry.Tracer.Start(ctx, spanName, opts...) + return Tracer.Start(ctx, spanName, opts...) } // NOTE: You can use [trace.WithAttributes] as a parameter to opts argument // Returns [c.UserContext] as [context.Context] func FStart(c *fiber.Ctx, opts ...trace.SpanStartOption) (context.Context, trace.Span) { - span := trace.SpanFromContext(fiberOpentelemetry.FromCtx(c)) - c.SetUserContext(trace.ContextWithSpan(c.UserContext(), span)) - return fiberOpentelemetry.Tracer.Start(c.UserContext(), c.Method()+" "+c.Route().Path, opts...) + return Tracer.Start(c.UserContext(), c.Method()+" "+c.Route().Path, opts...) +} + +// Just like [FStart] but makes it possible to assign custom span name. +func FStartRenamed(c *fiber.Ctx, spanName string, opts ...trace.SpanStartOption) (context.Context, trace.Span) { + return Tracer.Start(c.UserContext(), spanName, opts...) } // Retrieve span using [fiber.Ctx] func SpanFromContext(c *fiber.Ctx) trace.Span { - ctx := fiberOpentelemetry.FromCtx(c) - c.SetUserContext(trace.ContextWithSpan(c.UserContext(), trace.SpanFromContext(ctx))) - return trace.SpanFromContext(ctx) + return trace.SpanFromContext(c.UserContext()) } func NewMiddleware(config Config) func(*fiber.Ctx) error { @@ -78,10 +79,9 @@ func NewMiddleware(config Config) func(*fiber.Ctx) error { tracer := TP.Tracer("_maal-fiber-otel_") - return fiberOpentelemetry.New( - fiberOpentelemetry.Config{ - Tracer: tracer, - SpanName: "{{ .Method }} {{ .Route.Path }}", + return new( + middlewareConfig{ + Tracer: tracer, TracerStartAttributes: []trace.SpanStartOption{ trace.WithSpanKind(trace.SpanKindServer), trace.WithNewRoot(), diff --git a/pkg/fiber_tracing/middleware.go b/pkg/fiber_tracing/middleware.go new file mode 100644 index 0000000..3f9e73b --- /dev/null +++ b/pkg/fiber_tracing/middleware.go @@ -0,0 +1,90 @@ +package fiber_tracing + +// This was copied from "github.com/psmarcin/fiber-opentelemetry/pkg/fiber-otel" +// and slighltly modified but this piece of code is yet to be fully integrated +// into the package. + +import ( + "github.com/gofiber/fiber/v2" + "go.opentelemetry.io/otel" + semconv "go.opentelemetry.io/otel/semconv/v1.25.0" + "go.opentelemetry.io/otel/trace" +) + +var Tracer = otel.Tracer("fiber_tracing_middleware") + +// Config defines the config for middleware. +type middlewareConfig struct { + Tracer trace.Tracer + TracerStartAttributes []trace.SpanStartOption +} + +// ConfigDefault is the default config +var configDefault = middlewareConfig{ + Tracer: Tracer, + TracerStartAttributes: []trace.SpanStartOption{ + trace.WithNewRoot(), + }, +} + +// Helper function to set default values +func configDefaults(config ...middlewareConfig) middlewareConfig { + // Return default config if nothing provided + if len(config) < 1 { + return configDefault + } + + // Override default config + cfg := config[0] + + if len(cfg.TracerStartAttributes) == 0 { + cfg.TracerStartAttributes = configDefault.TracerStartAttributes + } + + return cfg +} + +func new(config ...middlewareConfig) fiber.Handler { + // Set default config + cfg := configDefaults(config...) + + // Return new handler + return func(c *fiber.Ctx) error { + spanStartAttributes := trace.WithAttributes( + semconv.HTTPMethod(c.Method()), + semconv.HTTPTarget(string(c.Request().RequestURI())), + semconv.HTTPRoute(c.Route().Path), + semconv.HTTPURL(c.OriginalURL()), + semconv.HostIP(c.IP()), + semconv.HTTPUserAgent(string(c.Request().Header.UserAgent())), + semconv.HTTPRequestContentLength(c.Request().Header.ContentLength()), + semconv.HTTPScheme(c.Protocol()), + semconv.NetTransportTCP, + ) + spanKind := trace.WithSpanKind(trace.SpanKindServer) + + opts := []trace.SpanStartOption{ + spanStartAttributes, + spanKind, + } + opts = append(opts, cfg.TracerStartAttributes...) + + otelCtx, span := Tracer.Start( + c.UserContext(), + c.Method()+" "+c.Route().Path, + opts..., + ) + + c.SetUserContext(otelCtx) + defer span.End() + + err := c.Next() + + statusCode := c.Response().StatusCode() + attrs := semconv.HTTPResponseStatusCode(statusCode) + + span.SetAttributes(attrs) + + return err + } +}