Files
Sharang ParnerkarandClaude Opus 5.5 65ad1b52f6
ci / shared (pull_request) Failing after 12s
ci / test (pull_request) Failing after 15m24s
ci / image (pull_request) Skipped
feat(telemetry): traces to SigNoz for requests, outbound calls and SQL
internal/telemetry: OTLP/HTTP tracer provider from the OTEL_* env
(already set on stage and prod; unset = off), W3C propagation, one server
span per request named after the ServeMux pattern, client spans on the
default transport (Keycloak, ERP, LiteLLM, OIDC) and a pgx QueryTracer.
Client and DB spans only inside a request, so the background loops add
no traces. No raw paths, query strings, bodies, SQL parameters or client
addresses in spans; outbound spans carry the host only.

slog lines logged with a request context get trace_id/span_id; the
access log names the route instead of the raw path (slugs are names).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KYcAAeTw22GE9QPzDM8yc4
2026-10-28 22:24:34 +01:00

92 lines
3.3 KiB
Go

// Package telemetry sends traces to SigNoz over OTLP/HTTP
// (research/observability/2026-10-28-instrumentation-portal-registry-db.md).
//
// Setup reads the standard variables: OTEL_EXPORTER_OTLP_ENDPOINT (unset = off,
// nothing is exported), OTEL_EXPORTER_OTLP_HEADERS (the bearer token),
// OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES.
//
// Privacy rule for every span: no raw paths (slugs are names), no query
// strings, no bodies, no SQL parameters, no client addresses. Server spans
// carry the route template, client spans the host only.
package telemetry
import (
"context"
"errors"
"os"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
const scope = "git.breakpilot.com/platform/tenant-registry"
// Info names the running build in the resource.
type Info struct {
Service string // default service.name when OTEL_SERVICE_NAME is unset
Version string
Commit string
Env string // dev | stage | prod
}
// Setup installs the global tracer provider and the W3C propagator. Without
// OTEL_EXPORTER_OTLP_ENDPOINT it installs only the propagator and returns a
// no-op shutdown, so a dev run or an unconfigured deployment works unchanged.
func Setup(ctx context.Context, info Info) (shutdown func(context.Context) error, err error) {
otel.SetTextMapPropagator(propagation.TraceContext{})
if os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT") == "" {
return func(context.Context) error { return nil }, nil
}
exp, err := otlptracehttp.New(ctx)
if err != nil {
return nil, err
}
tp, err := newProvider(ctx, info, sdktrace.WithBatcher(exp))
if err != nil {
return nil, errors.Join(err, exp.Shutdown(ctx))
}
otel.SetTracerProvider(tp)
return tp.Shutdown, nil
}
// newProvider builds the provider; tests pass a span recorder instead of the
// exporter.
func newProvider(ctx context.Context, info Info, opts ...sdktrace.TracerProviderOption) (*sdktrace.TracerProvider, error) {
attrs := []attribute.KeyValue{
attribute.String("service.name", info.Service),
attribute.String("service.version", info.Version),
}
if info.Commit != "" {
attrs = append(attrs, attribute.String("vcs.ref.head.revision", info.Commit))
}
if info.Env != "" {
attrs = append(attrs, attribute.String("deployment.environment", info.Env))
}
// Later options win: the environment overrides the defaults above.
res, err := resource.New(ctx, resource.WithAttributes(attrs...), resource.WithFromEnv(), resource.WithTelemetrySDK())
if err != nil {
return nil, err
}
opts = append([]sdktrace.TracerProviderOption{sdktrace.WithResource(res)}, opts...)
return sdktrace.NewTracerProvider(opts...), nil
}
// ShutdownTimeout bounds the final flush on SIGTERM.
const ShutdownTimeout = 5 * time.Second
func tracer() trace.Tracer { return otel.Tracer(scope) }
// hasParent reports whether ctx already belongs to a trace. Client and DB
// spans are only created inside one: background loops (outbox, reconcile,
// mail) poll every few seconds and would otherwise flood SigNoz with
// single-span traces.
func hasParent(ctx context.Context) bool {
return trace.SpanContextFromContext(ctx).IsValid()
}