Documentation
¶
Overview ¶
Package telemetry owns service tracing, correlated logging, HTTP request metrics and response correlation. Service entry points initialize exporters and middleware; domain packages do not depend on this package. Kafka header adaptation lives separately in telemetry/kafkatrace.
Index ¶
- Constants
- func CaptureTraceContext(ctx context.Context) map[string]string
- func ConfigureOTelDiagnostics()
- func ConfigureTracePropagation()
- func ContextWithTraceContext(ctx context.Context, headers map[string]string) context.Context
- func CorrelateResponse(w http.ResponseWriter, ctx context.Context) http.ResponseWriter
- func InitTracer(serviceName string) (func(context.Context) error, error)
- func IsProbePath(r *http.Request) bool
- func LogRequests(next http.Handler) http.Handler
- func LogRequestsForService(service string, next http.Handler) http.Handler
- func LogfCtx(ctx context.Context, format string, args ...any)
- func OTLPTraceOptions(endpoint string) []otlptracehttp.Option
- func RecordSpanFailure(ctx context.Context, operation, reason string, status int, err error)
- func SpanIDFromContext(ctx context.Context) string
- func StartMetricsServer(port string) (func(context.Context) error, <-chan error)
- func TraceIDFromContext(ctx context.Context) string
- func TraceLogSuffix(ctx context.Context) string
- func TraceableRequest(r *http.Request) bool
- type RequestMetrics
Constants ¶
const TraceResponseHeader = "X-Trace-ID"
TraceResponseHeader exposes the active span's trace ID, never a caller's arbitrary correlation header. It is an observation identifier, not identity.
Variables ¶
This section is empty.
Functions ¶
func CaptureTraceContext ¶
CaptureTraceContext serializes the active trace context for async handoffs.
func ConfigureOTelDiagnostics ¶
func ConfigureOTelDiagnostics()
ConfigureOTelDiagnostics routes OpenTelemetry SDK internal errors, such as failed OTLP exports, to the service log and a Prometheus counter, so a silently unreachable collector is visible. It is safe to call repeatedly.
func ConfigureTracePropagation ¶
func ConfigureTracePropagation()
ConfigureTracePropagation enables W3C trace context and baggage propagation.
func ContextWithTraceContext ¶
ContextWithTraceContext extracts serialized trace context into ctx.
func CorrelateResponse ¶
func CorrelateResponse(w http.ResponseWriter, ctx context.Context) http.ResponseWriter
CorrelateResponse sets the trace ID before headers are committed, including proxy responses. Unwrap preserves ResponseController operations and streaming.
func InitTracer ¶
InitTracer initializes OpenTelemetry tracing from OTEL_* environment variables.
func IsProbePath ¶
IsProbePath reports whether the request is a health, readiness, or metrics probe. Probes are excluded from tracing so they do not drown failure traces.
func LogRequests ¶
LogRequests emits correlated logfmt completion records using route templates. Raw URLs, headers and bodies are deliberately excluded; healthy probes are quiet.
func LogRequestsForService ¶
LogRequestsForService keeps bootstrap service identity aligned with metrics.
func OTLPTraceOptions ¶
func OTLPTraceOptions(endpoint string) []otlptracehttp.Option
OTLPTraceOptions configures OTLP HTTP exporter options. It sets up the endpoint URL and configures secure/insecure connections based on whether the endpoint uses HTTPS or HTTP.
func RecordSpanFailure ¶
RecordSpanFailure marks the active span as failed so error spans are searchable. operation and reason must be bounded, non-secret identifiers (never tokens, headers, or MCP tool arguments). status is the HTTP status returned to the caller, or 0 when unknown. A nil err is allowed.
func SpanIDFromContext ¶
SpanIDFromContext returns the active span ID, or an empty string when the context does not carry a valid span.
func StartMetricsServer ¶
StartMetricsServer starts a Prometheus metrics server with /metrics and /health.
func TraceIDFromContext ¶
TraceIDFromContext returns the active span trace ID, or an empty string when the context does not carry a valid trace.
func TraceLogSuffix ¶
TraceLogSuffix returns " trace_id=<id> span_id=<id>" for the active span, or an empty string when there is none. Append it to log lines so logs can be joined to traces by ID (for example Loki to Tempo in Grafana).
func TraceableRequest ¶
TraceableRequest is an otelhttp filter that skips probe requests.
Types ¶
type RequestMetrics ¶
type RequestMetrics struct {
// contains filtered or unexported fields
}
RequestMetrics records request throughput, errors, and latency for service HTTP handlers using bounded service, operation, status, and server labels.
func DefaultRequestMetrics ¶
func DefaultRequestMetrics() *RequestMetrics
func NewRequestMetrics ¶
func NewRequestMetrics(registerer prometheus.Registerer) *RequestMetrics
func (*RequestMetrics) Middleware ¶
Middleware records requests by ServeMux's matched route pattern. Unmatched paths share one label value so arbitrary URLs never become metric labels.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package kafkatrace carries existing trace context through Kafka headers.
|
Package kafkatrace carries existing trace context through Kafka headers. |
|
Package sqltrace opens database/sql handles whose queries join the active trace.
|
Package sqltrace opens database/sql handles whose queries join the active trace. |