Documentation
¶
Overview ¶
Package otel bootstraps OpenTelemetry tracing and provides helpers to inject a trace id into the context (and therefore into logs), open child spans, and propagate trace context across service boundaries.
Index ¶
- func AddSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func ExtractFromRequest(ctx context.Context, r *http.Request) context.Context
- func GetTraceID(ctx context.Context) string
- func InitTracing(ctx context.Context, cfg Config) (trace.Tracer, func(context.Context) error, error)
- func InjectToRequest(ctx context.Context, r *http.Request)
- func InjectTracing(ctx context.Context, tracer trace.Tracer) context.Context
- type Config
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSpan ¶
func AddSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
AddSpan starts a child span on the tracer stored in ctx. If no tracer is present it returns ctx unchanged and a no-op span, so callers never need a nil check.
func ExtractFromRequest ¶
ExtractFromRequest returns a context seeded with the trace context carried by an incoming request's headers.
func GetTraceID ¶
GetTraceID returns the trace id stored in ctx, or an empty string if none. It is designed to be passed as logger.TraceIDFn so every log line carries the active trace id.
func InitTracing ¶
func InitTracing(ctx context.Context, cfg Config) (trace.Tracer, func(context.Context) error, error)
InitTracing configures a global TracerProvider exporting via OTLP/gRPC and returns a Tracer plus a shutdown function. The shutdown flushes pending spans.
func InjectToRequest ¶
InjectToRequest writes the current trace context into an outgoing request's headers so the callee can continue the trace.
func InjectTracing ¶
InjectTracing stores the tracer in ctx and ensures a trace id is available (generating one when there is no active span), so downstream logging can pick it up via GetTraceID. Call it once per request, before handling.
Types ¶
type Config ¶
type Config struct {
// ServiceName tags every span; also used to create the named Tracer.
ServiceName string
// Endpoint is the OTLP/gRPC collector endpoint, e.g. "localhost:4317".
Endpoint string
// Insecure disables TLS to the collector (typical for local/in-cluster).
Insecure bool
// Probability is the sampling ratio in [0,1] applied to non-excluded routes.
Probability float64
// ExcludedRoutes are never sampled (health/readiness probes, etc.).
ExcludedRoutes map[string]struct{}
}
Config configures tracing.