Documentation
¶
Index ¶
- Constants
- func ExtractTraceIDFromRequest(r *http.Request) context.Context
- func GetInitError() error
- func GetTraceIDFromContext(ctx context.Context) string
- func HTTPMetricsMiddleware(next http.Handler) http.Handler
- func Handler() http.Handler
- func Initialize(config Config) error
- func IsExactRoute(route string) bool
- func IsInitialized() bool
- func RecordBusinessEvent(action, outcome string)
- func RecordExternalCall(target, operation string, duration time.Duration, err error)
- func RegisterRoutes(routesList []string)
- func TraceIDMiddleware(next http.Handler) http.Handler
- func WithTraceID(ctx context.Context, traceID string) context.Context
- type Config
Constants ¶
const TraceIDHeader = "X-Trace-ID"
TraceIDHeader is the HTTP header name for trace ID
Variables ¶
This section is empty.
Functions ¶
func ExtractTraceIDFromRequest ¶
ExtractTraceIDFromRequest extracts trace ID from HTTP header and adds it to context If no trace ID is found in header, generates a new one This ensures trace ID propagation across HTTP service boundaries
func GetInitError ¶
func GetInitError() error
GetInitError returns the initialization error if metrics failed to initialize. Returns nil if initialization succeeded or hasn't been attempted yet. Services can call this to check if metrics are working and take appropriate action (e.g., fail to start if metrics are critical, or log a health check status).
func GetTraceIDFromContext ¶
GetTraceIDFromContext retrieves the trace ID from the context Returns empty string if trace ID is not found in context This is used for distributed tracing and observability correlation across service boundaries
func HTTPMetricsMiddleware ¶
HTTPMetricsMiddleware wraps an HTTP handler to record metrics This now uses OpenTelemetry under the hood, but maintains backward compatibility
func Handler ¶
Handler returns the metrics HTTP handler This now uses OpenTelemetry under the hood, but maintains backward compatibility For Prometheus exporter, this returns the Prometheus metrics endpoint For OTLP exporter, this returns a simple status endpoint
func Initialize ¶
Initialize sets up OpenTelemetry metrics with the given configuration This function is thread-safe and can be called multiple times safely. Only the first call will perform initialization; subsequent calls return nil.
func IsExactRoute ¶
IsExactRoute checks if a route is exactly registered as a static route (no template matching).
func IsInitialized ¶
func IsInitialized() bool
IsInitialized returns true if metrics have been successfully initialized. Returns false if initialization failed or hasn't been attempted yet.
func RecordBusinessEvent ¶
func RecordBusinessEvent(action, outcome string)
RecordBusinessEvent records a business event This now uses OpenTelemetry under the hood, but maintains backward compatibility
func RecordExternalCall ¶
RecordExternalCall records an external service call This now uses OpenTelemetry under the hood, but maintains backward compatibility
func RegisterRoutes ¶
func RegisterRoutes(routesList []string)
RegisterRoutes registers routes for normalization. Supports static routes and templates with :id or {id} placeholders. Templates match incoming paths and normalize dynamic segments. Call during service initialization.
Example: RegisterRoutes([]string{"/health", "/api/v1/schema/:id", "/api/v1/applications/{id}/activate"})
func TraceIDMiddleware ¶
TraceIDMiddleware extracts or generates a trace ID and adds it to the request context It checks for X-Trace-ID header first, and if not present, generates a new UUID The trace ID is also set in the response header for client visibility This middleware should be applied early in the middleware chain to ensure trace ID is available throughout the request lifecycle
Types ¶
type Config ¶
type Config struct {
// ExporterType can be "prometheus", "otlp", or "none" (disabled)
ExporterType string
// ServiceName is the name of the service (e.g., "portal-backend", "orchestration-engine")
ServiceName string
// ServiceVersion is the version of the service (e.g., "1.0.0", "v2.3.1")
// Defaults to "dev" if not set via SERVICE_VERSION environment variable
ServiceVersion string
// OTLPEndpoint is the OTLP endpoint URL (for Datadog, New Relic, etc.)
// Example: "https://api.datadoghq.com/api/v2/otlp"
OTLPEndpoint string
// OTLPHeaders are additional headers for OTLP exporter (e.g., API keys)
OTLPHeaders map[string]string
// PrometheusPort is the port for Prometheus exporter (default: 8888)
PrometheusPort int
// OTLPTLSInsecure allows insecure TLS connections (only for development/testing)
// Set via OTEL_EXPORTER_OTLP_INSECURE environment variable
OTLPTLSInsecure bool
// HistogramBuckets allows customization of histogram bucket boundaries (in seconds)
// Default: [.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10]
// These boundaries are optimized for HTTP request latency measurements:
// - Sub-10ms: .005, .01, .025 (very fast responses)
// - 10-100ms: .05, .1, .25 (typical API responses)
// - 100ms-1s: .5, 1 (slower operations)
// - 1s+: 2.5, 5, 10 (long-running operations, timeouts)
HistogramBuckets []float64
}
Config holds the configuration for OpenTelemetry metrics
func DefaultConfig ¶
DefaultConfig returns a default configuration