telemetry

package
v0.11.4 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 20 Imported by: 0 modules, 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 wires OpenTelemetry tracing, metrics, and structured logging into the patchy binaries and owns their lifecycle.

Telemetry is off by default. Init picks one of three modes, flag/config winning over environment: when a directory is configured (Config.Dir, wired by each binary's flags/config) it runs in file mode, writing one JSON file per signal (traces.json, metrics.json, logs.json) via the OTEL stdout exporters — the exact format otel-tui's --from-json-file ingests, so a user can save the directory, ship it to us, and we replay it. When no directory is set but OTEL_* environment variables are, it runs in env mode and defers to contrib/exporters/autoexport (OTLP to a collector, console, or none). With neither, it stays disabled and logging is the plain stderr handler patchy has always used.

The package installs the global Tracer/Meter/Logger providers, so the instrumented packages reach them through the otel.Tracer/otel.Meter globals and the default slog.Logger rather than importing this package — that one-way dependency keeps telemetry free of cycles with the code it observes. The slog logger fans every record out to both the stderr text handler (gated at the configured verbosity) and an otelslog bridge into the OTEL log pipeline (which accepts debug regardless), so the shipped file captures full diagnostics while stderr stays as quiet as it was before. Logging goes to stderr only; stdout stays reserved for agent-runner's event stream.

Init returns a ShutdownFunc that flushes the providers in order (tracer, meter, logger) and then closes the files; main calls it inline after the command returns, since cobra skips PersistentPostRunE on error and os.Exit skips defers. Init never fails the caller: any setup error yields a working stderr-only logger plus a no-op shutdown alongside the error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(ctx context.Context, cfg Config) (*Provider, ShutdownFunc, error)

Init selects an export mode (file when Config.Dir is set, else env when OTEL_* is set, else disabled), installs the global Tracer/Meter/Logger providers, and returns the logger plus a shutdown that flushes them. It never fails the caller: on a setup error it returns a working stderr-only logger and a no-op shutdown alongside the error, leaving telemetry disabled.

Types

type Config

type Config struct {
	// Dir, when non-empty, selects file mode and names the directory the
	// per-signal JSON files are written under. It comes from each binary's
	// flags/config and always wins over OTEL_* env vars.
	Dir string
	// Level gates the stderr text handler; pass the shared verbosity LevelVar so
	// the file and stderr stay in lockstep with the flag.
	Level slog.Leveler
	// ServiceName and ServiceVersion populate the OTEL resource; each of the
	// four binaries passes its own name.
	ServiceName    string
	ServiceVersion string
	// Stderr is where the human-readable text handler writes; nil means
	// os.Stderr.
	Stderr io.Writer
}

Config configures Init. The zero value selects disabled mode with a stderr logger at the default level.

type Mode

type Mode int

Mode is the export path Init selected.

const (
	// ModeDisabled keeps telemetry off: no providers, stderr-only logging.
	ModeDisabled Mode = iota
	// ModeFile writes one JSON file per signal under Config.Dir.
	ModeFile
	// ModeEnv defers to autoexport, honoring the OTEL_* environment.
	ModeEnv
)

func (Mode) String

func (m Mode) String() string

String names the mode for diagnostics.

type Provider

type Provider struct {
	Logger *slog.Logger
	Mode   Mode
}

Provider is the result of Init: the logger every subcommand should adopt and the mode that was selected.

type ShutdownFunc

type ShutdownFunc func(context.Context) error

ShutdownFunc flushes the providers and closes any open files. It is safe to call once; later calls are no-ops.

Jump to

Keyboard shortcuts

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