ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m55s
ci / test (pull_request) Successful in 13m30s
ci / build (pull_request) Successful in 19m20s
ci / image (pull_request) Successful in 5m37s
Every status page server asking for a page got it encoded anew: the snapshot was marshalled once for its checksum and again for the answer, then copied into a stream frame. A page of several thousand components is about 20 MB of JSON, so servers fetching it together, as they do when told it changed, ran both API pods out of their 512 MiB. The shared build now also encodes the page once, as JSON and gzipped, and every server asking at the same time gets those bytes. Servers ask for gzip and unpack it, over HTTP and the stream; older ones still get plain JSON. A 20 MB page shrinks to about 1 MB. Uptime ratios in the snapshot are rounded to six decimals instead of up to 18 characters, never to 100% for a component that failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
419 lines
13 KiB
Go
419 lines
13 KiB
Go
package agentapi
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"slices"
|
|
"time"
|
|
)
|
|
|
|
// Page states, from best to worst.
|
|
const (
|
|
PageOperational = "operational"
|
|
PageDegraded = "degraded"
|
|
PageOutage = "outage"
|
|
PageUnknown = "unknown"
|
|
// PageUnderMaintenance marks planned maintenance in progress.
|
|
PageUnderMaintenance = "maintenance"
|
|
)
|
|
|
|
// Theme customizes a page.
|
|
type Theme struct {
|
|
// Accent is a #rrggbb color for headings and links.
|
|
Accent string `json:"accent,omitempty"`
|
|
LogoURL string `json:"logo_url,omitempty"`
|
|
// CSS is appended to the page's styles.
|
|
CSS string `json:"css,omitempty"`
|
|
}
|
|
|
|
// Day is one bar of a component's 90-day history. Uptime is -1 without data.
|
|
type Day struct {
|
|
Date string `json:"date"`
|
|
Uptime float64 `json:"uptime"`
|
|
}
|
|
|
|
// HourBucket is one hourly summary of a component's recent 24-hour history.
|
|
// Uptime is -1 when no checks were recorded during that hour.
|
|
type HourBucket struct {
|
|
Time time.Time `json:"time"`
|
|
Uptime float64 `json:"uptime"`
|
|
AvgLatencyMS int `json:"avg_latency_ms"`
|
|
}
|
|
|
|
// ComponentRegion is the public status of one monitoring region for a
|
|
// component. Error details and checker IDs stay private.
|
|
type ComponentRegion struct {
|
|
Name string `json:"name"`
|
|
Status string `json:"status"`
|
|
LatencyMS int `json:"latency_ms,omitempty"`
|
|
}
|
|
|
|
// ComponentDetails holds the public metrics shown on a component's detail page
|
|
// when the status page opts in via ShowDetails.
|
|
type ComponentDetails struct {
|
|
SLA24h float64 `json:"sla_24h"`
|
|
SLA7d float64 `json:"sla_7d"`
|
|
SLA30d float64 `json:"sla_30d"`
|
|
SLA90d float64 `json:"sla_90d"`
|
|
Hours []HourBucket `json:"hours,omitempty"`
|
|
Regions []ComponentRegion `json:"regions,omitempty"`
|
|
}
|
|
|
|
// Component is one monitor as the public sees it. Targets, check details
|
|
// and error messages stay private.
|
|
type Component struct {
|
|
// ID identifies the component on detail URLs when ShowDetails is enabled.
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name"`
|
|
Status string `json:"status"`
|
|
Uptime float64 `json:"uptime"`
|
|
Days []Day `json:"days"`
|
|
Details *ComponentDetails `json:"details,omitempty"`
|
|
}
|
|
|
|
// Section groups components and nested child sections under a heading; a
|
|
// top-level section with an empty Name holds ungrouped components.
|
|
type Section struct {
|
|
Name string `json:"name"`
|
|
Status string `json:"status,omitempty"`
|
|
Total int `json:"total,omitempty"`
|
|
Components []Component `json:"components"`
|
|
Children []Section `json:"children,omitempty"`
|
|
}
|
|
|
|
// PageIncident is an incident of a listed component.
|
|
type PageIncident struct {
|
|
// ID identifies the incident in feeds; empty from APIs before it.
|
|
ID string `json:"id,omitempty"`
|
|
Component string `json:"component"`
|
|
StartedAt time.Time `json:"started_at"`
|
|
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
|
|
// Title names the incident; empty shows the component.
|
|
Title string `json:"title,omitempty"`
|
|
// Status is investigating, identified, monitoring or resolved, or
|
|
// custom with StatusName and StatusColor; empty while nobody said
|
|
// anything.
|
|
Status string `json:"status,omitempty"`
|
|
StatusName string `json:"status_name,omitempty"`
|
|
StatusColor string `json:"status_color,omitempty"`
|
|
// Updates are the public updates, newest first.
|
|
Updates []PageIncidentUpdate `json:"updates,omitempty"`
|
|
}
|
|
|
|
// PageIncidentUpdate is what the organization told about an incident.
|
|
type PageIncidentUpdate struct {
|
|
Status string `json:"status"`
|
|
StatusName string `json:"status_name,omitempty"`
|
|
StatusColor string `json:"status_color,omitempty"`
|
|
// Message is Markdown.
|
|
Message string `json:"message"`
|
|
At time.Time `json:"at"`
|
|
}
|
|
|
|
// PageSnapshot is everything needed to render a status page.
|
|
type PageSnapshot struct {
|
|
Slug string `json:"slug"`
|
|
Title string `json:"title"`
|
|
Description string `json:"description"`
|
|
Theme Theme `json:"theme"`
|
|
Status string `json:"status"`
|
|
ShowDetails bool `json:"show_details,omitempty"`
|
|
// Subscribe offers visitors email subscriptions to incidents.
|
|
Subscribe bool `json:"subscribe,omitempty"`
|
|
Sections []Section `json:"sections"`
|
|
Incidents []PageIncident `json:"incidents"`
|
|
GeneratedAt time.Time `json:"generated_at"`
|
|
// Legal holds the legal notice and privacy policy shown on the page.
|
|
// They travel with the snapshot so they stay available during an API
|
|
// outage, which some jurisdictions require.
|
|
Legal []LegalDoc `json:"legal,omitempty"`
|
|
// Maintenance lists occurrences of planned maintenance from shortly
|
|
// before generation to two weeks ahead. Whether one is in progress is
|
|
// decided when the page is shown, see WithMaintenance, so a cached
|
|
// snapshot stays right while the API is unreachable.
|
|
Maintenance []PageMaintenance `json:"maintenance,omitempty"`
|
|
}
|
|
|
|
// PageMaintenance is one occurrence of planned maintenance.
|
|
type PageMaintenance struct {
|
|
Title string `json:"title"`
|
|
// Description is Markdown.
|
|
Description string `json:"description,omitempty"`
|
|
Start time.Time `json:"start"`
|
|
End time.Time `json:"end"`
|
|
// Components are the names of the affected components.
|
|
Components []string `json:"components"`
|
|
}
|
|
|
|
// Summarize derives a page or group state from component states. Unknown
|
|
// components do not make a page look broken, and a single failed component is
|
|
// a partial problem: only when all fail is it an outage.
|
|
func Summarize(states []string) string {
|
|
rank := map[string]int{PageOperational: 0, PageUnknown: 0, PageDegraded: 1, PageOutage: 2}
|
|
worst, allUnknown, allDown := PageOperational, true, len(states) > 0
|
|
for _, s := range states {
|
|
if rank[s] > rank[worst] {
|
|
worst = s
|
|
}
|
|
allUnknown = allUnknown && s == PageUnknown
|
|
allDown = allDown && s == PageOutage
|
|
}
|
|
switch {
|
|
case allUnknown:
|
|
return PageUnknown
|
|
case worst == PageOutage && !allDown:
|
|
return PageDegraded
|
|
}
|
|
return worst
|
|
}
|
|
|
|
func summarizeWithMaintenance(states []string, hasMaintenance bool) string {
|
|
if len(states) == 0 {
|
|
if hasMaintenance {
|
|
return PageUnderMaintenance
|
|
}
|
|
return PageUnknown
|
|
}
|
|
status := Summarize(states)
|
|
if hasMaintenance && (status == PageOperational || status == PageUnknown) {
|
|
return PageUnderMaintenance
|
|
}
|
|
return status
|
|
}
|
|
|
|
// WithMaintenance returns s as of now: components under maintenance in
|
|
// progress show as such and the page and group states reflect it.
|
|
func (s PageSnapshot) WithMaintenance(now time.Time) PageSnapshot {
|
|
affected := map[string]bool{}
|
|
for _, m := range s.Maintenance {
|
|
if !now.Before(m.Start) && now.Before(m.End) {
|
|
for _, c := range m.Components {
|
|
affected[c] = true
|
|
}
|
|
}
|
|
}
|
|
if len(affected) == 0 {
|
|
return s
|
|
}
|
|
var allStates []string
|
|
var anyMaint bool
|
|
var apply func(src Section) (Section, []string, bool)
|
|
apply = func(src Section) (Section, []string, bool) {
|
|
dst := Section{
|
|
Name: src.Name,
|
|
Total: src.Total,
|
|
Components: make([]Component, len(src.Components)),
|
|
}
|
|
var subStates []string
|
|
subMaint := false
|
|
for i, c := range src.Components {
|
|
if affected[c.Name] {
|
|
c.Status = PageUnderMaintenance
|
|
subMaint = true
|
|
} else {
|
|
subStates = append(subStates, c.Status)
|
|
}
|
|
dst.Components[i] = c
|
|
}
|
|
if len(src.Children) > 0 {
|
|
dst.Children = make([]Section, len(src.Children))
|
|
for i, child := range src.Children {
|
|
var childStates []string
|
|
var childMaint bool
|
|
dst.Children[i], childStates, childMaint = apply(child)
|
|
subStates = append(subStates, childStates...)
|
|
subMaint = subMaint || childMaint
|
|
}
|
|
}
|
|
if dst.Name != "" {
|
|
dst.Status = summarizeWithMaintenance(subStates, subMaint)
|
|
}
|
|
return dst, subStates, subMaint
|
|
}
|
|
|
|
sections := make([]Section, len(s.Sections))
|
|
for i, sec := range s.Sections {
|
|
var states []string
|
|
var maint bool
|
|
sections[i], states, maint = apply(sec)
|
|
allStates = append(allStates, states...)
|
|
anyMaint = anyMaint || maint
|
|
}
|
|
s.Sections = sections
|
|
s.Status = summarizeWithMaintenance(allStates, anyMaint)
|
|
return s
|
|
}
|
|
|
|
// ComponentByID finds a component anywhere in s.Sections by its ID.
|
|
func (s PageSnapshot) ComponentByID(id string) (Component, bool) {
|
|
if id == "" {
|
|
return Component{}, false
|
|
}
|
|
var find func(secs []Section) (Component, bool)
|
|
find = func(secs []Section) (Component, bool) {
|
|
for _, sec := range secs {
|
|
for _, c := range sec.Components {
|
|
if c.ID == id {
|
|
return c, true
|
|
}
|
|
}
|
|
if c, ok := find(sec.Children); ok {
|
|
return c, true
|
|
}
|
|
}
|
|
return Component{}, false
|
|
}
|
|
return find(s.Sections)
|
|
}
|
|
|
|
// IncidentsForComponent returns incidents belonging to componentName.
|
|
func (s PageSnapshot) IncidentsForComponent(componentName string) []PageIncident {
|
|
var out []PageIncident
|
|
for _, inc := range s.Incidents {
|
|
if inc.Component == componentName {
|
|
out = append(out, inc)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// OpenIncidentStatus returns the newest unresolved incident of
|
|
// componentName the organization gave a status, if any.
|
|
func (s PageSnapshot) OpenIncidentStatus(componentName string) (PageIncident, bool) {
|
|
var found PageIncident
|
|
ok := false
|
|
for _, inc := range s.Incidents {
|
|
if inc.Component != componentName || inc.ResolvedAt != nil || inc.Status == "" {
|
|
continue
|
|
}
|
|
if !ok || inc.StartedAt.After(found.StartedAt) {
|
|
found, ok = inc, true
|
|
}
|
|
}
|
|
return found, ok
|
|
}
|
|
|
|
// MaintenanceForComponent splits planned maintenance affecting componentName
|
|
// into ongoing and upcoming occurrences at now.
|
|
func (s PageSnapshot) MaintenanceForComponent(componentName string, now time.Time) (ongoing, upcoming []PageMaintenance) {
|
|
for _, m := range s.Maintenance {
|
|
if !slices.Contains(m.Components, componentName) {
|
|
continue
|
|
}
|
|
switch {
|
|
case !now.Before(m.Start) && now.Before(m.End):
|
|
ongoing = append(ongoing, m)
|
|
case now.Before(m.Start):
|
|
upcoming = append(upcoming, m)
|
|
}
|
|
}
|
|
return ongoing, upcoming
|
|
}
|
|
|
|
// Ongoing returns occurrences in progress at now.
|
|
func (s PageSnapshot) Ongoing(now time.Time) []PageMaintenance {
|
|
var out []PageMaintenance
|
|
for _, m := range s.Maintenance {
|
|
if !now.Before(m.Start) && now.Before(m.End) {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Upcoming returns occurrences that have not started at now.
|
|
func (s PageSnapshot) Upcoming(now time.Time) []PageMaintenance {
|
|
var out []PageMaintenance
|
|
for _, m := range s.Maintenance {
|
|
if now.Before(m.Start) {
|
|
out = append(out, m)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// LegalDoc is a legal notice ("imprint") or privacy policy ("privacy"):
|
|
// a link to a page elsewhere or Markdown to render.
|
|
type LegalDoc struct {
|
|
Kind string `json:"kind"`
|
|
URL string `json:"url,omitempty"`
|
|
Markdown string `json:"markdown,omitempty"`
|
|
}
|
|
|
|
// LegalDocByKind returns the document of kind, if any.
|
|
func (s PageSnapshot) LegalDocByKind(kind string) (LegalDoc, bool) {
|
|
for _, d := range s.Legal {
|
|
if d.Kind == kind {
|
|
return d, true
|
|
}
|
|
}
|
|
return LegalDoc{}, false
|
|
}
|
|
|
|
// PageRef names a page a status page server serves: by Slug on its own
|
|
// hosts and, when Host is set, by that verified custom domain.
|
|
type PageRef struct {
|
|
Slug string `json:"slug"`
|
|
Host string `json:"host,omitempty"`
|
|
}
|
|
|
|
// PageIndex lists the pages a status page server serves.
|
|
type PageIndex struct {
|
|
Pages []PageRef `json:"pages"`
|
|
// HomeURL is the platform's home page, which the server links from
|
|
// its error page; empty for none.
|
|
HomeURL string `json:"home_url,omitempty"`
|
|
}
|
|
|
|
// Pages lists every page this status page server serves, so it can keep
|
|
// them all cached before anyone visits.
|
|
func (c *Client) Pages(ctx context.Context) (PageIndex, error) {
|
|
var out PageIndex
|
|
err := c.do(ctx, http.MethodGet, "/pages/index", nil, &out)
|
|
return out, err
|
|
}
|
|
|
|
// ErrNotFound means no published page matches.
|
|
var ErrNotFound = errors.New("agentapi: not found")
|
|
|
|
// ErrNotModified means the page still matches the checksum sent.
|
|
var ErrNotModified = errors.New("agentapi: not modified")
|
|
|
|
// Checksum identifies the content of s, leaving out when it was generated.
|
|
func (s PageSnapshot) Checksum() string {
|
|
s.GeneratedAt = time.Time{}
|
|
raw, _ := json.Marshal(s)
|
|
sum := sha256.Sum256(raw)
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
// Page fetches the published page for a custom domain host, or for slug if
|
|
// host is empty.
|
|
func (c *Client) Page(ctx context.Context, host, slug string) (PageSnapshot, error) {
|
|
return c.PageIfChanged(ctx, host, slug, "")
|
|
}
|
|
|
|
// PageIfChanged is Page, but answers ErrNotModified instead of sending the
|
|
// page again when it still has the checksum given.
|
|
func (c *Client) PageIfChanged(ctx context.Context, host, slug, checksum string) (PageSnapshot, error) {
|
|
q := url.Values{}
|
|
if host != "" {
|
|
q.Set("host", host)
|
|
} else {
|
|
q.Set("slug", slug)
|
|
}
|
|
// Pages run to megabytes of JSON that compresses many times over.
|
|
h := http.Header{"Accept-Encoding": {"gzip"}}
|
|
if checksum != "" {
|
|
h.Set("If-None-Match", `"`+checksum+`"`)
|
|
}
|
|
var p PageSnapshot
|
|
err := c.doWith(ctx, http.MethodGet, "/pages?"+q.Encode(), h, nil, &p)
|
|
return p, err
|
|
}
|