go-api/tracing.go
Snider 8ba1716215 feat: add WithTracing OpenTelemetry distributed tracing middleware
Adds WithTracing(serviceName) option using the official otelgin
instrumentation (go.opentelemetry.io/contrib/.../otelgin v0.65.0).
Each request produces a span with http.request.method, http.route,
and http.response.status_code attributes. Trace context is propagated
via W3C traceparent headers.

Also exposes NewTracerProvider() convenience helper for wiring up
a synchronous TracerProvider in tests and simple deployments.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-21 00:24:46 +00:00

47 lines
1.8 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package api
import (
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)
// WithTracing adds OpenTelemetry distributed tracing middleware via otelgin.
// Each incoming request produces a span tagged with the HTTP method, route,
// and status code. Trace context is propagated using W3C traceparent headers.
//
// The serviceName identifies this service in distributed traces. If a
// TracerProvider has not been configured globally via otel.SetTracerProvider,
// the middleware uses the global default (which is a no-op until set).
//
// Typical setup in main:
//
// tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
// otel.SetTracerProvider(tp)
// otel.SetTextMapPropagator(propagation.TraceContext{})
//
// engine, _ := api.New(api.WithTracing("my-service"))
func WithTracing(serviceName string, opts ...otelgin.Option) Option {
return func(e *Engine) {
e.middlewares = append(e.middlewares, otelgin.Middleware(serviceName, opts...))
}
}
// NewTracerProvider creates a TracerProvider configured with the given
// SpanExporter and returns it. The caller is responsible for calling
// Shutdown on the returned provider when the application exits.
//
// This is a convenience helper for tests and simple deployments.
// Production setups should build their own TracerProvider with batching,
// resource attributes, and appropriate exporters.
func NewTracerProvider(exporter sdktrace.SpanExporter) *sdktrace.TracerProvider {
tp := sdktrace.NewTracerProvider(
sdktrace.WithSyncer(exporter),
)
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(propagation.TraceContext{})
return tp
}