Skip to content

Latest commit

 

History

History
95 lines (64 loc) · 2.45 KB

File metadata and controls

95 lines (64 loc) · 2.45 KB

OpenBreweryDB Metrics Worker

Cloudflare Worker that collects and stores OpenBreweryDB traffic metrics from Cloudflare Analytics GraphQL API to Workers KV under the key transparency_dashboard.

Schedule

The worker runs on the following schedules:

  • Production: Hourly at the top of each hour (0 * * * *)
  • Testing: Every 5 minutes (*/5 * * * *)

Each run collects:

  • Last 24 hours of traffic data
  • Last 7 days of aggregated traffic data
  • Metrics broken down by: API requests, WWW requests, other requests, visits, and bandwidth

Requirements

  • Cloudflare account + wrangler authenticated (npx wrangler login)
  • A Workers KV namespace bound as OBDB_METRICS
  • Secrets in the production environment:
    • CLOUDFLARE_API_TOKEN
    • ZONE_ID

Local development

npm install
npm run typecheck
npm run dev -- --env testing

Wrangler will not automatically run cron triggers locally. To manually trigger the scheduled handler while running wrangler dev:

curl "http://localhost:8787/cdn-cgi/handler/scheduled"

Deploy (production)

npx wrangler deploy --env production

Set required secrets:

npx wrangler secret put CLOUDFLARE_API_TOKEN --env production
npx wrangler secret put ZONE_ID --env production

Manually trigger the scheduled job (production)

This worker includes an authenticated endpoint for on-demand runs:

  • Path: GET /__scheduled
  • Header: x-manual-trigger-token: <MANUAL_TRIGGER_TOKEN>

Set the token secret:

npx wrangler secret put MANUAL_TRIGGER_TOKEN --env production

Trigger a run:

curl -sS -i \
  -H "x-manual-trigger-token: <YOUR_TOKEN>" \
  https://openbrewerydb-metrics.wandering-leaf-studios.workers.dev/__scheduled

Read the KV value

Wrangler v4 commands default to local mode, so to read data from the Cloudflare-hosted KV namespace you must pass --remote.

Reference: Cloudflare docs (Wrangler v3 -> v4 migration) state that wrangler kv key get now queries locally unless --remote is specified.

Production namespace:

npx wrangler kv key get transparency_dashboard --binding OBDB_METRICS --env production --text --remote

Testing namespace:

npx wrangler kv key get transparency_dashboard --binding OBDB_METRICS --env testing --text --remote

Preview namespace (if you explicitly wrote to preview):

npx wrangler kv key get transparency_dashboard --binding OBDB_METRICS --env testing --preview --text --remote