Documentation
¶
Overview ¶
Package telemetry is Angelia's OpenTelemetry tracing (story #118, ADR 0020).
Tracing is off by default. Setup turns it on only when the standard OpenTelemetry environment variables ask for an OTLP exporter, and returns a shutdown function that flushes the spans. The rest of the code starts spans through Start, which is a no-op until a provider is installed, so instrumented packages need no configuration and no nil checks.
One check is one trace: the scheduler starts the root span, and fetch, commit and emit are its children. A delivery happens later and maybe in another process, so it is a trace of its own that carries a span link to the check; TraceParent and LinkFromTraceParent carry the context through the delivery row.
Span attributes are ids, kinds, counts and status codes. They never hold a URL, a header, a sink configuration or an error message that might quote one; RecordError keeps only the type of an error unless the caller passes an already redacted message.
Index ¶
- Constants
- func Enabled(getenv func(string) string) bool
- func LinkFromTraceParent(ctx context.Context, tp string) []trace.Link
- func NewLogHandler(h slog.Handler) slog.Handler
- func RecordError(span trace.Span, err error, msg string)
- func RecordErrorAs(span trace.Span, err error, class, msg string)
- func Setup(ctx context.Context, getenv func(string) string, version string, ...) (tp trace.TracerProvider, shutdown func(context.Context) error, err error)
- func Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func StartRoot(ctx context.Context, tp trace.TracerProvider, name string, links []trace.Link, ...) (context.Context, trace.Span)
- func TraceParent(ctx context.Context) string
Constants ¶
const FlushTimeout = 2 * time.Second
FlushTimeout bounds the final flush of buffered spans at shutdown. `angelia serve` takes it from the drain budget when tracing is on.
const ServiceName = "angelia"
ServiceName is the service.name used unless OTEL_SERVICE_NAME or OTEL_RESOURCE_ATTRIBUTES set one.
const TracerName = "github.com/angelia-dev/angelia"
TracerName is the instrumentation scope of every span Angelia starts.
Variables ¶
This section is empty.
Functions ¶
func Enabled ¶
Enabled reports whether env asks for an OTLP trace exporter: the SDK is not disabled (OTEL_SDK_DISABLED), the exporter list (OTEL_TRACES_EXPORTER) is empty or names otlp, and an endpoint is set (OTEL_EXPORTER_OTLP_ENDPOINT or OTEL_EXPORTER_OTLP_TRACES_ENDPOINT). Without an endpoint the answer is no: the SDK default of localhost:4318 would make every plain install try to export.
func LinkFromTraceParent ¶
LinkFromTraceParent returns a link to the span a traceparent names, or nil when tp is empty or malformed.
func NewLogHandler ¶
NewLogHandler wraps h so that records logged with a context that carries a span get trace_id and span_id attributes. Other records pass through.
func RecordError ¶
RecordError marks span as failed. It records a coarse class of err as error.type, never its text, because the text of a transport or sink error can quote a URL, a header or a token: "timeout", "canceled", or the Go type. msg is shown as the status description; pass only text that is safe to export (a redacted message, or a fixed phrase).
func RecordErrorAs ¶
RecordErrorAs is RecordError with a caller-chosen class, for errors only the caller can recognise (a permanent sink error). An empty class falls back to the generic one.
func Setup ¶
func Setup(ctx context.Context, getenv func(string) string, version string, log *slog.Logger) (tp trace.TracerProvider, shutdown func(context.Context) error, err error)
Setup builds a tracer provider that exports over OTLP/HTTP when Enabled says so, and returns it with the function that flushes and stops it. When tracing is off it returns a no-op provider and a shutdown that does nothing. The provider is not installed globally: the roles take it in their Options and spans below theirs inherit it through the context.
The one process-global piece is OpenTelemetry's error handler (the SDK has no per-provider alternative). When tracing is on, Setup installs one that logs export failures through log at Warn, at most once per 30 s with the number of errors dropped meanwhile. Call Setup once, from the command.
The exporter, sampler and resource read the standard variables themselves: OTEL_EXPORTER_OTLP_[TRACES_]ENDPOINT, _HEADERS, _TIMEOUT, _COMPRESSION, _CERTIFICATE, _INSECURE, OTEL_TRACES_SAMPLER[_ARG], OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES. Only http/protobuf is supported; asking for grpc is an error rather than a silent fallback. version is the service.version resource attribute.
func Start ¶
func Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
Start starts a span named name as a child of the span in ctx. It uses the tracer provider of that span, so it is a no-op when ctx carries none.
func StartRoot ¶
func StartRoot(ctx context.Context, tp trace.TracerProvider, name string, links []trace.Link, attrs ...attribute.KeyValue) (context.Context, trace.Span)
StartRoot starts a span from tp that begins a new trace even if ctx carries a span, with optional links to other spans. A nil tp records nothing.
func TraceParent ¶
TraceParent returns the W3C traceparent of the span in ctx, or "" when there is none.
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package telemetrytest records spans in memory for tests.
|
Package telemetrytest records spans in memory for tests. |