Documentation
¶
Overview ¶
Package otel provides a shared, env-driven OpenTelemetry tracing bootstrap for rest-api binaries. It installs a global SDK tracer provider exporting over OTLP, configured through standard OTEL_* environment variables plus bounded batch-processor settings, so the instrumentation libraries already wired throughout the codebase (otelecho, otelhttp, otelgrpc, bunotel, Temporal interceptors) emit real spans.
Index ¶
- func Bootstrap(ctx context.Context, enabled bool, serviceNameFallback string) (func(context.Context) error, error)
- func EndSpan(span trace.Span, err error)
- func ExporterConfigured() bool
- func LoggerWithTrace(ctx context.Context, logger zerolog.Logger) zerolog.Logger
- func Propagator() propagation.TextMapPropagator
- func RecordError(span trace.Span, err error)
- func RecordHTTPError(ctx context.Context, statusCode int)
- func RecordSuccess(span trace.Span)
- func SetAttribute(span trace.Span, attr attribute.KeyValue)
- func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func TracingEnabled() bool
- func TransportEnabled() bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bootstrap ¶
func Bootstrap(ctx context.Context, enabled bool, serviceNameFallback string) (func(context.Context) error, error)
Bootstrap installs a global OTLP-exporting tracer provider and the OTEL_PROPAGATORS-configured propagator (W3C trace-context by default). Propagation remains active when enabled is false or no OTLP endpoint is configured, allowing non-exporting services to preserve distributed traces. serviceNameFallback is used only when the environment does not provide service.name.
The returned shutdown function flushes pending spans and is always non-nil.
func EndSpan ¶
EndSpan records an error when err is non-nil and ends the span. Intended for use with named returns: defer func() { cotel.EndSpan(span, retErr) }().
func ExporterConfigured ¶
func ExporterConfigured() bool
ExporterConfigured reports whether an OTLP trace exporter endpoint is configured via the standard environment variables. It is intended for binaries whose tracing enablement is entirely environment-driven.
func LoggerWithTrace ¶
LoggerWithTrace returns logger enriched with trace_id/span_id fields when ctx carries a valid span context, enabling log/trace correlation.
func Propagator ¶
func Propagator() propagation.TextMapPropagator
Propagator returns the installed global propagator, or the environment- configured propagator for clients constructed before Bootstrap. Invalid pre-bootstrap configuration is reported through the global OTel error handler and falls back to W3C trace-context propagation.
func RecordError ¶
RecordError records err and marks the span as failed without ending it.
func RecordHTTPError ¶
RecordHTTPError records stable HTTP failure metadata without exporting response messages or internal error details. Server failures mark the span as failed; client failures are recorded as events.
func RecordSuccess ¶
RecordSuccess marks a span as successfully completed.
func SetAttribute ¶
SetAttribute sets an attribute on the span.
func StartSpan ¶
func StartSpan(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
StartSpan starts a child span using the global TracerProvider; the parent is taken from ctx via its W3C span context, so it works under otelecho, Temporal workers, gRPC handlers — anywhere a context flows. It never returns a nil span: when no provider is installed the returned span is a no-op.
func TracingEnabled ¶
func TracingEnabled() bool
TracingEnabled reports whether Bootstrap successfully installed a tracer provider. Recording-only instrumentation such as database hooks must use this gate. Transport instrumentation must use TransportEnabled so context can cross a service that does not export spans locally.
func TransportEnabled ¶
func TransportEnabled() bool
TransportEnabled reports whether Bootstrap configured transport instrumentation. It remains true when local export is disabled so an intermediate service can still extract and forward incoming trace context.
OTEL_PROPAGATORS=none is asymmetric: without export it disables transport instrumentation entirely, since there is nothing to record or forward. With export it keeps transport instrumentation on so local spans still record, and only injection and extraction are disabled.
Types ¶
This section is empty.