Documentation
¶
Overview ¶
Package telemetry is the optional OpenTelemetry bootstrap for leanproxy-mcp (issue #317): OTLP/HTTP trace and metric exporters, off by default. It never touches the global otel providers unless explicitly enabled, so a deployment that does not configure telemetry pays no exporter cost and starts no background goroutines — otel's default no-op providers stay in effect and every span/metric call pkg/mcp makes is a cheap no-op.
Enabling telemetry never changes what is recorded: pkg/mcp attaches only names, sizes, durations, counts and status codes to spans and metrics, never argument or result payloads (see pkg/mcp's telemetry.go).
Index ¶
Constants ¶
const DefaultServiceName = "leanproxy-mcp"
DefaultServiceName is the `service.name` resource attribute used when Config.ServiceName is empty.
Variables ¶
This section is empty.
Functions ¶
func WrapTransport ¶
func WrapTransport(base http.RoundTripper) http.RoundTripper
WrapTransport wraps an http.RoundTripper so every outgoing request carries the current trace context as a W3C `traceparent` header (issue #317, acceptance criterion "an upstream HTTP server receives a traceparent header"). It reads the span from the request's own context, so callers must build requests with http.NewRequestWithContext using a context that carries the span pkg/mcp started for the call.
This is always safe to install, telemetry enabled or not: with telemetry disabled the global propagator stays the SDK's default no-op composite propagator, so Inject is a cheap type switch that writes nothing.
Types ¶
type Config ¶
type Config struct {
// Enabled is the master switch. Even with Endpoint set, telemetry stays
// off unless Enabled is true or an OTEL_EXPORTER_OTLP_*ENDPOINT env var
// is present (env vars are the standard, expected way to turn this on
// per issue #317, so they imply Enabled).
Enabled bool `yaml:"enabled,omitempty"`
// OTLP holds the exporter endpoint/protocol. Absent means "read from
// OTEL_EXPORTER_OTLP_ENDPOINT / OTEL_EXPORTER_OTLP_PROTOCOL" (or
// OTEL_EXPORTER_OTLP_TRACES_ENDPOINT / _METRICS_ENDPOINT for
// per-signal overrides, per the OTel spec).
OTLP *OTLPConfig `yaml:"otlp,omitempty"`
// ServiceName overrides the `service.name` resource attribute reported
// to the collector. Defaults to "leanproxy-mcp".
ServiceName string `yaml:"service_name,omitempty"`
}
Config is the `telemetry:` YAML block plus the standard OTel environment variables. A zero Config is disabled.
type OTLPConfig ¶
type OTLPConfig struct {
// Endpoint is the collector base URL, e.g. "http://localhost:4318".
// Empty means "read OTEL_EXPORTER_OTLP_ENDPOINT".
Endpoint string `yaml:"endpoint,omitempty"`
// Protocol is "http/protobuf" (default) or "http/json". Empty means
// "read OTEL_EXPORTER_OTLP_PROTOCOL", defaulting to http/protobuf.
Protocol string `yaml:"protocol,omitempty"`
// Insecure allows a plain-HTTP (non-TLS) endpoint. Inferred from the
// endpoint's scheme when false and the endpoint is http://.
Insecure bool `yaml:"insecure,omitempty"`
}
OTLPConfig is the `telemetry.otlp:` block.
type Protocol ¶
type Protocol string
Protocol is the OTLP wire protocol used for both traces and metrics. Only HTTP is supported (issue #317 deliberately excludes the gRPC exporter to keep the binary size delta small).
const ( // ProtocolHTTPProtobuf is the OTLP/HTTP exporter with protobuf bodies // (the OTEL_EXPORTER_OTLP_PROTOCOL default, "http/protobuf"). ProtocolHTTPProtobuf Protocol = "http/protobuf" // ProtocolHTTPJSON is the OTLP/HTTP exporter with JSON bodies // ("http/json"). ProtocolHTTPJSON Protocol = "http/json" )
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider owns the trace and meter providers Init installed as the global otel providers, plus their exporters. Shutdown flushes and closes both. A disabled Provider (Init with a disabled Resolved) has nil providers and Shutdown is a no-op: nothing was ever registered globally, so pkg/mcp's otel.Tracer/otel.Meter calls keep resolving to the SDK's built-in no-op implementations, exactly as if this package were never imported.
func Init ¶
Init sets up OTLP/HTTP exporters per r and installs them as the global otel trace/metric providers plus a W3C tracecontext propagator. Disabled (r.Enabled == false) is the default path and does nothing at all: no exporter, no batching goroutine, no periodic reader, and the global providers are left as otel's defaults (no-op).
The exporters are this package's own minimal OTLP/HTTP JSON encoder (see otlpjson.go), not go.opentelemetry.io/otel/exporters/otlp/*: those pull in google.golang.org/grpc and google.golang.org/protobuf transitively (an upstream design choice, not something this binary otherwise needs), which alone cost roughly 6 MB of binary size — well over issue #317's +3 MB budget. r.Protocol is still read from OTEL_EXPORTER_OTLP_PROTOCOL for forward compatibility (an operator's existing env var is not an error), but both http/protobuf and http/json currently produce the same JSON body: every OTLP/HTTP receiver is required to accept it (see otlpjson.go's doc comment).
type Resolved ¶
type Resolved struct {
Enabled bool
TracesEndpoint string
MetricsEndpoint string
Protocol Protocol
Insecure bool
ServiceName string
}
Resolved is the effective, environment-merged configuration Init consumes.
func Resolve ¶
Resolve merges cfg (the `telemetry:` config block, possibly nil) with the standard OTEL_EXPORTER_OTLP_* environment variables, which always win over the config block, matching every other OTel SDK's precedence rules. Telemetry is enabled when cfg.Enabled is true, or when any endpoint is configured (env or config) — an operator who sets an endpoint clearly wants telemetry on, without also having to set `enabled: true`.