otel

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Overview

Package otel provides framework-owned OpenTelemetry bootstrap and transport instrumentation helpers.

The package builds on the lower-level HTTP and gRPC OpenTelemetry wrappers in `github.com/CaliLuke/loom/http/middleware/otel` and `github.com/CaliLuke/loom/grpc/middleware/otel`. Use this package when a service wants Loom-owned provider bootstrap, HTTP metric-mode selection, and transport-level attribute hooks in addition to normal transport instrumentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddHTTPAttributes

func AddHTTPAttributes(ctx context.Context, attrs ...attribute.KeyValue)

AddHTTPAttributes appends request-scoped transport attributes to the active HTTP observability collector when the request is running inside HTTPMiddleware.

func GRPCClientOption

func GRPCClientOption(cfg GRPCConfig) grpc.DialOption

GRPCClientOption returns a gRPC dial option that installs OpenTelemetry stats handling using the official otelgrpc implementation.

func GRPCServerOption

func GRPCServerOption(cfg GRPCConfig) grpc.ServerOption

GRPCServerOption returns a gRPC server option that installs OpenTelemetry stats handling using the official otelgrpc implementation.

func HTTPMiddleware

func HTTPMiddleware(cfg HTTPConfig) func(http.Handler) http.Handler

HTTPMiddleware instruments an HTTP handler chain using the official OpenTelemetry HTTP middleware plus Loom-specific post-response enrichment hooks.

func WrapHTTPClient

func WrapHTTPClient(client *http.Client, cfg HTTPClientConfig) *http.Client

WrapHTTPClient returns a shallow copy of client with OpenTelemetry transport instrumentation configured according to cfg.

Types

type Config

type Config struct {
	// ServiceName is the logical service identity exported on resources.
	ServiceName string
	// ServiceVersion is the service version exported on resources.
	ServiceVersion string
	// Environment is the deployment environment exported on resources.
	Environment string
	// ResourceAttributes are merged into the provider resource.
	ResourceAttributes []attribute.KeyValue
	// Propagators configures request propagation for traces and logs.
	Propagators propagation.TextMapPropagator
	// Traces configures trace provider bootstrap.
	Traces TraceConfig
	// Metrics configures metric provider bootstrap.
	Metrics MetricConfig
	// Logs configures log provider bootstrap.
	Logs LogConfig
}

Config configures framework-owned OpenTelemetry bootstrap.

type GRPCConfig

type GRPCConfig struct {
	// TracerProvider overrides the trace provider used by otelgrpc.
	TracerProvider trace.TracerProvider
	// MeterProvider overrides the meter provider used by otelgrpc.
	MeterProvider metric.MeterProvider
	// Propagators overrides the propagators used by otelgrpc.
	Propagators propagation.TextMapPropagator
}

GRPCConfig configures gRPC OpenTelemetry instrumentation.

type HTTPAttributeSource

type HTTPAttributeSource interface {
	Attributes(*http.Request, HTTPRequestInfo) []attribute.KeyValue
}

HTTPAttributeSource returns span attributes for a completed request.

type HTTPClientConfig

type HTTPClientConfig struct {
	// ServiceName is the fallback operation name for client spans.
	ServiceName string
	// MetricMode selects otelhttp metrics or suppression for the client
	// transport.
	MetricMode HTTPMetricMode
	// TracerProvider overrides the trace provider used by otelhttp.
	TracerProvider trace.TracerProvider
	// MeterProvider overrides the meter provider used by otelhttp.
	MeterProvider metric.MeterProvider
	// Propagators overrides the propagators used by otelhttp.
	Propagators propagation.TextMapPropagator
}

HTTPClientConfig configures HTTP client-side OpenTelemetry instrumentation.

type HTTPConfig

type HTTPConfig struct {
	// ServiceName is the fallback operation name when no Loom route pattern is
	// available.
	ServiceName string
	// MetricMode selects otelhttp metrics, custom metrics, both, or neither.
	MetricMode HTTPMetricMode
	// TracerProvider overrides the trace provider used by otelhttp.
	TracerProvider trace.TracerProvider
	// MeterProvider overrides the meter provider used by otelhttp.
	MeterProvider metric.MeterProvider
	// Propagators overrides the propagators used by otelhttp.
	Propagators propagation.TextMapPropagator
	// AttributeSource returns post-response span attributes.
	AttributeSource HTTPAttributeSource
	// MetricsRecorder records custom post-response HTTP metrics.
	MetricsRecorder HTTPMetricsRecorder
}

