Skip to content

Canaries & real-user monitoring

Metrics and logs describe your services from the inside. Two CloudWatch features watch them from where your users are. A Synthetics canary is a script that runs on a schedule and uses your site or API like a user would, so you hear about an outage before anyone reports it. RUM (real-user monitoring) collects page loads, errors and slow requests from real visitors’ browsers.

A canary is a Node.js (Puppeteer or Playwright) or Python (Selenium) script that runs in a Lambda Function managed by Synthetics. Each run stores screenshots and logs in S3:

alchemy.run.ts
const artifacts = yield* AWS.S3.Bucket("CanaryArtifacts");
const canary = yield* AWS.Synthetics.Canary("Homepage", {
script: `
const synthetics = require("Synthetics");
exports.handler = async () => {
return await synthetics.executeStep("load homepage", async () => {
const page = await synthetics.getPage();
await page.goto("https://example.com", { waitUntil: "domcontentloaded" });
});
};
`,
artifactS3Location: Output.interpolate`s3://${artifacts.bucketName}/homepage`,
schedule: { expression: "rate(5 minutes)" },
start: true,
});

A canary is created stopped unless start: true. Without executionRoleArn, one is created with access to the artifact bucket, CloudWatch Logs and the CloudWatchSynthetics metrics.

Every run publishes SuccessPercent to the CloudWatchSynthetics namespace. Alarm when it drops:

alchemy.run.ts
yield* AWS.CloudWatch.Alarm("HomepageDown", {
Namespace: "CloudWatchSynthetics",
MetricName: "SuccessPercent",
Dimensions: [{ Name: "CanaryName", Value: canary.canaryName }],
Statistic: "Average",
Period: 300,
EvaluationPeriods: 2,
Threshold: 100,
ComparisonOperator: "LessThanThreshold",
AlarmActions: [alerts.topicArn],
});

See CloudWatch for routing alarms to a Topic.

An app monitor collects what visitors’ browsers report for the domains it covers:

alchemy.run.ts
const monitor = yield* AWS.RUM.AppMonitor("SiteMonitor", {
domain: "*.example.com",
appMonitorConfiguration: {
sessionSampleRate: 1,
telemetries: ["errors", "performance", "http"],
},
cwLogEnabled: true,
});

Your pages load AWS’s CloudWatch RUM web client with the monitor’s appMonitorId. sessionSampleRate is the share of visits recorded, from 0 to 1. With cwLogEnabled, every event is also copied to CloudWatch Logs, where you can query it like any log group.