Otel Middleware
📖 Official Go Fiber Documentation: https://docs.gofiber.io/contrib/otel/
OpenTelemetry instrumentation middleware adapted from github.com/gofiber/contrib/v3/otel.
Installation
go get github.com/BeamFoundry/rays-fiber/pkg/middleware/contrib/otelProgrammatic Configuration Interfaces
If your Controller or FiberPathFilter implements any of the following methods, the Otel middleware will automatically bind them:
OtelNext() func(fiber.Ctx) boolOtelTracerProvider() oteltrace.TracerProviderOtelMeterProvider() otelmetric.MeterProviderOtelPropagators() propagation.TextMapPropagatorOtelResponsePropagators() propagation.TextMapPropagatorOtelPublicEndpointFn() func(fiber.Ctx) boolOtelSpanStartOptions() []oteltrace.SpanStartOptionOtelSpanNameFormatter() func(fiber.Ctx) stringOtelCustomAttributes() func(fiber.Ctx) []attribute.KeyValueOtelCustomResponseAttributes() func(fiber.Ctx) []attribute.KeyValueOtelCustomMetricAttributes() func(fiber.Ctx) []attribute.KeyValueOtelCustomResponseMetricAttributes() func(fiber.Ctx) []attribute.KeyValueOtelOptions(options []otel.Option) []otel.Option
Example: Configuring Otel
Here is an example of a controller embedding the middleware and implementing the OtelOptions interface to customize the underlying otel.Option slice:
package controllers
import (
"github.com/gofiber/contrib/v3/otel"
. "github.com/BeamFoundry/rays-fiber/pkg/core"
. "github.com/BeamFoundry/rays/pkg/core"
. "github.com/BeamFoundry/rays/pkg/web"
. "github.com/BeamFoundry/rays-fiber/pkg/middleware/contrib/otel"
)
type MyOtelController struct {
FiberController
RequestMapping `Path:"/example"`
// 1. Embed the middleware
Otel
}
// 2. Implement the configuration interface
func (this *MyOtelController) OtelOptions(options []otel.Option) []otel.Option {
// Append or override otel.Option values for this controller
return options
}
var _, _ = any(&MyOtelController{}).(Stereotype)Default YAML Configuration
The framework applies these default settings to the "primary" server. To override them, copy the properties you want to change into your application.yaml under rays.fiber.servers.primary.otel.
rays:
fiber:
servers:
primary:
otel:
withoutMetrics: false
traceResponseHeader: ""
publicEndpoint: false
port: null
clientIP: true
capturedRequestHeaders: []
capturedResponseHeaders: []
redactedQueryParams: []Integration with rays-otel & SecurityFilterChain
- Container Provider Injection: When
github.com/BeamFoundry/rays-otel(or customTracerProvider,MeterProvider, orTextMapPropagatorBeams) is registered in the container,Otelautomatically injects them viaOptional[T]before applying controller/filter interface overrides (OtelTracerProvider,OtelMeterProvider,OtelPropagators,OtelOptions). TransactionManagerContext Binding:rays-fiberautomatically bindsc.Context()ontoTransactionManager.SetContext(...)beforeSecurityFilterChainand before controller handler execution, so downstreamrays-orm,rays-data-mongo,rays-web-security-oauth2, andLoggingcalls inherit the active HTTP span.- Middleware Ordering:
SecurityFilterChainruns afterFiberPathFilters and beforeFiberControllermiddlewares. To include authentication/authorization inside the HTTP server span, embedOtelin a globalFiberPathFilter:
type GlobalOtelFilter struct {
FiberPathFilter
Otel
}
var _, _ = any(&GlobalOtelFilter{}).(Stereotype)