Documentation
¶
Overview ¶
Package observability provides shared Prometheus metrics and OpenTelemetry tracing helpers for all nSelf Go services.
profiling.go provides pprof endpoint registration with token-based auth and optional Pyroscope continuous profiling agent integration.
Index ¶
- Variables
- func AuditLog(ctx context.Context, event AuditEvent)
- func BaseLabels(cfg RegistryConfig) prometheus.Labels
- func InitTracer(cfg TracerConfig) (func(context.Context) error, error)
- func MetricsHandler(registry *prometheus.Registry) http.Handler
- func NewJSONLogger(cfg LogConfig) *slog.Logger
- func NewRegistry(cfg RegistryConfig) *prometheus.Registry
- func PprofHandler(token string) http.Handler
- func Redact(s string) string
- func RedactionFailures() int64
- func ServeMetrics(registry *prometheus.Registry, bindAddr string)
- func ServeProfiling(cfg ProfilingConfig)
- func SetDefaultLogger(cfg LogConfig)
- func TraceMiddleware(serviceName string) func(http.Handler) http.Handler
- func Tracer(name string) trace.Tracer
- func ValidateLabels(labels []string) error
- type AIMetrics
- type AuditEvent
- type CacheMetrics
- type ClawMetrics
- type DBMetrics
- type HTTPMetrics
- type LogConfig
- type MuxMetrics
- type PingMetrics
- type ProfilingConfig
- type QueueMetrics
- type RedactHandler
- type RegistryConfig
- type StackTraceHandler
- type TraceLogHandler
- type TracerConfig
Constants ¶
This section is empty.
Variables ¶
var ( // DurationBuckets for _seconds histograms (HTTP, DB, AI, cache, queue). DurationBuckets = []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10} // SizeBuckets for _bytes histograms (request/response body sizes). SizeBuckets = []float64{100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 5000000} )
SI-unit histogram buckets per spec §1.1.
Functions ¶
func AuditLog ¶
func AuditLog(ctx context.Context, event AuditEvent)
AuditLog emits a structured audit event. These events are routed by Promtail to the audit-specific Loki tenant (audit-$ENV) based on the job=audit label.
func BaseLabels ¶
func BaseLabels(cfg RegistryConfig) prometheus.Labels
BaseLabels returns the standard label set from config plus environment. Services should include these as ConstLabels or via a wrapping registerer.
func InitTracer ¶
func InitTracer(cfg TracerConfig) (func(context.Context) error, error)
InitTracer initialises the OpenTelemetry trace pipeline with an OTLP/gRPC exporter. It reads standard OTel env vars (OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_TRACES_SAMPLER_ARG, etc.) and falls back to sensible defaults. Returns a shutdown function that flushes remaining spans.
func MetricsHandler ¶
func MetricsHandler(registry *prometheus.Registry) http.Handler
MetricsHandler returns an http.Handler that serves /metrics for the given registry.
func NewJSONLogger ¶
NewJSONLogger creates a slog.Logger with JSON output on stdout that includes all 12 standard fields per the observability spec: ts, level, service, env, version, msg, trace_id, span_id, request_id, user_id, tenant, caller.
The returned logger wraps a TraceLogHandler (for trace_id/span_id injection) around a PII-redacting handler around the base slog.JSONHandler.
func NewRegistry ¶
func NewRegistry(cfg RegistryConfig) *prometheus.Registry
NewRegistry creates a *prometheus.Registry pre-configured with base labels and process/go collectors. It enforces naming conventions (_total for counters, _seconds/_bytes for histograms) and forbids user_id as a label.
func PprofHandler ¶
PprofHandler returns an http.Handler that serves pprof endpoints with token authentication. Useful for registering on an existing mux.
func Redact ¶
Redact applies PII pattern matching and replaces matches with [REDACTED].
Coverage (S12.T09): emails, phones, SSNs, IBANs, credit cards, IPv4/IPv6, MAC addresses, JWTs, and API/license/Stripe/AWS/GitHub keys via the apiKeyPrefixes table. Loopback IPs (127.0.0.1, ::1) are preserved.
COPPA + GDPR Article 9 / "special category" data (race, religion, health, minor status, sexual orientation, political opinion, biometric identifiers) is NEVER allowed into telemetry payloads by construction — the doctor check OBS-REDACT-01 enforces that callers do not send such fields at all rather than relying on textual redaction.
func RedactionFailures ¶
func RedactionFailures() int64
RedactionFailures returns the current count of redaction failures.
func ServeMetrics ¶
func ServeMetrics(registry *prometheus.Registry, bindAddr string)
ServeMetrics starts an HTTP server on bindAddr serving the /metrics endpoint for the given registry. It blocks, so call in a goroutine.
func ServeProfiling ¶
func ServeProfiling(cfg ProfilingConfig)
ServeProfiling starts a pprof HTTP server on the configured bind address. It blocks, so call in a goroutine. The server is bound to 127.0.0.1 only and is never exposed publicly via nginx.
func SetDefaultLogger ¶
func SetDefaultLogger(cfg LogConfig)
SetDefaultLogger creates and sets the global slog default logger.
func TraceMiddleware ¶
TraceMiddleware creates spans for incoming HTTP requests. Span name: "HTTP <METHOD> <route-template>".
func ValidateLabels ¶
ValidateLabels checks that no forbidden labels (e.g. user_id) are used. Returns an error if a forbidden label is found.
Types ¶
type AIMetrics ¶
type AIMetrics struct {
CallsTotal *prometheus.CounterVec
CallDuration *prometheus.HistogramVec
InputTokensTotal *prometheus.CounterVec
OutputTokensTotal *prometheus.CounterVec
CostUSDTotal *prometheus.CounterVec
RetriesTotal *prometheus.CounterVec
RateLimitHits *prometheus.CounterVec
QualityScore *prometheus.HistogramVec
ContextTokens *prometheus.HistogramVec
ConcurrentCalls *prometheus.GaugeVec
}
AIMetrics holds the 10 AI call metrics per spec §1.3.
func NewAIMetrics ¶
func NewAIMetrics(reg *prometheus.Registry) *AIMetrics
NewAIMetrics creates and registers the 10 AI metrics on the given registry.
type AuditEvent ¶
type AuditEvent struct {
Event string // e.g. "user.login", "admin.delete", "permission.denied"
Actor string // who performed the action (user ID or system identifier)
Target string // what was acted upon (resource type:id)
Metadata map[string]string // additional context
}
AuditEvent represents a structured audit log entry.
type CacheMetrics ¶
type CacheMetrics struct {
HitsTotal *prometheus.CounterVec
MissesTotal *prometheus.CounterVec
OpDuration *prometheus.HistogramVec
EvictionsTotal *prometheus.CounterVec
}
CacheMetrics holds cache metrics per spec §1.5.
func NewCacheMetrics ¶
func NewCacheMetrics(reg *prometheus.Registry) *CacheMetrics
NewCacheMetrics creates and registers cache metrics on the given registry.
type ClawMetrics ¶
type ClawMetrics struct {
MessagesTotal *prometheus.CounterVec
ExtractionsTotal *prometheus.CounterVec
ExtractionDuration *prometheus.HistogramVec
DedupRatio prometheus.Histogram
TopicClassificationConfidence prometheus.Histogram
TopicsActive prometheus.Gauge
MemoryBytes prometheus.Gauge
GraphEdgesTotal prometheus.Counter
SearchLatency *prometheus.HistogramVec
EmbeddingDim prometheus.Gauge
}
ClawMetrics holds the 10 claw-specific metrics per spec §1.7.
func NewClawMetrics ¶
func NewClawMetrics(reg *prometheus.Registry) *ClawMetrics
NewClawMetrics creates and registers claw domain metrics.
type DBMetrics ¶
type DBMetrics struct {
QueryDuration *prometheus.HistogramVec
QueriesTotal *prometheus.CounterVec
PoolInUse *prometheus.GaugeVec
PoolIdle *prometheus.GaugeVec
PoolMax *prometheus.GaugeVec
ConnWait *prometheus.HistogramVec
DeadlocksTotal *prometheus.CounterVec
}
DBMetrics holds the seven database metrics per spec §1.4.
func NewDBMetrics ¶
func NewDBMetrics(reg *prometheus.Registry) *DBMetrics
NewDBMetrics creates and registers database metrics on the given registry.
type HTTPMetrics ¶
type HTTPMetrics struct {
RequestsTotal *prometheus.CounterVec
RequestDuration *prometheus.HistogramVec
RequestSize *prometheus.HistogramVec
ResponseSize *prometheus.HistogramVec
RequestsInFlight prometheus.Gauge
// contains filtered or unexported fields
}
HTTPMetrics holds the five standard HTTP metrics per spec §1.2.
func NewHTTPMetrics ¶
func NewHTTPMetrics(reg *prometheus.Registry, service string) *HTTPMetrics
NewHTTPMetrics creates and registers the five HTTP metrics on the given registry. service is the service name label value (e.g. "claw", "ai", "mux").
func (*HTTPMetrics) Middleware ¶
Middleware returns a chi-compatible middleware that records all five HTTP metrics. Route labels use chi's route pattern (template), never raw URLs.
type LogConfig ¶
type LogConfig struct {
Service string // e.g. "claw", "ai", "mux"
Env string // "dev", "staging", "prod"
Version string // e.g. "1.0.3"
Tenant string // multi-tenant isolation key
RequestID string // per-request correlation ID
UserID string // optional, PII-gated
}
LogConfig holds configuration for the structured JSON logger.
type MuxMetrics ¶
type MuxMetrics struct {
MessagesReceived *prometheus.CounterVec
ClassificationTotal *prometheus.CounterVec
ClassificationDuration *prometheus.HistogramVec
ActionSuccess *prometheus.CounterVec
FalsePositiveTotal *prometheus.CounterVec
ProcessingDuration *prometheus.HistogramVec
}
MuxMetrics holds the 6 mux-specific metrics per spec §1.8.
func NewMuxMetrics ¶
func NewMuxMetrics(reg *prometheus.Registry) *MuxMetrics
NewMuxMetrics creates and registers mux domain metrics.
type PingMetrics ¶
type PingMetrics struct {
LicenseChecksTotal *prometheus.CounterVec
LicenseCheckDuration *prometheus.HistogramVec
PluginInstallsTotal *prometheus.CounterVec
PluginUptimeSeconds *prometheus.GaugeVec
PluginVersionInfo *prometheus.GaugeVec
}
PingMetrics holds the 5 ping_api/license metrics per spec §1.9.
func NewPingMetrics ¶
func NewPingMetrics(reg *prometheus.Registry) *PingMetrics
NewPingMetrics creates and registers ping_api metrics.
type ProfilingConfig ¶
type ProfilingConfig struct {
// BindAddr for pprof HTTP server (default 127.0.0.1:6060).
BindAddr string
// Token required in X-Profile-Token header. Empty disables auth.
Token string
// PyroscopeEnabled enables continuous profiling push to Pyroscope.
PyroscopeEnabled bool
// PyroscopeServerURL is the Pyroscope server address.
PyroscopeServerURL string
// ApplicationName identifies this service in Pyroscope.
ApplicationName string
}
ProfilingConfig configures pprof and Pyroscope integration.
func DefaultProfilingConfig ¶
func DefaultProfilingConfig() ProfilingConfig
DefaultProfilingConfig reads config from environment variables.
type QueueMetrics ¶
type QueueMetrics struct {
Depth *prometheus.GaugeVec
EnqueuedTotal *prometheus.CounterVec
CompletedTotal *prometheus.CounterVec
JobDuration *prometheus.HistogramVec
JobWait *prometheus.HistogramVec
WorkersActive *prometheus.GaugeVec
RetriedTotal *prometheus.CounterVec
DLQDepth *prometheus.GaugeVec
}
QueueMetrics holds queue metrics per spec §1.6.
func NewQueueMetrics ¶
func NewQueueMetrics(reg *prometheus.Registry) *QueueMetrics
NewQueueMetrics creates and registers queue metrics on the given registry.
type RedactHandler ¶
type RedactHandler struct {
// contains filtered or unexported fields
}
RedactHandler wraps a slog.Handler and redacts PII from string attribute values.
func NewRedactHandler ¶
func NewRedactHandler(inner slog.Handler) *RedactHandler
NewRedactHandler creates a handler that redacts PII patterns from log attributes.
type RegistryConfig ¶
type RegistryConfig struct {
Service string // e.g. "claw", "ai", "mux"
Instance string // hostname or pod name
Env string // "dev", "staging", "prod"
Version string // e.g. "1.0.3"
}
RegistryConfig configures a new metrics registry.
type StackTraceHandler ¶
type StackTraceHandler struct {
// contains filtered or unexported fields
}
StackTraceHandler is a slog.Handler that captures stack traces for warn+ levels. It wraps any inner handler.
func NewStackTraceHandler ¶
func NewStackTraceHandler(inner slog.Handler) *StackTraceHandler
NewStackTraceHandler wraps a handler to add err.stack for warn and above.
type TraceLogHandler ¶
type TraceLogHandler struct {
// contains filtered or unexported fields
}
TraceLogHandler wraps a slog.Handler to inject trace_id and span_id from the OTel span context into every log record.
func NewTraceLogHandler ¶
func NewTraceLogHandler(inner slog.Handler) *TraceLogHandler
NewTraceLogHandler creates a slog handler that injects trace context fields.
func (*TraceLogHandler) Handle ¶
Handle injects trace_id and span_id from the OTel span context, then delegates to the inner handler.
type TracerConfig ¶
TracerConfig holds configuration for the OTel tracer.