Documentation
¶
Overview ¶
Package observability gives a togo app built-in request inspection, error tracking, structured-log capture, health checks and metrics — a lightweight, app-internal Telescope/Pulse for Go.
Everything is recorded into bounded in-memory ring buffers (with a pluggable seam for a durable store) and exposed over a Go API plus REST at /api/observability/* (+ /healthz, /readyz).
o, _ := observability.FromKernel(k)
router.Use(o.Middleware) // record every request
router.Use(o.Recover) // capture panics as errors
slog.SetDefault(slog.New(o.SlogHandler(slog.LevelInfo)))
o.RegisterCheck("db", func(ctx context.Context) error { return db.PingContext(ctx) })
Index ¶
- type EndpointStat
- type ErrorEvent
- type HealthResult
- type Issue
- type LogRecord
- type Metrics
- type RequestRecord
- type Service
- func (s *Service) CaptureError(_ context.Context, err error, ctxData map[string]any)
- func (s *Service) Events(fingerprint string) []ErrorEvent
- func (s *Service) Health(ctx context.Context) HealthResult
- func (s *Service) Issues() []Issue
- func (s *Service) Logs(level string) []LogRecord
- func (s *Service) Metrics() Metrics
- func (s *Service) Middleware(next http.Handler) http.Handler
- func (s *Service) Recover(next http.Handler) http.Handler
- func (s *Service) RegisterCheck(name string, fn func(ctx context.Context) error)
- func (s *Service) Requests() []RequestRecord
- func (s *Service) SlogHandler(level slog.Level) slog.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type EndpointStat ¶
type EndpointStat struct {
Path string `json:"path"`
AvgMS float64 `json:"avg_ms"`
Count int `json:"count"`
}
EndpointStat is per-path aggregate latency.
type ErrorEvent ¶
type ErrorEvent struct {
Fingerprint string `json:"fingerprint"`
Message string `json:"message"`
Context map[string]any `json:"context,omitempty"`
Time time.Time `json:"time"`
}
ErrorEvent is a single captured error.
type HealthResult ¶
type HealthResult struct {
Status string `json:"status"` // ok | degraded
Checks map[string]string `json:"checks"`
}
HealthResult is the outcome of running the readiness checks.
type Issue ¶
type Issue struct {
Fingerprint string `json:"fingerprint"`
Title string `json:"title"`
Level string `json:"level"`
Count int `json:"count"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
}
Issue is a group of error events sharing a fingerprint.
type LogRecord ¶
type LogRecord struct {
Level string `json:"level"`
Message string `json:"message"`
Attrs map[string]any `json:"attrs,omitempty"`
Time time.Time `json:"time"`
}
LogRecord is one captured structured log line.
type Metrics ¶
type Metrics struct {
Requests int `json:"requests"`
ErrorRate float64 `json:"error_rate"`
P50MS float64 `json:"p50_ms"`
P95MS float64 `json:"p95_ms"`
Slowest []EndpointStat `json:"slowest"`
}
Metrics is a rolling summary derived from observed requests.
type RequestRecord ¶
type RequestRecord struct {
ID string `json:"id"`
Method string `json:"method"`
Path string `json:"path"`
Status int `json:"status"`
DurationMS float64 `json:"duration_ms"`
IP string `json:"ip"`
Time time.Time `json:"time"`
}
RequestRecord is one observed HTTP request.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the observability runtime stored on the kernel (k.Get("observability")).
func FromKernel ¶
FromKernel returns the observability Service.
func (*Service) CaptureError ¶
CaptureError records an error, grouping by fingerprint into an Issue.
func (*Service) Events ¶
func (s *Service) Events(fingerprint string) []ErrorEvent
Events returns the error events for an issue fingerprint.
func (*Service) Health ¶
func (s *Service) Health(ctx context.Context) HealthResult
Health runs all registered checks; status is "degraded" if any fail.
func (*Service) Metrics ¶
Metrics computes request count, error rate, p50/p95 latency and slowest paths.
func (*Service) Middleware ¶
Middleware records every request (method, path, status, duration, IP).
func (*Service) RegisterCheck ¶
RegisterCheck adds a named readiness check.
func (*Service) Requests ¶
func (s *Service) Requests() []RequestRecord
Requests returns recent observed requests (most recent last).