telemetry

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: AGPL-3.0 Imports: 38 Imported by: 0

Documentation

Overview

Package telemetry sets up logging and metrics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAttrs added in v0.3.0

func AddAttrs(ctx context.Context, attrs ...slog.Attr)

AddAttrs adds attrs to the attributes ctx carries (WithAttrs) in place, so that what is logged with the context WithAttrs returned has them as well: how a request learns its tenant once the session is known, for what its outer layers log about it. It does nothing when ctx carries none.

func AddressTag added in v0.3.0

func AddressTag(key []byte, addr string) string

AddressTag is what stands for addr in a log that redacts addresses: the first 8 hex digits of its HMAC-SHA256 under key, lowercased first, so the same address gets the same tag whatever its case, and no tag can be turned back into its address without key.

func LogKey added in v0.3.0

func LogKey(dataDir string) ([]byte, error)

LogKey returns the key of AddressTag kept in dataDir, generating it the first time. It is kept apart from the master key so that tags stay the same when that key is replaced or retired (the hosted build keeps keys in a key service); it opens nothing, so losing it only changes every address's tag.

func LogKeyFile added in v0.3.0

func LogKeyFile(dataDir string) string

LogKeyFile is where the key of AddressTag is kept in dataDir.

func LogPanic added in v0.3.0

func LogPanic(ctx context.Context, msg string, v any, args ...any)

LogPanic logs v, what recover returned, at error level under msg, with args, the panic as "panic" and the stack of the goroutine that panicked as "stack". Call it from the deferred function that recovered.

func NewLogger

func NewLogger(w io.Writer, level string, addressKey []byte, errs ...io.Writer) *slog.Logger

NewLogger returns a JSON logger that writes every record at level and above to w, and drops credentials and email bodies. With addressKey, every email address in a line, in its message as in its attributes, is written as its AddressTag under that key instead; nil keeps addresses. Each of errs gets, as well, every record at error level and above, as the one JSON line w gets for it (whatever level is): the same redaction, so it never holds more than the log does. A writer in errs must not block: the code that logged waits for it.

A record logged with a context that carries attributes (WithAttrs, AddAttrs) has them too, except those whose key the record has already.

func ReadLogKey added in v0.3.0

func ReadLogKey(dataDir string) ([]byte, error)

ReadLogKey reads the key of AddressTag from dataDir; fs.ErrNotExist when there is none.

func WithAttrs added in v0.3.0

func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

WithAttrs returns ctx with attrs added to every record logged with it (slog.InfoContext and the like, through NewLogger's logger), such as the mailbox or the request route an error happened in. They override attributes of the same key that ctx carries already.

Types

type Export added in v0.3.0

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

Export sends the daemon's metrics and log to an OpenTelemetry receiver (MAI-239). The metrics are the Prometheus registry's (Metrics), read and sent by the SDK's periodic reader; the log lines are those the log writes, redaction included, handed to Write. Nothing about it ever waits on the receiver: Write only queues, and a receiver that is slow or down costs dropped lines, counted in mailrules_otel_log_records_dropped_total.

func NewExport added in v0.3.0

func NewExport(ctx context.Context, cfg ExportConfig, m *Metrics) (*Export, error)

NewExport starts the metrics' periodic export and returns the Export whose Run sends the log lines Write queues. It connects to nothing before the first export.

func (*Export) Run added in v0.3.0

func (x *Export) Run(ctx context.Context)

Run sends the queued log lines until ctx is done, then, for up to exportDrainTime, the metrics' last reading and the lines still queued. Lines written after it returns are never sent.

func (*Export) Write added in v0.3.0

func (x *Export) Write(p []byte) (int, error)

Write queues a copy of the log line p. It never blocks and never fails: with logQueue lines waiting, p is dropped and counted.

type ExportConfig added in v0.3.0

type ExportConfig struct {
	// Endpoint is the receiver's base URL: metrics go to /v1/metrics under it, log lines
	// to /v1/logs (OTLP/HTTP, protobuf).
	Endpoint string
	// Headers go with every export; they hold the receiver's key, so they are never logged.
	Headers     map[string]string
	ServiceName string
	Version     string
	// Environment is deployment.environment.name; empty sends none.
	Environment string
}

ExportConfig says where the OpenTelemetry export sends, and as what.

type Metrics

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

Metrics is the daemon's Prometheus registry. This file is the only place that imports the Prometheus client, with export.go, which hands the registry to OpenTelemetry: the HTTP layer reports through ObserveHTTP and serves Handler. A nil *Metrics records nothing.

func NewMetrics

func NewMetrics(version string) *Metrics

NewMetrics returns a registry with the Go runtime and process collectors, the build version and the HTTP request metrics.

func (*Metrics) DroppedError added in v0.3.0

func (m *Metrics) DroppedError(module string)

DroppedError counts a logged error module was not handed: too many were waiting for it.

func (*Metrics) Handler

func (m *Metrics) Handler() http.Handler

Handler serves the registry in the Prometheus text format.

func (*Metrics) ObserveHTTP

func (m *Metrics) ObserveHTTP(method, route string, status int, took time.Duration)

ObserveHTTP records one served request. route is the matched route pattern, never the raw path, so ids do not become label values.

Jump to

Keyboard shortcuts

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