Documentation
¶
Overview ¶
Package pagerless reports errors, request health, heartbeats and deploys from a Go service to Pagerless.
func main() {
pagerless.Init("") // reads PAGERLESS_KEY
defer pagerless.Close(context.Background())
mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", getOrder)
http.ListenAndServe(":8080", pagerless.Middleware(mux))
}
The SDK groups repeats in-process (a new problem is sent at once, repeats are counted per 10 s window), scrubs secrets and personal data before anything leaves the process, never blocks the caller, and never panics into user code. When the endpoint is down it backs off and drops the oldest data first once its bounded buffer is full.
Index ¶
- Constants
- Variables
- func Capture(err error, opts ...CaptureOption)
- func CapturePanic(v any, opts ...CaptureOption)
- func CheckIn(ctx context.Context, slug string, opts ...CheckInOption) error
- func CheckInSlug(name string) string
- func Close(ctx context.Context) error
- func Deploy(release string)
- func Event(title string, severity Severity, tags map[string]string)
- func Fatal(err error, opts ...CaptureOption)
- func Init(key string, opts ...Option) error
- func Middleware(h http.Handler) http.Handler
- func MiddlewareRoutes(h http.Handler, route RouteFunc) http.Handler
- func Monitor(ctx context.Context, slug string, s Schedule, fn func(context.Context) error) error
- func RecordRequest(route string, status int, latency time.Duration)
- func Recovered(v any, r *http.Request, route string)
- func RouteFor(method, path string, status int) string
- func Scrub(s string) string
- func SlogHandler(next slog.Handler) slog.Handler
- type CaptureOption
- type CheckInOption
- type CheckInStatus
- type Client
- func (c *Client) Capture(err error, opts ...CaptureOption)
- func (c *Client) CapturePanic(v any, opts ...CaptureOption)
- func (c *Client) CheckIn(ctx context.Context, slug string, opts ...CheckInOption) (err error)
- func (c *Client) Close(ctx context.Context) error
- func (c *Client) Deploy(release string)
- func (c *Client) Event(title string, severity Severity, tags map[string]string)
- func (c *Client) Fatal(err error, opts ...CaptureOption)
- func (c *Client) Middleware(h http.Handler) http.Handler
- func (c *Client) MiddlewareRoutes(h http.Handler, route RouteFunc) http.Handler
- func (c *Client) Monitor(ctx context.Context, slug string, s Schedule, fn func(context.Context) error) error
- func (c *Client) RecordRequest(route string, status int, latency time.Duration)
- func (c *Client) Recovered(v any, r *http.Request, route string)
- func (c *Client) SlogHandler(next slog.Handler) slog.Handler
- type Option
- func WithEndpoint(url string) Option
- func WithEnv(env string) Option
- func WithHTTPClient(hc *http.Client) Option
- func WithHeartbeat(d time.Duration) Option
- func WithLogger(l *slog.Logger) Option
- func WithMaxQueue(n int) Option
- func WithRelease(release string) Option
- func WithService(name string) Option
- func WithWindow(d time.Duration) Option
- type RouteFunc
- type Schedule
- type Severity
Constants ¶
const DefaultEndpoint = "https://events.pagerless.dev/v1/events" // the ingest API's public address
DefaultEndpoint is the ingest event API.
Variables ¶
var ErrBadSlug = errors.New("pagerless: check-in name must be 1-64 letters, digits, '.', '_' or '-'")
ErrBadSlug is returned for a job name that isn't 1-64 letters, digits, '.', '_' or '-'.
var ErrNoKey = errors.New("pagerless: no key (pass one to Init or set PAGERLESS_KEY); reporting is off")
ErrNoKey is returned by Init and New when no key is given and PAGERLESS_KEY is unset. The returned client is still usable; it records nothing.
Functions ¶
func Capture ¶
func Capture(err error, opts ...CaptureOption)
Capture reports err on the default client.
func CapturePanic ¶ added in v0.2.0
func CapturePanic(v any, opts ...CaptureOption)
CapturePanic reports a recovered panic on the default client.
func CheckIn ¶
func CheckIn(ctx context.Context, slug string, opts ...CheckInOption) error
CheckIn reports one run on the default client.
func CheckInSlug ¶ added in v0.2.0
CheckInSlug turns a job or schedule name into a check-in name the way `npx pagerless connect` does ("Nightly Report" → "nightly-report"), so code and connect agree on the name.
func Fatal ¶ added in v0.2.0
func Fatal(err error, opts ...CaptureOption)
Fatal reports err as the reason the process is exiting, delivers what's buffered (up to 3 s) and exits with status 1. Use it where you'd call log.Fatal: an exit right after start counts toward a crash loop in Pagerless, which a plain os.Exit never reports.
if err := srv.ListenAndServe(); err != nil {
pagerless.Fatal(err)
}
func Init ¶
Init starts the default client. key falls back to PAGERLESS_KEY and the endpoint to PAGERLESS_ENDPOINT. Without a key, reporting is off and ErrNoKey is returned; the rest of the API keeps working as a no-op. Calling Init again replaces (and closes) the previous client.
func Middleware ¶
Middleware wraps h to record status and latency per route and to report panics. Wrap the outermost handler (usually your *http.ServeMux) so the matched Go 1.22+ pattern, e.g. "GET /orders/{id}", is the route. A panic is reported and then re-panicked, so net/http (or your own recovery) still handles it as before. Uses the default client at request time, so it can be installed before Init.
func MiddlewareRoutes ¶ added in v0.2.0
MiddlewareRoutes is Middleware for routers that keep the matched route themselves: route names it. The adapters (github.com/lcbasu/pagerless-go/chi, .../gorillamux) are built on it.
func RecordRequest ¶ added in v0.2.0
RecordRequest records a request on the default client.
func Recovered ¶
Recovered reports a panic that a framework's own recovery already caught. Gin's Recovery, Echo's Recover and Chi's Recoverer stop the panic before it reaches Middleware, so call this from their hook. route is the framework's matched pattern if it has one (Gin: c.FullPath(), e.g. "/orders/:id"); "" falls back to the request path with IDs collapsed. It only reports: writing the 500 stays with the framework. Call it inside the recovery hook (while the panic is being handled) so the stack points at the panic site.
r.Use(gin.CustomRecovery(func(c *gin.Context, v any) {
pagerless.Recovered(v, c.Request, c.FullPath())
c.AbortWithStatus(http.StatusInternalServerError)
}))
func RouteFor ¶ added in v0.2.0
RouteFor names a request no router template matched: the method and the path with ID-like segments collapsed ("GET /files/:id"), or "GET (unmatched)" for a 404, so routes stay few.
func Scrub ¶
Scrub removes secrets and personal data from s: URL credentials, JWTs, bearer tokens, vendor API keys, secret key=value pairs, emails, card numbers (Luhn-checked) and phone numbers.
func SlogHandler ¶ added in v0.2.0
SlogHandler wraps next so every record at Error level or above is also captured: the record's error attribute (any attr holding an error) gives the type, the message says what failed. All records still go to next unchanged. Uses the default client at log time.
slog.SetDefault(slog.New(pagerless.SlogHandler(slog.NewJSONHandler(os.Stderr, nil))))
Don't wrap slog.Default().Handler() while it is still Go's built-in default handler: setting that back as the default makes the log package loop into itself. Wrap the handler you build.
Types ¶
type CaptureOption ¶
type CaptureOption func(*wireEvent)
CaptureOption adds context to Capture.
func ErrorType ¶ added in v0.2.0
func ErrorType(name string) CaptureOption
ErrorType overrides the error's type name, which groups problems: use it when the type of err says less than a name the app already has (a Temporal ApplicationError's Type, say).
func Route ¶
func Route(route string) CaptureOption
Route sets the route the error happened on, e.g. "POST /checkout".
func Tags ¶
func Tags(tags map[string]string) CaptureOption
Tags attaches tags (scrubbed; values under secret-looking keys are redacted). Several Tags options add up; a later key wins.
type CheckInOption ¶
type CheckInOption func(*checkInBody)
CheckInOption sets a field of one check-in.
func Failed ¶
func Failed(reason string) CheckInOption
Failed marks the run failed, with the reason (scrubbed before sending).
func RunID ¶
func RunID(id string) CheckInOption
RunID is an idempotency key for this run: a retry with the same id counts once.
func WithSchedule ¶
func WithSchedule(s Schedule) CheckInOption
WithSchedule creates the check-in on first use and keeps it in step (unless edited in the app).
type CheckInStatus ¶
type CheckInStatus string
CheckInStatus is the outcome of one run.
const ( // CheckInOK is a run that finished fine. CheckInOK CheckInStatus = "ok" // CheckInFailed is a run that failed; Pagerless opens a problem at once. CheckInFailed CheckInStatus = "failed" )
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client reports to one Pagerless project. Use the package-level functions unless you need more than one client. All methods are safe for concurrent use and on a nil *Client.
func Default ¶ added in v0.2.0
func Default() *Client
Default returns the client Init started (nil before Init; every method is a no-op on nil). Adapters call it per request, so they can be installed before Init.
func (*Client) Capture ¶
func (c *Client) Capture(err error, opts ...CaptureOption)
Capture reports err. Repeats of the same problem (type + call site + route) are counted, not re-sent. Safe to call with a nil error (no-op).
func (*Client) CapturePanic ¶ added in v0.2.0
func (c *Client) CapturePanic(v any, opts ...CaptureOption)
CapturePanic reports a value recovered from a panic. Call it inside the deferred function that recovered it, so the stack starts at the panic site. http.ErrAbortHandler (net/http's deliberate abort) is ignored. It only reports: re-panic or write the error yourself.
func (*Client) CheckIn ¶
CheckIn reports one run of a scheduled job, at once (not batched: jobs often exit right after). It returns ErrNoKey when reporting is off and never panics.
func (*Client) Close ¶
Close flushes the current window and tries to deliver everything buffered until ctx ends. Events recorded after Close are ignored.
func (*Client) Deploy ¶
Deploy sends a deploy marker for release. The ID is derived from service, env and release, so every replica and restart of one release collapses to a single marker at ingest.
func (*Client) Event ¶
Event reports a custom event. severity is wake, tell or log; an invalid severity is sent as tell (unsure means tell, never log).
func (*Client) Fatal ¶ added in v0.2.0
func (c *Client) Fatal(err error, opts ...CaptureOption)
Fatal is the per-client version of the package-level Fatal.
func (*Client) Middleware ¶
Middleware is the per-client version of the package-level Middleware.
func (*Client) MiddlewareRoutes ¶ added in v0.2.0
MiddlewareRoutes is the per-client version of the package-level MiddlewareRoutes.
func (*Client) Monitor ¶
func (c *Client) Monitor(ctx context.Context, slug string, s Schedule, fn func(context.Context) error) error
Monitor runs fn and reports the run: ok with its run time, or failed with fn's error (or panic, which is re-raised after reporting). It returns fn's error; a check-in that couldn't be sent is logged, never returned, so it can't hide the job's own result.
func (*Client) RecordRequest ¶ added in v0.2.0
RecordRequest feeds one finished request into the current window: route is the matched template ("GET /orders/:id"), status the response code. 5xx count as failures.
type Option ¶
type Option func(*config)
Option configures Init / New.
func WithEndpoint ¶
WithEndpoint overrides the ingest URL (default PAGERLESS_ENDPOINT, then DefaultEndpoint).
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used to send batches.
func WithHeartbeat ¶
WithHeartbeat sets the heartbeat interval (default 60 s).
func WithLogger ¶
WithLogger receives SDK diagnostics (send failures, drops). Default: discarded.
func WithMaxQueue ¶
WithMaxQueue bounds how many unsent events are buffered (default 2000).
func WithRelease ¶
WithRelease sets the release (default PAGERLESS_RELEASE, then the VCS revision in the build info). A known release is sent once as a deploy marker at Init.
func WithService ¶
WithService names the service (default PAGERLESS_SERVICE, then the executable name).
func WithWindow ¶
WithWindow sets the grouping window (default 10 s).
type RouteFunc ¶ added in v0.2.0
RouteFunc names the route template a request matched ("/orders/{id}"), read after the handler ran; "" when there is none. Routers that don't set r.Pattern (chi, gorilla/mux) supply one.
type Schedule ¶
type Schedule struct {
// Name is the plain-words name for alerts ("Nightly backup"); default from the slug.
Name string
// Every is the time between runs (at least a minute).
Every time.Duration
// Cron is a 5-field cron expression ("30 3 * * *") or a macro (@daily).
Cron string
// TZ is the IANA zone for Cron; default the account's time zone, else UTC.
TZ string
// Grace is how late a run may be before it counts as missed; default a tenth of the gap,
// 5 minutes to 1 hour.
Grace time.Duration
// Level is Tell (default) or Wake. Log is treated as Tell: a job nobody hears about isn't watched.
Level Severity
}
Schedule says when a job should report. Set exactly one of Every or Cron.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
gin
module
|
|
|
Package pagerlesstest is a fake Pagerless ingest for tests: point a client at it, run the code under test, close the client, and read what would have been sent.
|
Package pagerlesstest is a fake Pagerless ingest for tests: point a client at it, run the code under test, close the client, and read what would have been sent. |
|
temporal
module
|