A Cloudflare Workers template that monitors other Workers for errors and sends email notifications when critical issues are detected.
This project implements a Tail Worker that listens to execution events from other Cloudflare Workers. When errors occur, it automatically sends email alerts with detailed information about the failures.
- Monitors Worker execution outcomes (exceptions, CPU exceeded, etc.)
- Sends email notifications for critical errors
- Includes exception details and stack traces
- Aggregates multiple errors into a single alert
Copy the .dev.vars.example file to .dev.vars and update with your email addresses:
cp .dev.vars.example .dev.varsEdit .dev.vars:
# Email sender information
SENDER_NAME=Your Sender Name
SENDER_ADDR=sender@yourdomain.com
# Email recipient information
RECIPIENT_NAME=Your Recipient Name
RECIPIENT_ADDR=recipient@yourdomain.comNote: .dev.vars is git-ignored and should never be committed to version control.
Set environment variables using the Cloudflare Dashboard or Wrangler CLI:
Option A: Using Cloudflare Dashboard
- Go to Workers & Pages > Your Worker > Settings > Variables
- Add the following environment variables:
SENDER_NAMESENDER_ADDRRECIPIENT_NAMERECIPIENT_ADDR
Option B: Using Wrangler CLI
wrangler secret put SENDER_NAME
wrangler secret put SENDER_ADDR
wrangler secret put RECIPIENT_NAME
wrangler secret put RECIPIENT_ADDRConfigure Cloudflare Email Routing for your domain to enable email sending capabilities.
To enable this Tail Worker to monitor other Workers, you need to create a tail consumer binding:
Using Wrangler CLI:
wrangler tail <WORKER_NAME> --format json | wrangler tail <THIS_TAIL_WORKER_NAME>Or configure via wrangler.toml of the Worker you want to monitor:
[[tail_consumers]]
service = "tail-worker" # Name of this Tail WorkerOr wrangler.jsonc:
{
"tail_consumers": [
{
"service": "tail-worker"
}
]
}Or use the Cloudflare Dashboard:
- Go to Workers & Pages
- Select the Worker you want to monitor
- Navigate to Settings > Triggers
- Add a Tail Consumer and select this Tail Worker
For more details, refer to the Tail Workers documentation.
The Tail Worker implements the tail() handler that receives execution events from monitored Workers:
- Event Monitoring: Listens to all execution events from Workers
- Error Detection: Identifies failures (exceptions, CPU exceeded, etc.)
- Error Aggregation: Collects all critical errors with detailed information
- Email Notification: Sends an alert email containing:
- Number of errors detected
- Worker script names
- Outcome types
- Exception messages and stack traces
The worker monitors various error outcomes:
exception- Runtime exceptionsexceededCpu- CPU time limit exceededexceededMemory- Memory limit exceeded- Other non-
okand non-canceledoutcomes
Subject: CRITICAL ALERT: 1 Worker Error(s) Detected
Worker 実行中に以下のエラーが検出されました:
[WORKER ERROR] Script: producer-worker-name, Outcome: exception
Exception: ReferenceError: undefined_value is not defined
Stack:
at Object.fetch (index.js:4:17)
The project uses TypeScript with generated types for Cloudflare Workers bindings:
interface Env {
SENDER_NAME: string;
SENDER_ADDR: string;
RECIPIENT_NAME: string;
RECIPIENT_ADDR: string;
send_email_bindings: SendEmail;
}