telemetry

package
v0.113.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 28 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package telemetry installs W3C trace propagation, creates privacy-conscious inbound/outbound HTTP spans, and exports them with OTLP/HTTP when configured.

Go Common's server.New initializes this package automatically. Applications using a custom server may call Init and wrap the handler with HTTPMiddleware. safehttp.NewClient adds NewTransport automatically.

For automatic OpenObserve export, provide a service-scoped FLEET_SECRETS_API_KEY (recommended) or legacy FLEET_API_KEY that can read the allowlisted ingestion secret from Go Fleet Secrets. Without an endpoint override, export uses https://openobserve.0own.com/api/default/v1/traces. An OTEL_EXPORTER_OTLP_ENDPOINT override is preserved; the vault token is attached only for the approved HTTPS hosts otlp.0exec.com and openobserve.0own.com. Other collector hosts must use OTEL_EXPORTER_OTLP_HEADERS for their own credentials. Header values follow the OpenTelemetry comma-separated, URL-escaped key=value convention. OTEL_EXPORTER_OTLP_CERTIFICATE may specify a private CA PEM; client certificates can be supplied with OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE and OTEL_EXPORTER_OTLP_CLIENT_KEY. OTEL_SAMPLE_RATE defaults to 0.1 and uses parent-based sampling. OTEL_DISABLED=true turns off exporting. Cleartext HTTP is rejected unless OTEL_EXPORTER_OTLP_INSECURE=true is explicitly set for a protected network. Trace context propagation uses W3C traceparent only; baggage is not propagated by default.

The ingestion credential is read once, before Init returns, with a 2 s limit. If that read fails transiently (a timeout, a connection error, or a 5xx, 408 or 429 answer), Init still returns at once and a bounded background retry installs the tracer provider when the credential is finally read: at most six more attempts after waits of 2, 4, 8, 16, 32 and 64 s (about two minutes, each wait shortened by up to 20% so that services restarting together do not meet the vault in lockstep), each with the same 2 s limit. A 401, 403 or 404, and any other answer the vault gave on purpose, is a configuration error: it is reported once and never retried, and tracing stays off. Config.Shutdown cancels the retry. One line is logged if tracing comes up late and one error line if the attempts run out; neither contains a credential.

HTTP spans intentionally omit URLs, query strings, headers, and bodies. Application-created spans should also use low-cardinality names and avoid user data, credentials, raw SQL, and other secrets in attributes.

Package telemetry provides fleet-wide OpenTelemetry tracing primitives.

Init installs W3C Trace Context propagation and, when an OTLP endpoint is configured, a batch-exporting SDK. HTTPMiddleware and NewTransport create privacy-conscious HTTP spans. They deliberately omit URLs, headers, bodies, SQL text, and baggage from exported data.

Index

Constants

View Source
const ExporterTimeout = 5 * time.Second

ExporterTimeout is the default bound used by service shutdown hooks.

Variables

This section is empty.

Functions

func HTTPMiddleware

func HTTPMiddleware(next http.Handler) http.Handler

HTTPMiddleware extracts W3C trace context and creates a server span without recording request paths, query strings, headers, or bodies. Health and metrics endpoints are excluded to avoid filling the trace store with probes.

func NewTransport

func NewTransport(base http.RoundTripper) http.RoundTripper

NewTransport adds an outbound client span and injects W3C trace context. It records only method, response status, and transport-error type; the URL, query string, request/response headers, and bodies are intentionally omitted.

func StartSpan

func StartSpan(ctx context.Context, operation string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

StartSpan starts an application span. Keep operation names and attributes low-cardinality and free of user data, URLs, SQL text, and secrets.

func Tracer

func Tracer(scope string) trace.Tracer

Tracer returns a named OpenTelemetry tracer for application-level spans.

Types

type Config

type Config struct {
	ServiceName    string
	ServiceVersion string
	OTLPEndpoint   string
	SampleRate     float64
	Disabled       bool
	// contains filtered or unexported fields
}

Config describes one process's tracing configuration. A process should call Init once from Go Common's server setup and call Shutdown during termination.

func Init

func Init(serviceName, serviceVersion string, opts ...Option) *Config

Init installs W3C Trace Context propagation and initializes the global SDK if an endpoint is configured. Services can use a protected token file or a dedicated Fleet Secrets API key to load the ingestion-only OpenObserve token and export directly over HTTPS. It is safe to call repeatedly from tests and from server construction; one process shares one provider/exporter.

The credential is read once before Init returns, with a 2 s limit. If that read fails transiently (timeout, connection error, 5xx, 408 or 429), Init still returns at once and a bounded background retry installs the provider when the credential is finally read; Shutdown cancels it. A failure that retrying cannot cure, such as a 401, 403 or 404, is not retried.

func (*Config) Shutdown

func (c *Config) Shutdown(ctx context.Context) error

Shutdown flushes exported spans. If Init deferred to a background credential retry, Shutdown first cancels it, aborting an attempt in flight, and waits for it to exit, so no provider can be installed after Shutdown returns. The caller supplies a bounded context.

type Option

type Option func(*Config)

Option configures tracing initialization.

func WithDisabled

func WithDisabled() Option

WithDisabled disables trace export while leaving W3C context extraction and injection available for upstream/downstream continuity.

func WithOTLP

func WithOTLP(endpoint string) Option

WithOTLP sets the OTLP/HTTP endpoint. HTTPS is required unless the explicit insecure option or OTEL_EXPORTER_OTLP_INSECURE=true is set.

func WithSampleRate

func WithSampleRate(rate float64) Option

WithSampleRate sets the root trace sampling ratio in [0,1]. The default is 0.1; parent-based sampling preserves the decision made by an upstream trace.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL