Help us shape the future of Go! Take the 2026 Go Developer Survey

monitoring

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const TraceIDHeader = "X-Trace-ID"

TraceIDHeader is the HTTP header name for trace ID

Variables

This section is empty.

Functions

func ExtractTraceIDFromRequest

func ExtractTraceIDFromRequest(r *http.Request) context.Context

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

func GetTraceIDFromContext(ctx context.Context) string

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

func HTTPMetricsMiddleware(next http.Handler) http.Handler

HTTPMetricsMiddleware wraps an HTTP handler to record metrics This now uses OpenTelemetry under the hood, but maintains backward compatibility

func Handler

func Handler() http.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

func Initialize(config Config) error

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

func IsExactRoute(route string) bool

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

func RecordExternalCall(target, operation string, duration time.Duration, err error)

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

func TraceIDMiddleware(next http.Handler) http.Handler

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

func WithTraceID

func WithTraceID(ctx context.Context, traceID string) context.Context

WithTraceID adds the given trace ID to the context This is used to propagate trace IDs for distributed tracing and observability correlation

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

func DefaultConfig(serviceName string) Config

DefaultConfig returns a default configuration

Jump to

Keyboard shortcuts

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