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
- Variables
- func DeleteLogDay(dir, day string, now time.Time) error
- func End(span trace.Span, err error)
- func LogDays(dir string) ([]string, error)
- func Logger(name string) *slog.Logger
- func OpenLog(cfg config.LogConfig, r *redact.Redactor, extra ...slog.Handler) (*slog.Logger, io.Closer, error)
- func ParseLevel(s string) (level slog.Level, ok bool, err error)
- func ParseTraceparent(tp string) (trace.SpanContext, bool)
- func RecordApproval(ctx context.Context, kind, decision string)
- func RecordSession(ctx context.Context, reason string)
- func RecordTokens(ctx context.Context, model string, input, output, cached int64, ...)
- func RecordToolCall(ctx context.Context, tool, outcome string)
- func RecordTurn(ctx context.Context, agent, outcome string)
- func RememberSecrets(cfg *config.Config)
- func SecretRedactor(cfg *config.Config) *redact.Redactor
- func Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- func StartProcess(ctx context.Context, cfg *config.Config, version string, warn func(string)) func()
- func StartWith(ctx context.Context, name string, opts ...trace.SpanStartOption) (context.Context, trace.Span)
- func Traceparent(span trace.Span) string
- type LogAttr
- type LogEntry
- type LogPage
- type LogQuery
- type Telemetry
Constants ¶
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.
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.
const LoggerKey = "logger"
LoggerKey is the attribute naming a record's logger ("gateway"), which log.levels sets a level for (LOG-01b).
const ServiceName = "blitz"
ServiceName identifies Blitz in exported telemetry.
Variables ¶
var ErrBadLogDay = errors.New("not a log day: use YYYY-MM-DD")
ErrBadLogDay reports a day that isn't a date (YYYY-MM-DD).
var ErrLogInUse = errors.New("today's log is still being written")
ErrLogInUse is returned when deleting the log being written: today's.
var ErrNoLogDay = errors.New("no log for that day")
ErrNoLogDay reports a day dir has no log for.
Functions ¶
func DeleteLogDay ¶
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 Logger ¶ added in v0.9.12
Logger is the default logger named name, a dotted path ("gateway", "gateway.token"): its records carry the name, and log.levels can set its level apart from the rest. It's looked up at each call, so a package can keep a name before the service has opened its log.
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. Each logger (Logger) writes at its level in log.levels, else at log.level. With every level "off" there is no file; with log.level "off", the extra handlers receive the rest at info.
func ParseLevel ¶
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 ¶
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 ¶
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 ¶
RecordToolCall counts a tool call: ok or error.
func RecordTurn ¶
RecordTurn counts a prompt run by agent: ok, error, blocked or limit.
func RememberSecrets ¶ added in v0.9.1
RememberSecrets has every redactor mask cfg's credentials from now on (redact.Remember): the process's diagnostic log and telemetry, whose redactor is built from the global settings when the service starts, included. Workspaces call it as they open and as their settings change, so keys set later in Settings, a workspace's own keys, and the tokens of MCP servers added later or defined in a workspace are masked there too. Of the headers' and MCP servers' env values, only those that look secret are remembered (an Authorization header, a name like a key or token): the remembered values are also masked in text shown as it is.
func SecretRedactor ¶ added in v0.3.1
SecretRedactor masks the settings' credentials (config.EachSecret: the providers' API keys, Azure's included, the web search key, the api gateway's and http hooks' headers, MCP servers' environment and header values) and the values of scrubbed environment variables 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 ¶
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 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.
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 ¶
Flush exports everything recorded so far, without shutting down. The service uses Shutdown; Flush is for tests (here and in pkg/engine/runtime, whose in-memory exporters Shutdown would clear) and embedders of NewTelemetry.
func (*Telemetry) LogHandler ¶
LogHandler returns an slog handler that sends records to the collector, or nil when telemetry is off.
func (*Telemetry) Shutdown ¶
Shutdown flushes pending spans and logs, waiting at most a few seconds.
func (*Telemetry) StartMetrics ¶
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.