observability

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 31 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 observability provides the diagnostic log and OpenTelemetry export. Both are fed from the goroutines doing the work and written by dedicated background goroutines, so logging and tracing never wait on disk or network I/O.

Index

Constants

View Source
const ConversationID = attribute.Key("gen_ai.conversation.id")

ConversationID is the OpenTelemetry GenAI key for the session a span belongs to. The ADK sets it on its spans; ours use the same key so one query finds every turn of a session.

View Source
const DefaultEndpoint = "http://localhost:4318"

DefaultEndpoint is the OTLP/HTTP collector address from the OpenTelemetry spec. The Go exporters would otherwise default to https, which local collectors don't serve.

View Source
const ServiceName = "blitz"

ServiceName identifies Blitz in exported telemetry.

Variables

View Source
var ErrBadLogDay = errors.New("not a log day: use YYYY-MM-DD")

ErrBadLogDay reports a day that isn't a date (YYYY-MM-DD).

View Source
var ErrLogInUse = errors.New("today's log is still being written")

ErrLogInUse is returned when deleting the log being written: today's.

View Source
var ErrNoLogDay = errors.New("no log for that day")

ErrNoLogDay reports a day dir has no log for.

Functions

func DeleteLogDay

func DeleteLogDay(dir, day string, now time.Time) error

DeleteLogDay deletes day's log (YYYY-MM-DD) in dir. Today's, by now's date, is the one being written and is refused (ErrLogInUse).

func End

func End(span trace.Span, err error)

End records err (cancellation is not an error) and ends the span.

func LogDays

func LogDays(dir string) ([]string, error)

LogDays lists the days dir has a log for (YYYY-MM-DD), newest first.

func OpenLog

func OpenLog(cfg config.LogConfig, r *redact.Redactor, extra ...slog.Handler) (*slog.Logger, io.Closer, error)

OpenLog returns a logger writing JSON lines to cfg.Dir (secrets masked) and to each extra handler (e.g. Telemetry.LogHandler), plus a func that flushes and closes the file. With level "off" only the extra handlers receive records, at info level.

func ParseLevel

func ParseLevel(s string) (level slog.Level, ok bool, err error)

ParseLevel maps a config level to slog; ok is false for "off".

func ParseTraceparent

func ParseTraceparent(tp string) (trace.SpanContext, bool)

ParseTraceparent decodes a W3C traceparent into a remote span context.

func RecordApproval

func RecordApproval(ctx context.Context, kind, decision string)

RecordApproval counts an approval decision by action kind and how it was made (its first word: user, rule-allow, mode-bypass, hook-deny…).

func RecordSession

func RecordSession(ctx context.Context, reason string)

RecordSession counts a session started (reason: startup, new, resume…).

func RecordTokens

func RecordTokens(ctx context.Context, model string, input, output, cached int64, costUSD float64, priced bool)

RecordTokens counts a model call's tokens (input, output, cached input) and its estimated cost (when priced).

func RecordToolCall

func RecordToolCall(ctx context.Context, tool, outcome string)

RecordToolCall counts a tool call: ok or error.

func RecordTurn

func RecordTurn(ctx context.Context, agent, outcome string)

RecordTurn counts a prompt run by agent: ok, error, blocked or limit.

func SecretRedactor added in v0.3.1

func SecretRedactor(cfg *config.Config) *redact.Redactor

SecretRedactor masks configured credentials, the values of scrubbed environment variables, and MCP servers' environment and header values (tokens, often) in audit entries, logs and telemetry.

func Start

func Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)

Start begins a span on the current global provider (a no-op with telemetry off). Attributes must not carry content (prompts, file text, arguments): use sizes, names and outcomes.

func StartProcess added in v0.3.1

func StartProcess(ctx context.Context, cfg *config.Config, version string, warn func(string)) func()

StartProcess opens the diagnostic log and, when enabled, OpenTelemetry export, and makes the log the slog default. The returned func flushes both. Failures only disable the affected part: diagnostics must never stop a session.

func StartWith

func StartWith(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)

StartWith is Start with arbitrary span options, e.g. trace.WithLinks.

func Traceparent

func Traceparent(span trace.Span) string

Traceparent encodes a span's context in W3C Trace Context form, or "" when the span is not recording a real trace (telemetry off).

Types

type LogAttr

type LogAttr struct{ Key, Value string }

LogAttr is one field of a log record.

type LogEntry

type LogEntry struct {
	Time  time.Time
	Level string // DEBUG, INFO, WARN or ERROR
	Msg   string
	// Attrs are the record's other fields, in the file's order, values as
	// text (strings bare, the rest as JSON).
	Attrs []LogAttr
}

LogEntry is one record of a log file.

type LogPage

type LogPage struct {
	Day  string
	Path string
	// Entries match the query, newest first, at most Limit of them.
	Entries []LogEntry
	// Matched counts every record that matched (more than Entries when
	// Limit cut them).
	Matched int
}

LogPage is what ReadLog found.

func ReadLog

func ReadLog(dir string, q LogQuery) (LogPage, error)

ReadLog reads the records of a day's log in dir that match q, newest first. Lines that aren't records (a torn last line) are skipped.

type LogQuery

type LogQuery struct {
	// Day is YYYY-MM-DD; "" is the newest day there is.
	Day string
	// MinLevel keeps records at this level and above ("" or DEBUG: all).
	MinLevel string
	// Text keeps records containing every word of it, in any field,
	// ignoring case.
	Text string
	// Limit is the most records returned, newest first (0: 500).
	Limit int
}

LogQuery picks records from a day's log.

type Telemetry

type Telemetry struct {
	// contains filtered or unexported fields
}

Telemetry owns the OpenTelemetry providers. A nil *Telemetry is valid and does nothing.

func NewTelemetry

func NewTelemetry(cfg config.TelemetryConfig, version string, r *redact.Redactor, spanExp sdktrace.SpanExporter, logExp sdklog.Exporter) *Telemetry

NewTelemetry installs global providers exporting through the given exporters (content filtering and masking still apply). StartTelemetry uses it with OTLP/HTTP exporters; embedders and tests can pass their own. Install telemetry once per process: the ADK binds its tracer to the first global provider, so spans from later providers would miss the ADK's.

func StartTelemetry

func StartTelemetry(ctx context.Context, cfg config.TelemetryConfig, version string, r *redact.Redactor) (*Telemetry, error)

StartTelemetry installs global trace and log providers exporting over OTLP/HTTP when cfg.Enabled; otherwise it returns nil. Export happens on the SDK's batch goroutines, so instrumented code only enqueues.

func (*Telemetry) Flush

func (t *Telemetry) Flush(ctx context.Context) error

Flush exports everything recorded so far.

func (*Telemetry) LogHandler

func (t *Telemetry) LogHandler() slog.Handler

LogHandler returns an slog handler that sends records to the collector, or nil when telemetry is off.

func (*Telemetry) Shutdown

func (t *Telemetry) Shutdown(ctx context.Context) error

Shutdown flushes pending spans and logs, waiting at most a few seconds.

func (*Telemetry) StartMetrics

func (t *Telemetry) StartMetrics(reader sdkmetric.Reader)

StartMetrics installs the global meter provider, reading through reader (a periodic OTLP export, or a manual reader in tests). Call it before anything is recorded: instruments bind to the provider in place at their first use.

Jump to

Keyboard shortcuts

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