Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions convox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ services:
# Pinned to an immutable release tag. scripts/bump-version.sh sets it to the new
# version (:vX.Y.Z); the release workflow publishes that tag when the v* git tag is pushed.
# :e327add is the v0.1.1 release (identical to :latest when this was pinned).
image: docker.io/docspringcom/rack-gateway:v0.1.35
image: docker.io/docspringcom/rack-gateway:v0.1.36
command: ./scripts/start-gateway.sh
environment:
- PORT=8080
Expand All @@ -54,7 +54,7 @@ services:

admin:
# Uses same image as gateway service (kept in sync by scripts/bump-version.sh)
image: docker.io/docspringcom/rack-gateway:v0.1.35
image: docker.io/docspringcom/rack-gateway:v0.1.36
command: echo "Use this service to run migrations, database admin tasks, etc."
environment:
- PORT=8080
Expand Down
4 changes: 3 additions & 1 deletion docs/src/content/docs/operations/monitoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ monitoring service's key, so the gateway never logs them.
- **Uptime** (`UPTIME_HEARTBEAT_URL`): requested when the gateway starts and every 15 minutes
(`UPTIME_HEARTBEAT_INTERVAL_MINUTES`) while the health check would pass. It's a background job, so it stops when
the gateway is down, can't reach its database, has stopped running jobs, or can't write its audit log. Give the
monitor that schedule and some grace for restarts, for example 15 minutes.
monitor that schedule and some grace for restarts, for example 15 minutes. A failed request is logged, and from
the third failure in a row it's also reported to Sentry, with the reason (an HTTP status, a timeout): one failure
is usually the monitoring service or the network, and the monitor alerts when requests stop anyway.
- **Audit verification** (`AUDIT_VERIFY_HEARTBEAT_URL`): requested after every
[hourly audit verification](/deployment/terraform/s3-worm-storage/#hourly-verification-in-the-gateway) run,
half past every hour (UTC), whatever it found: failures already alert through Sentry and Slack, and the monitor
Expand Down
3 changes: 2 additions & 1 deletion internal/gateway/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ Key files:
`AUDIT_VERIFY_HEARTBEAT_URL`, so a heartbeat monitor alerts when runs stop.
- `heartbeat/`, `jobs/uptime/` - Heartbeat pings (https URLs from env vars, never logged: they carry the monitor's
key). `UPTIME_HEARTBEAT_URL` is pinged on start and every `UPTIME_HEARTBEAT_INTERVAL_MINUTES` while the health
check would pass.
check would pass. A failed ping is logged; the job fails (and so reports to Sentry) only from
`uptime.FailuresBeforeReport` failures in a row.

### `/middleware` - HTTP Middleware

Expand Down
25 changes: 23 additions & 2 deletions internal/gateway/jobs/uptime/uptime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strconv"
"strings"
"sync/atomic"
"time"

"github.com/riverqueue/river"
Expand All @@ -28,6 +29,12 @@ const (
// DefaultInterval is how often the gateway pings unless UPTIME_HEARTBEAT_INTERVAL_MINUTES says otherwise.
const DefaultInterval = 15 * time.Minute

// FailuresBeforeReport is how many pings in a row must fail before the job fails, which reports it to Sentry. A
// single failed ping is usually the monitoring service or the network having a moment, and the heartbeat monitor
// alerts when pings stop arriving, whatever the gateway reports. The job error is for a ping that keeps failing (a
// wrong URL, a revoked key), and says why.
const FailuresBeforeReport = 3

// IntervalFromEnv returns UPTIME_HEARTBEAT_INTERVAL_MINUTES, or DefaultInterval when it isn't set.
func IntervalFromEnv() (time.Duration, error) {
value := strings.TrimSpace(os.Getenv(IntervalEnv))
Expand All @@ -54,6 +61,9 @@ type Worker struct {
// health is the health check's audit check (audit.WriteHealth).
health func(time.Time) error
now func() time.Time
// failures counts the pings that have failed in a row. The worker lives as long as the process, and a gateway
// runs one process.
failures atomic.Int32
}

// NewWorker returns the worker; ping requests the heartbeat URL.
Expand All @@ -62,13 +72,24 @@ func NewWorker(ping func(context.Context) error) *Worker {
}

// Work pings the heartbeat unless the health check would fail: a gateway that refuses every request because it
// can't write its audit log isn't up. A failed ping isn't retried; the next one is due within the interval.
// can't write its audit log isn't up. A failed ping isn't retried; the next one is due within the interval. A failed
// ping is logged, and the job fails from the FailuresBeforeReport-th failure in a row.
func (w *Worker) Work(ctx context.Context, _ *river.Job[Args]) error {
if err := w.health(w.now()); err != nil {
gtwlog.Errorf("Uptime heartbeat: not pinging, the gateway is unhealthy: %v", err)
return nil
}
return w.ping(ctx)
if err := w.ping(ctx); err != nil {
failures := w.failures.Add(1)
if failures < FailuresBeforeReport {
gtwlog.Warnf("Uptime heartbeat: ping failed (%d in a row, reported from %d): %v",
failures, FailuresBeforeReport, err)
return nil
}
return fmt.Errorf("%w (%d pings in a row have failed)", err, failures)
}
w.failures.Store(0)
return nil
}

// PeriodicJob pings when the gateway starts and every interval.
Expand Down
34 changes: 31 additions & 3 deletions internal/gateway/jobs/uptime/uptime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,38 @@ func TestWorkDoesNotPingWhileUnhealthy(t *testing.T) {
assert.Equal(t, 0, *pings)
}

func TestWorkFailsWhenThePingFails(t *testing.T) {
worker := NewWorker(func(context.Context) error { return errors.New("UPTIME_HEARTBEAT_URL ping failed: HTTP 500") })
// pingResults returns a worker whose pings fail or pass in the given order.
func pingResults(results ...error) *Worker {
worker := NewWorker(func(context.Context) error {
result := results[0]
results = results[1:]
return result
})
worker.health = func(time.Time) error { return nil }
require.EqualError(t, worker.Work(context.Background(), nil), "UPTIME_HEARTBEAT_URL ping failed: HTTP 500")
return worker
}

// One failed ping is usually the monitoring service or the network, and the heartbeat monitor alerts if pings stop:
// the job fails, and so reports, from the third failure in a row.
func TestWorkFailsWhenPingsKeepFailing(t *testing.T) {
failed := errors.New("UPTIME_HEARTBEAT_URL ping failed: HTTP 500")
worker := pingResults(failed, failed, failed, failed)
require.NoError(t, worker.Work(context.Background(), nil))
require.NoError(t, worker.Work(context.Background(), nil))
require.EqualError(t, worker.Work(context.Background(), nil),
"UPTIME_HEARTBEAT_URL ping failed: HTTP 500 (3 pings in a row have failed)")
err := worker.Work(context.Background(), nil)
require.ErrorIs(t, err, failed)
assert.EqualError(t, err, "UPTIME_HEARTBEAT_URL ping failed: HTTP 500 (4 pings in a row have failed)")
}

func TestWorkCountsFailuresAgainAfterAPing(t *testing.T) {
failed := errors.New("UPTIME_HEARTBEAT_URL ping failed: timeout")
worker := pingResults(failed, failed, nil, failed, failed, failed)
for range 5 {
require.NoError(t, worker.Work(context.Background(), nil))
}
require.Error(t, worker.Work(context.Background(), nil))
}

func TestIntervalFromEnv(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "web",
"version": "0.1.35",
"version": "0.1.36",
"private": true,
"type": "module",
"scripts": {
Expand Down