Documentation
¶
Overview ¶
Package analytics connects an app to Trakk (github.com/trick77/trakk), the self-hosted web traffic observability: the origin the app's pages load Trakk's tracker from, and the feed that reports every request the app serves, so clients that never run JavaScript (crawlers, probes, API scripts) show up too. The contract is Trakk's INTEGRATION.md. Trakk is optional: down, slow or misconfigured, it never touches a request.
Index ¶
- Constants
- func Origin(raw string) (string, error)
- func RecordRoute(next http.Handler, catchAll ...string) http.Handler
- func Recorder(next http.Handler) http.Handler
- func SetAccount(ctx context.Context, id string)
- func SetError(ctx context.Context, code string)
- func SetOp(ctx context.Context, op string)
- type Config
- type Emitter
- type Event
- type Feed
- type Settings
- type Stats
Constants ¶
const ( ScriptPath = "/trakk.js" FeedPath = "/api/feed" )
The paths Trakk serves, fixed by its contract.
Variables ¶
This section is empty.
Functions ¶
func Origin ¶
Origin is raw (ANALYTICS_URL) reduced to scheme://host[:port], the one form that goes into the CSP header and the script tag. Anything that could smuggle more into either (a path, a query, userinfo, whitespace) is refused, not cleaned up, and so is any host but a DNS name or an IP literal: a CSP wildcard (https://*, https://*.example) would allow scripts from hosts Trakk does not own. The errors never echo raw: it may hold a password.
func RecordRoute ¶
RecordRoute notes the pattern the mux inside matched (r.Pattern, which the mux sets on the request it is handed). catchAll names the mux's fallback patterns ("GET /", "/"): a request they answer is no route of the app's, so its route stays empty and Trakk names the path by the site's rules. Without Recorder outside it only passes the request on. Panics too: the deferred copy runs before a Recover outside answers.
func Recorder ¶
Recorder puts the request's record into its context and counts the response body. It goes outside the app's access log, so the access log's request carries the record and EventFrom can read it. Mount it only with a feed: without one there is nothing to record, and the response writer stays the one net/http handed in.
func SetAccount ¶
SetAccount names the account the request acted as: an opaque id, never an email or a name. A request without Recorder outside ignores it. Call it, like SetOp and SetError, before the handler returns: the access log reads the record then.
func SetError ¶
SetError says the request failed where its status does not (a JSON-RPC error answered 200): the app's own code, from a fixed list like SetOp's. The last one set counts; "" takes it back. A request without Recorder outside ignores it.
func SetOp ¶
SetOp names what the request did inside its route, where one route serves several things (an MCP tool): a name from the app's own fixed list, never text a client chose, which Trakk would list. ASCII letters, digits and _ . / : - only (Trakk stores the rest as _), at most 64 bytes. A request without Recorder outside ignores it.
Types ¶
type Emitter ¶
type Emitter struct {
// contains filtered or unexported fields
}
Emitter batches events to Trakk's feed from one background goroutine. Enqueue never blocks. Events wait in one bounded backlog, oldest first out. A batch that failed because Trakk is away (retryable) stays and goes again after the pause (Retry-After, else a doubling back-off), each event until maxAge. One that was refused is dropped: a 400 or 413 is that batch's fault, anything else (401, a firewall's 403) pauses the feed too, the events behind it waiting.
func (*Emitter) Enqueue ¶
Enqueue hands ev to the sender without blocking: a full queue, a stopped emitter, or an event Trakk would refuse (an ip that is no address) drops it and counts the drop. What is queued is a sanitized copy: within Trakk's field limits, sharing no memory with the request. A nil Emitter takes nothing.
type Event ¶
type Event struct {
T int64 `json:"t"` // request start, epoch milliseconds
IP string `json:"ip"`
UA string `json:"ua"`
Method string `json:"method"`
Path string `json:"path"`
Route string `json:"route"`
Status int `json:"status"`
DurMS int64 `json:"dur_ms"`
Bytes int64 `json:"bytes"`
Ref string `json:"ref"`
Lang string `json:"lang"`
SFSite string `json:"sf_site"`
SFMode string `json:"sf_mode"`
SFDest string `json:"sf_dest"`
CHUA string `json:"ch_ua"`
SigAgent string `json:"sig_agent"` // Signature-Agent (Web Bot Auth), as sent
Account string `json:"account"` // an opaque account id, never an email or a name; "" for none
Op string `json:"op"` // what the request did inside its route (an MCP tool); "" where the route says it
Err string `json:"err"` // why it failed where the status does not say so; "" for none
}
Event is one served request, exactly the fields Trakk's feed takes and nothing else: no raw header, query string, cookie or body leaves the app.
func EventFrom ¶
EventFrom maps a finished request to Trakk's fields, and only those: the query, cookies, credentials and every other header stay in the app. ip is the client as the app resolves it (after its trusted proxies), status and dur what the access log measured; route, bytes, account, operation and failure come from Recorder's record.
type Feed ¶
type Feed interface {
Enqueue(Event)
}
Feed takes one event per served request; *Emitter is the real one. Enqueue must not block.
type Settings ¶
type Settings struct {
Origin string // Trakk's origin: the tracker's script tag and the CSP; "" is off
Feed *Config // the server feed; nil is off
}
Settings is what the configuration turns on. The zero value is Trakk off: no script tag, the app's own CSP, no feed.
func Resolve ¶
Resolve reads ANALYTICS_URL (rawURL), ANALYTICS_FEED_KEY and ANALYTICS_SITE; defaultSite (the app's public host, "" for none) names the site when ANALYTICS_SITE is unset. A URL alone turns on the tracker, a key adds the feed. A value that does not work turns the part it belongs to off and comes back as a warning: Trakk is optional, it never stops the app from starting.
type Stats ¶
type Stats struct {
Sent uint64 // accepted by Trakk (2xx), on the first send or a later one
Dropped uint64 // never sent: queue or backlog full, too old, after shutdown, or no valid ip
Failed uint64 // sent and given up on: refused, or still failing when its age, the backlog's bound or shutdown ends it
}
Stats are the emitter's counters since start, in events.