Telemetry
go.putnami.dev/telemetry provides OpenTelemetry integration for distributed tracing and metrics collection.
Plugin setup
import (
"go.putnami.dev/app"
"go.putnami.dev/telemetry"
)
a := app.New("my-service")
a.Module.Use(telemetry.NewPlugin(telemetry.Config{
ServiceName: "my-service",
ServiceVersion: "1.0.0",
TraceSampling: &telemetry.TraceSampling{Budget: 100, Ratio: 0.1, FloorPerMinute: 1},
}))The plugin:
- Initializes the OpenTelemetry SDK
- Registers
trace.TracerProviderandmetric.MeterProviderin DI - Shuts down exporters gracefully on application stop
Configuration
| Field | Type | Default | Description |
|---|---|---|---|
ServiceName |
string |
— | Service name for telemetry data |
ServiceVersion |
string |
— | Service version |
TraceSampling |
*telemetry.TraceSampling |
telemetry.DefaultTraceSampling() when nil |
Trace budget; see Trace budget |
TraceExporter |
exporter | — | Custom trace exporter |
MetricReader |
reader | — | Custom metric reader |
Trace budget
Each instance keeps its first budget root traces, then keeps a root when
ratio picks its trace ID, and keeps at least floorPerMinute roots each UTC
minute, so a rarely called service still exports traces. A span with a parent,
remote or local, follows its parent's sampled flag: a service follows its
caller's decision.
Behind a load balancer such as Cloud Run's, the edge sends a traceparent
whose sampled flag is usually off, so following it drops almost every server
span. With parent: decide, a server span whose parent came from outside, a
traceparent without an x-putnami-traceparent on the same trace, takes a
root decision by the budget instead, and keeps the parent's trace ID. A call
from another Putnami service sends both headers, so its decision still holds.
Key in telemetry.traces |
Default | Meaning |
|---|---|---|
budget |
100 |
Root traces the instance keeps first |
ratio |
0.01 |
Share of root traces kept once the budget is spent, from 0 to 1 |
floorPerMinute |
1 |
Root traces kept each minute whatever the ratio picks |
parent |
follow |
follow an outside parent's sampled flag, or decide as a root |
# conf/.env.yaml, or CONFIG_DATA
telemetry:
traces:
budget: 100
ratio: 0.01
floorPerMinute: 1
parent: decideConfig.TraceSampling sets the same values in code (Parent takes
telemetry.ParentFollow or telemetry.ParentDecide); each key of the
config section overrides its field. While it runs, the plugin reloads the
config sources every 30 seconds and applies a changed value without a restart.
A change to a file under conf/ applies within 30 seconds. CONFIG_DATA keeps
its first load for the life of the process, and so do the Putnami Cloud remote
sources unless CONFIG_SERVER_REFRESH sets a refresh period: a Cloud config
change then applies within that period plus a minute. To change a running
process otherwise, call plugin.Sampler().Update.
A value out of range keeps the settings in force and logs a warning.
The counter trace.decisions, labeled outcome kept or dropped, counts
each root decision. Plugin.Sampler() returns the sampler: Settings() reads
its own settings, without an export directive's, and Update(settings) replaces them at runtime until
the config section changes. The TypeScript runtime applies the same keys, rule
and counter, which go.putnami.dev/protocol/telemetry defines.
Export directives
The OTLP endpoint can set the trace settings and tighten the log level of
every service that pushes to it, without a redeploy. Putnami Cloud's collector
sends the settings a person set with putnami cloud telemetry set. Any export
response, from the metrics, traces or logs path and whatever its status, may
carry a Putnami-Export-Directive header:
Putnami-Export-Directive: trace-ratio=1, trace-parent=decide, log-level=warn, ttl=300| Key | Value | Effect while the directive holds |
|---|---|---|
trace-budget |
a whole number of 0 or more | replaces telemetry.traces.budget |
trace-ratio |
a number from 0 to 1 | replaces telemetry.traces.ratio |
trace-floor |
a whole number of 0 or more | replaces telemetry.traces.floorPerMinute |
trace-parent |
follow or decide |
replaces telemetry.traces.parent |
log-level |
debug, info, warn or error |
the higher of it and the logger's own level |
ttl |
whole seconds, required, capped at 3600 | how long the directive holds |
A trace setting the directive carries wins, higher or lower, and one it leaves
out keeps the service's own. The log level only tightens: a lower level
changes nothing, and a record at error is always emitted. A valid directive
replaces the one in force; when its ttl ends, the service returns to its own
settings. A member without =, a malformed known value, a repeated key or a
missing ttl voids the header, and an unknown key is ignored. The directive applies to the sampler of
each running plugin, whose Settings() keep the plugin's own values, and every
logger through logger.SetLevelLimit, without changing the live level. The
roots and records it keeps or drops still count in trace.decisions and
log.records.
A 429 Too Many Requests with a Retry-After, in seconds or as an HTTP date,
delays the next export of that signal to that collector, from every exporter
of the process, by at most an hour. A span or
metrics batch due during the delay is dropped; log records wait in the sink's
bounded queue, and a shutdown during the delay drops them with a warning. The
TypeScript runtime reads the same header by the same rule,
which go.putnami.dev/protocol/telemetry defines.
Tracing
Creating spans
import "go.putnami.dev/telemetry"
func processOrder(ctx context.Context, orderID string) error {
ctx, span := telemetry.StartSpan(ctx, "processOrder", "order")
defer span.End()
telemetry.SpanAttrs(span, "orderId", orderID)
if err := chargePayment(ctx, orderID); err != nil {
telemetry.SetSpanError(span, err)
return err
}
telemetry.SetSpanOK(span)
return nil
}Getting a tracer
tracer := telemetry.Tracer("my-service")
ctx, span := tracer.Start(ctx, "operation-name")
defer span.End()Span helpers
| Function | Description |
|---|---|
StartSpan(ctx, name, op) |
Create a child span |
SetSpanError(span, err) |
Record an error on the span |
SetSpanOK(span) |
Mark the span as successful |
SetSpanErrorStructured(span, err) |
Record a structured error (bridges go.putnami.dev/errors) |
SpanAttrs(span, key, value, ...) |
Set span attributes |
TraceIDFromContext(ctx) |
Extract trace ID (32-char hex) |
Trace ID extraction
traceID := telemetry.TraceIDFromContext(ctx)
// "4bf92f3577b34da6a3ce929d0e0e4736"Metrics
Creating instruments
import "go.putnami.dev/telemetry"
meter := telemetry.Meter("my-service")
// Counter
counter, _ := telemetry.Counter(meter, "requests_total")
counter.Add(ctx, 1)
// Histogram
histogram, _ := telemetry.Histogram(meter, "request_duration_ms")
histogram.Record(ctx, 42.5)
// Gauge
gauge, _ := telemetry.Gauge(meter, "active_connections")
gauge.Record(ctx, 15.0)
// Up/down counter
upDown, _ := telemetry.UpDownCounter(meter, "queue_size")
upDown.Add(ctx, 1) // item added
upDown.Add(ctx, -1) // item removedMetric helpers
| Function | Returns | Description |
|---|---|---|
Counter(meter, name) |
metric.Int64Counter |
Monotonically increasing counter |
Histogram(meter, name, opts...) |
metric.Float64Histogram |
Distribution of values |
Gauge(meter, name, opts...) |
metric.Float64Gauge |
Point-in-time value |
UpDownCounter(meter, name) |
metric.Int64UpDownCounter |
Counter that can increase or decrease |
HTTP middleware
Auto-trace HTTP requests with the telemetry middleware:
import (
fhttp "go.putnami.dev/http"
"go.putnami.dev/telemetry"
)
server := fhttp.NewServerPlugin(fhttp.ServerConfig{Port: 3000})
server.Use(telemetry.HTTPMiddleware("my-service"))The middleware automatically:
- Creates a span per request
- Records
http.method,http.path,http.status_codeattributes - Emits
http.server.request_countandhttp.server.durationmetrics, the same as the TypeScript runtime: Add observability lists them - Propagates trace context to downstream services
A proxy between two services may insert a span it never exports and rewrite
the parent ID of traceparent. A generated client therefore writes its
traceparent value twice, the second time in x-putnami-traceparent, which
proxies leave alone. When both headers carry the same trace ID and different
parent IDs, the server span parents to the one in x-putnami-traceparent, so
the trace keeps the call as one edge. When the second header is absent,
malformed or on another trace, the server uses traceparent. The plugin
installs this as telemetry.ProxySafeTraceContext; the TypeScript runtime
applies the same rule.
The plugin also carries a trace across events: a publish inside a span writes
the span's traceparent and tracestate into the event attributes, and each
delivery runs its handler in a consumer span, process <topic>, that continues
that trace, or starts a new root for an event without one. A Go publisher and a
TypeScript consumer share the trace.
Error bridge
When the telemetry plugin is active, structured errors from go.putnami.dev/errors automatically emit an errors_total counter with labels:
| Label | Source |
|---|---|
error.code |
Error code |
error.category |
Error category |
error.source |
Package where the error originated |
// This error automatically increments errors_total{code="db.query", category="infra"}
err := errors.Wrap(dbErr, "db.query").WithCategory(errors.CategoryInfra)Patterns
Service-level tracing
type OrderService struct {
tracer trace.Tracer
repo *OrderRepository
}
func NewOrderService(repo *OrderRepository) *OrderService {
return &OrderService{
tracer: telemetry.Tracer("OrderService"),
repo: repo,
}
}
func (s *OrderService) Create(ctx context.Context, input CreateOrderInput) (*Order, error) {
ctx, span := s.tracer.Start(ctx, "CreateOrder")
defer span.End()
order, err := s.repo.Create(ctx, input)
if err != nil {
telemetry.SetSpanError(span, err)
return nil, err
}
span.SetAttributes(attribute.String("orderId", order.ID))
return order, nil
}Custom metrics
type PaymentService struct {
meter metric.Meter
paymentCounter metric.Int64Counter
amountHist metric.Float64Histogram
}
func NewPaymentService() *PaymentService {
meter := telemetry.Meter("payments")
counter, _ := telemetry.Counter(meter, "payments_total")
hist, _ := telemetry.Histogram(meter, "payment_amount")
return &PaymentService{meter: meter, paymentCounter: counter, amountHist: hist}
}
func (s *PaymentService) Process(ctx context.Context, amount float64) error {
s.paymentCounter.Add(ctx, 1)
s.amountHist.Record(ctx, amount)
return nil
}Related guides
- HTTP & Middleware — HTTP tracing
- Errors — error metrics
- Logging — trace-correlated logs
Support and contract
go.putnami.dev/telemetry is stable in the workspace support catalog. Its
behavior is defined by the service telemetry specification and two accepted
decision records next to the package source (go/framework/telemetry/specs/ and
go/framework/telemetry/doc/adr/). Export failures are dropped rather than
propagated, so an unreachable collector degrades observability and nothing else.
Before v1.0.0 a minor 0.x release may still contain a documented breaking
change; strict compatibility between every pre-1.0 minor is not promised.