Documentation
¶
Overview ¶
Package telemetry initialises OpenTelemetry tracing and metrics for a service in a single call, exposing a Prometheus /metrics endpoint and sending traces to an OTLP/gRPC collector.
Initialisation ¶
Call New once at startup. It installs the global TracerProvider and MeterProvider, starts the Prometheus HTTP server, and returns a shutdown function that must be called on exit to flush pending spans.
shutdown, err := telemetry.New(ctx, telemetry.Config{
ServiceName: "my-service",
OTLPEndpoint: "otel-collector:4317",
MetricsPort: 9090,
})
if err != nil {
log.Fatal(err)
}
defer shutdown(ctx)
Tracing ¶
After New returns, use the global tracer from anywhere in the application.
tracer := otel.Tracer("my-service/orders")
ctx, span := tracer.Start(ctx, "process-order")
defer span.End()
Metrics ¶
After New returns, use the global meter from anywhere in the application.
meter := otel.Meter("my-service/orders")
counter, _ := meter.Int64Counter("orders.processed")
counter.Add(ctx, 1)
Pluggable collectors ¶
Custom metric sources implement the Collector interface and are passed in Config.Collectors. Each collector registers its instruments once and optionally runs a background polling loop.
shutdown, err := telemetry.New(ctx, telemetry.Config{
ServiceName: "my-service",
MetricsPort: 9090,
Collectors: []telemetry.Collector{myCollector},
})
A collector that fails RegisterMetrics is skipped without stopping the rest of the subsystem.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New initialises the global TracerProvider and MeterProvider, then starts a dedicated HTTP server on cfg.MetricsPort exposing /metrics for Prometheus. Any collectors supplied in cfg.Collectors are registered and started.
The returned shutdown function must be called on application exit to flush pending spans and stop the metrics server. It is safe to call even if New returned an error (it becomes a no-op).
Types ¶
type Collector ¶
type Collector interface {
// RegisterMetrics registers the collector's instruments. It is called
// once during New, after the MeterProvider is set globally.
RegisterMetrics() error
// Start launches the collector's background work, if any. It must not
// block; long-running work belongs in a goroutine bound to ctx.
Start(ctx context.Context)
}
Collector is a pluggable source of custom metrics. Implementations register their instruments against the global MeterProvider (already configured by New) and optionally run a background loop to feed them.
The lifecycle is:
- RegisterMetrics is called once, after the MeterProvider is installed but before Start. Returning an error aborts the collector (Start is skipped) without affecting the rest of the telemetry subsystem.
- Start is called with a context tied to the telemetry lifetime; the collector should return promptly, spawning any background work as a goroutine that exits when ctx is cancelled.
A typical implementation polls an external system on a ticker and exposes the latest snapshot through OTel observable instruments.
type Config ¶
type Config struct {
// ServiceName identifies this service in traces and metrics. Required.
ServiceName string
// OTLPEndpoint is the OTLP/gRPC collector address (host:port). When empty,
// the exporter's default endpoint is used.
OTLPEndpoint string
// MetricsPort is the TCP port for the Prometheus /metrics HTTP server.
MetricsPort uint16
// Collectors are optional pluggable metric sources. Each is registered and
// started during New, after the global providers are installed. A
// collector whose RegisterMetrics fails is skipped without aborting the
// rest of the subsystem.
Collectors []Collector
}
Config holds the runtime settings for the telemetry subsystem.