analytics

package
v0.1.113 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Overview

Package analytics is the official, opt-in error reporting and product analytics pack. Server panics and 500s, frontend errors and named events land in Postgres (app_error, app_event) through a bounded queue, and optionally go out as OTLP log records to any collector. Nothing leaves the app unless ANALYTICS_OTLP_URL is set; no IP addresses are stored.

Index

Constants

View Source
const Tables = `` /* 857-byte string literal not displayed */

Tables is the DDL matching the schema fragment `lidza pack add analytics` appends to schema.lidza (models AppError and AppEvent). Tests create it.

Variables

This section is empty.

Functions

func Fingerprint

func Fingerprint(e report.Error) string

Fingerprint groups the same failure: source, route and the first line of the message.

func Handler

func Handler() http.Handler

Handler accepts frontend reports: POST /api/v1/analytics/errors with a report.Error, POST /api/v1/analytics/events with an Event. Register with r.Handle("POST /api/v1/analytics/{kind}", analytics.Handler()). Bodies are capped at 32 KB and clients are rate limited.

func Pack

func Pack() lidza.Pack

Pack returns the pack for packs.go; it needs the db pack started first.

Types

type Analytics

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

Analytics is the running pack.

func From

func From(ctx context.Context) *Analytics

From returns the pack from a request context.

func New

func New(cfg Config, pool *pgxpool.Pool) *Analytics

New builds the pack outside the lifecycle (tests).

func (*Analytics) Name

func (a *Analytics) Name() string

Name implements lidza.Pack.

func (*Analytics) Report

func (a *Analytics) Report(ctx context.Context, e report.Error)

Report implements report.Reporter: queue and return.

func (*Analytics) Run

func (a *Analytics) Run()

Run starts the writer goroutine.

func (*Analytics) Start

func (a *Analytics) Start(ctx context.Context, s *lidza.Services) error

Start reads the configuration, takes the pool, applies retention and starts the writer.

func (*Analytics) Stop

func (a *Analytics) Stop(ctx context.Context) error

Stop flushes what is queued and stops the writer.

func (*Analytics) TelemetryStats

func (a *Analytics) TelemetryStats() map[string]float64

TelemetryStats reports the queue to /metrics.

func (*Analytics) Track

func (a *Analytics) Track(ctx context.Context, name string, props map[string]any)

Track records a product event from the server side.

type Config

type Config struct {
	// OTLPURL is an OTLP/HTTP logs endpoint (http://collector:4318/v1/logs).
	// Empty keeps everything in Postgres.
	OTLPURL string `env:"ANALYTICS_OTLP_URL"`
	// Queue bounds records waiting to be written; the rest are dropped and
	// counted.
	Queue int `env:"ANALYTICS_QUEUE" default:"1024"`
	// ClientRPS limits what one client may post per second.
	ClientRPS float64 `env:"ANALYTICS_CLIENT_RPS" default:"5"`
	// Retention drops rows older than this on start; 0 keeps them.
	Retention time.Duration `env:"ANALYTICS_RETENTION" default:"720h"`
}

Config comes from the environment.

type Event

type Event struct {
	Name      string         `json:"name"`
	Props     map[string]any `json:"props,omitempty"`
	URL       string         `json:"url,omitempty"`
	SessionID string         `json:"sessionId,omitempty"`
	UserID    string         `json:"userId,omitempty"`
	At        time.Time      `json:"at"`
}

Event is a named product event.

type StoredError

type StoredError struct {
	ID          string    `json:"id"`
	Source      string    `json:"source"`
	Message     string    `json:"message"`
	Stack       *string   `json:"stack,omitempty"`
	Route       *string   `json:"route,omitempty"`
	Method      *string   `json:"method,omitempty"`
	URL         *string   `json:"url,omitempty"`
	RequestID   *string   `json:"requestId,omitempty"`
	UserID      *string   `json:"userId,omitempty"`
	Fingerprint string    `json:"fingerprint"`
	CreatedAt   time.Time `json:"createdAt"`
}

StoredError is a row of app_error.

func Recent

func Recent(ctx context.Context, pool *pgxpool.Pool, limit int) ([]StoredError, error)

Recent returns the latest errors, newest first, for the MCP tool and dashboards.

Jump to

Keyboard shortcuts

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