2024-05-21 05:54:22 +00:00
|
|
|
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(),
|
2024-05-21 06:16:26 +00:00
|
|
|
c.Method()+" "+c.OriginalURL(),
|
2024-05-21 05:54:22 +00:00
|
|
|
opts...,
|
|
|
|
)
|
|
|
|
|
|
|
|
c.SetUserContext(otelCtx)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
err := c.Next()
|
|
|
|
|
|
|
|
statusCode := c.Response().StatusCode()
|
|
|
|
attrs := semconv.HTTPResponseStatusCode(statusCode)
|
|
|
|
|
|
|
|
span.SetAttributes(attrs)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|