telemetry

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 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, with flags and config winning over the environment:

  • File mode, when a directory is configured (Config.Dir, from each binary's flags/config): one JSON file per signal (traces.json, metrics.json, logs.json) via the OTEL stdout exporters. That is the format otel-tui's --from-json-file reads, so a user can send us the directory and we can replay it.
  • Env mode, when no directory is set but OTEL_* environment variables are: defers to contrib/exporters/autoexport (OTLP to a collector, console, or none).
  • Disabled otherwise: logging is the plain stderr handler.

The package installs the global Tracer/Meter/Logger providers. Instrumented packages reach them through the otel.Tracer/otel.Meter globals and the default slog.Logger rather than importing this package, so telemetry never forms an import cycle with the code it observes. The slog logger fans every record out to the stderr text handler (gated at the configured verbosity) and to an otelslog bridge into the OTEL log pipeline (which accepts debug regardless). The exported file therefore captures full diagnostics while stderr stays at the chosen level. Logging goes to stderr only; stdout is 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 binary
	// 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