CodaBool / analytics

Send weekly emails to notify about CF R2 usage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ₯… Goal

Provides pricing and uses the MailChannel integration with CloudFlare. This will give a percent report on all class A, class B, and storage size metrics.

Then if any of them are above 50% of the free tier usage it will email you an alert. However, every Monday an email will be sent regardless to get an idea of where your usage stands.

πŸ’Έ Free Tier

This worker has been running for me well under the 10ms CPU time. Meaning it should be completely free to use.

πŸ”‘ Required Variables

create a .dev.vars file with the following contents

You can get your account id from the right on this page

CLOUDFLARE_ACCOUNT=account_id
CLOUDFLARE_API_TOKEN=global_or_scoped_token
EMAIL=destination@email.com

Update the DOMAIN & NAME variable found in wrangler.toml

create a new line for DEBUG=true to help with your initial deploy

[vars]
DOMAIN="pom-pom-purin.com"
NAME="Hacker"
DEBUG=true

πŸ₯Ύ Usage

🟒 Node 20.6+

For local testing I'm using node --env-file which requires 20.6+. If you have a lower version use the dotenv package.

Keep in mind there is currently no way to use MailChannel to test locally so that logic is removed entirely in the local.js file.

πŸ— Deployment

Use wrangler CLI. Add the three secrets required:

wrangler secret put CLOUDFLARE_ACCOUNT

CLOUDFLARE_ACCOUNT=
CLOUDFLARE_API_TOKEN=
EMAIL=

Then deploy with wrangler

wrangler deploy

πŸ“¨ MailChannel

Integration with MailChannel is dead simple. However, there is little good info online about how to do the setup. The only real documentation I've found has been here

This is how I integrated:

  1. have a DNS record managed by Cloudflare. Meaning you can create records for it. You can use nameservers if you bought from another service.
  2. create a new "TXT" record with the name "_mailchannels"
  3. In the Content input follow this format
v=mc1 auth=example.com cfid=example.workers.dev

Since I have the codabool.com domain mine looks like this

This auth domain must match the domain variable found in wrangler.toml

# example _mailchannels record for codabool.com
v=mc1 auth=codabool.com cfid=codabool.workers.dev

The cfid can be tricky to configure right. I believe you will need to setup a subdomain for your workers on this page. Also keep in mind that since these are DNS changes it can take a couple minutes to propgate and testing record changes may not be reflected immediately.

  1. Verify it's working by testing your worker. This worker is only runnable through a scheduled cron in it's current state. So, put it on a * * * * * cron at the triggers tab. Then activate a log stream to capture a log event for debugging.
  2. Once verified working, set the debug var to false in the worker settings. set to a daily cron of 0 9 * * * and enjoy weekly (unless past alarm state it will email daily) metric emails β˜•

πŸ€– Automation

I have automatted builds which require setting these two GitHub Action secrets:

this will deploy the worker on any push to the main branch. Keep in mind Global API keys are not allowed for this. You will need to create a scoped token here

CLOUDFLARE_API_TOKEN=
CLOUDFLARE_ACCOUNT=

About

Send weekly emails to notify about CF R2 usage


Languages

Language:JavaScript 100.0%