Documentation
¶
Overview ¶
Package otelsetup wires an OpenTelemetry OTLP pipeline for gopheragent with a single call. It is the ONLY package in the repository that imports the OpenTelemetry SDK and OTLP exporters, so a library consumer that does not need export pays nothing: the core (pkg/agent, pkg/telemetry/otelllm, pkg/telemetry/oteltools) depends only on the OTel API.
Usage:
tel, shutdown, err := otelsetup.Setup(ctx, otelsetup.Config{ServiceName: "my-agent"})
if err != nil { ... }
defer shutdown(context.Background())
llm := otelllm.NewProvider(p, otelllm.WithTracer(tel.Tracer), otelllm.WithMeter(tel.Meter), ...)
loop := agent.New(sm, reg, llm, agent.WithTracer(tel.Tracer), agent.WithMeter(tel.Meter))
Telemetry is off unless an endpoint is configured (Config.Endpoint or the standard OTEL_EXPORTER_OTLP_[<SIGNAL>_]ENDPOINT variables). It also honors OTEL_SDK_DISABLED=true, OTEL_TRACES_EXPORTER / OTEL_METRICS_EXPORTER ("otlp" or "none"), and OTEL_EXPORTER_OTLP_[<SIGNAL>_]PROTOCOL ("grpc" or "http/protobuf"). Config fields, when set, take precedence over the environment. A disabled signal yields a no-op handle, so callers wire the returned Tracer/Meter unconditionally.
When tracing is enabled, Setup also installs the W3C TraceContext + Baggage propagators globally so an agent run joins the caller's distributed trace (extract inbound headers with otel.GetTextMapPropagator()).
Index ¶
Constants ¶
const ( ProtocolGRPC = "grpc" ProtocolHTTP = "http/protobuf" )
OTLP transport protocols accepted by Config.Protocol and the OTEL_EXPORTER_OTLP_[<SIGNAL>_]PROTOCOL environment variables.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// ServiceName sets service.name on emitted telemetry. Empty falls back to
// "gopheragent" (OTEL_SERVICE_NAME still overrides via the SDK if set).
ServiceName string
// Endpoint overrides OTEL_EXPORTER_OTLP_ENDPOINT as a host:port, e.g.
// "localhost:4317" (gRPC) or "localhost:4318" (HTTP). Empty defers to the
// environment; with no endpoint anywhere, export stays off.
Endpoint string
// Insecure disables transport security (plaintext). Use for local
// collectors; leave false in production.
Insecure bool
// Protocol overrides OTEL_EXPORTER_OTLP_[<SIGNAL>_]PROTOCOL: ProtocolGRPC
// (the default) or ProtocolHTTP.
Protocol string
// BaggageKeys allowlists W3C Baggage members copied onto every span as
// attributes (e.g. "tenant.id" set by an upstream service). Inbound
// baggage is caller-controlled, so only listed keys are copied. Attributes
// set in-process via spanattr.With are always stamped.
BaggageKeys []string
}
Config controls the OTLP pipeline. The zero value is usable: it names the service "gopheragent" and reads everything else from the standard OTEL_* environment variables.
type Providers ¶
type Providers struct {
Tracer trace.Tracer
Meter metric.Meter
// Enabled reports whether at least one signal exports. Callers that want
// strictly zero instrumentation cost when off can skip wiring on false.
Enabled bool
}
Providers holds the ready-to-use Tracer and Meter handles for the gopheragent instrumentation scope. Both are always non-nil: a disabled signal gets a no-op implementation.
func Setup ¶
Setup resolves the export plan from cfg and the environment, builds an OTLP TracerProvider and/or MeterProvider for the enabled signals, registers them (plus the W3C propagators when tracing) as the global OpenTelemetry providers, and returns handles plus a shutdown function that flushes and closes the exporters. Callers should defer the shutdown function so buffered spans and metrics are exported on exit; it is safe to call when disabled.
func (*Providers) Instrument ¶ added in v0.46.0
Instrument wires full tracing and metrics into an already-built loop in one call: run and iteration spans on the loop, a chat span plus token metrics around loop.LLM, and an execute_tool span plus latency on every tool in loop.Tools. Pass otelllm options to label the model, e.g.
tel.Instrument(loop, otelllm.WithSystem("anthropic"), otelllm.WithModel(model))
Call it once the loop is configured and its tools are registered; tools registered later are not instrumented (a warning names them on the loop's first event). Calling it twice is harmless. When telemetry is disabled (no endpoint configured) it changes nothing, so it is safe to call unconditionally. Returns loop for chaining.
Sub-agent tools (builtin.CallSubAgentTool) hold their own LLM: wrap it with otelllm.NewProvider when constructing the tool to get the worker's chat spans. Worker run spans and metrics are inherited automatically.