// 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() }