HTTPConfig configures HTTP server-side OpenTelemetry instrumentation.

type HTTPMetricMode

type HTTPMetricMode string

HTTPMetricMode controls whether transport-level HTTP metrics are emitted by otelhttp, by a custom recorder, by both, or by neither.

const (
	// HTTPMetricModeOTelOnly emits otelhttp metrics only.
	HTTPMetricModeOTelOnly HTTPMetricMode = "otel_only"
	// HTTPMetricModeCustomOnly suppresses otelhttp metrics and uses only the
	// custom recorder.
	HTTPMetricModeCustomOnly HTTPMetricMode = "custom_only"
	// HTTPMetricModeBoth emits both otelhttp metrics and the custom recorder.
	HTTPMetricModeBoth HTTPMetricMode = "both"
	// HTTPMetricModeNone emits neither otelhttp metrics nor custom metrics.
	HTTPMetricModeNone HTTPMetricMode = "none"
)

type HTTPMetricsRecorder

type HTTPMetricsRecorder interface {
	Record(context.Context, *http.Request, HTTPRequestInfo)
}

HTTPMetricsRecorder records custom transport metrics for a completed request.

type HTTPRequestInfo

type HTTPRequestInfo struct {
	// Method is the HTTP method.
	Method string
	// Route is the Loom route pattern or fallback method-path name.
	Route string
	// StatusCode is the final HTTP response status.
	StatusCode int
	// Duration is the total request duration observed by the middleware.
	Duration time.Duration
	// PathValues contains resolved path parameter values keyed by parameter
	// name when the route pattern contains named segments.
	PathValues map[string]string
	// Authenticated is reserved for consumers that want to project auth state
	// into the request info externally; the framework does not infer it.
	Authenticated bool
}

HTTPRequestInfo describes a completed HTTP request as seen by the observability middleware.

type LogConfig

type LogConfig struct {
	// Enabled enables log provider bootstrap.
	Enabled bool
	// Endpoint is the OTLP HTTP endpoint host:port.
	Endpoint string
	// Insecure disables TLS for OTLP HTTP export.
	Insecure bool
	// Headers are sent on OTLP HTTP export requests.
	Headers map[string]string
}

LogConfig configures log provider bootstrap.

type MetricConfig

type MetricConfig struct {
	// Enabled enables metric provider bootstrap.
	Enabled bool
	// Endpoint is the OTLP HTTP endpoint host:port.
	Endpoint string
	// Insecure disables TLS for OTLP HTTP export.
	Insecure bool
	// Headers are sent on OTLP HTTP export requests.
	Headers map[string]string
}

MetricConfig configures metric provider bootstrap.

type Runtime

type Runtime struct {
	// TracerProvider is the initialized trace provider, if enabled.
	TracerProvider trace.TracerProvider
	// MeterProvider is the initialized metric provider, if enabled.
	MeterProvider metric.MeterProvider
	// LoggerProvider is the initialized log provider, if enabled.
	LoggerProvider log.LoggerProvider
	// Propagators is the propagator set used by this runtime.
	Propagators propagation.TextMapPropagator
	// Shutdown closes initialized providers in reverse initialization order.
	Shutdown func(context.Context) error
}

Runtime contains the initialized OpenTelemetry providers.

func New

func New(ctx context.Context, cfg Config) (*Runtime, error)

New initializes the configured OpenTelemetry providers and returns the resulting runtime.

type TraceConfig

type TraceConfig struct {
	// Enabled enables trace provider bootstrap.
	Enabled bool
	// Endpoint is the OTLP HTTP endpoint host:port.
	Endpoint string
	// Insecure disables TLS for OTLP HTTP export.
	Insecure bool
	// Headers are sent on OTLP HTTP export requests.
	Headers map[string]string
	// SampleRatio controls TraceID-based head sampling.
	SampleRatio float64
}

TraceConfig configures trace provider bootstrap.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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