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 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 Start(ctx context.Context, name string, attrs ...attribute.KeyValue) (context.Context, trace.Span)
- 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 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 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 ¶
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 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 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) 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.