telemetry

package
v0.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 0 Imported by: 0

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

View Source
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

func CaptureTraceContext(ctx context.Context) map[string]string

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

func ContextWithTraceContext(ctx context.Context, headers map[string]string) context.Context

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

func InitTracer(serviceName string) (func(context.Context) error, error)

InitTracer initializes OpenTelemetry tracing from OTEL_* environment variables.

func IsProbePath

func IsProbePath(r *http.Request) bool

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

func LogRequests(next http.Handler) http.Handler

LogRequests emits correlated logfmt completion records using route templates. Raw URLs, headers and bodies are deliberately excluded; healthy probes are quiet.

func LogRequestsForService

func LogRequestsForService(service string, next http.Handler) http.Handler

LogRequestsForService keeps bootstrap service identity aligned with metrics.

func LogfCtx

func LogfCtx(ctx context.Context, format string, args ...any)

LogfCtx logs like log.Printf and appends trace_id/span_id when ctx carries a span.

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

func RecordSpanFailure(ctx context.Context, operation, reason string, status int, err error)

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

func SpanIDFromContext(ctx context.Context) string

SpanIDFromContext returns the active span ID, or an empty string when the context does not carry a valid span.

func StartMetricsServer

func StartMetricsServer(port string) (func(context.Context) error, <-chan error)

StartMetricsServer starts a Prometheus metrics server with /metrics and /health.

func TraceIDFromContext

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext returns the active span trace ID, or an empty string when the context does not carry a valid trace.

func TraceLogSuffix

func TraceLogSuffix(ctx context.Context) string

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

func TraceableRequest(r *http.Request) bool

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

func (m *RequestMetrics) Middleware(service string, next http.Handler) http.Handler

Middleware records requests by ServeMux's matched route pattern. Unmatched paths share one label value so arbitrary URLs never become metric labels.

func (*RequestMetrics) Record

func (m *RequestMetrics) Record(service, operation string, status int, server string, duration time.Duration)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL