diff --git a/infra/workers/README.md b/infra/workers/README.md index 3b8d3b10da..c4615419d7 100644 --- a/infra/workers/README.md +++ b/infra/workers/README.md @@ -33,3 +33,8 @@ To import an existing worker from the Cloudflare dashboard, use ```sh npm create cloudflare@2 existing-worker-name -- --type pre-existing --existing-script existing-worker-name ``` + +## Logging + +Attach our tail worker (called `tail`) to your worker. Then, any `console.log` +statements called by your worker will be forwarded to Grafana. diff --git a/infra/workers/tail/package.json b/infra/workers/tail/package.json new file mode 100644 index 0000000000..2ec6898e4f --- /dev/null +++ b/infra/workers/tail/package.json @@ -0,0 +1,10 @@ +{ + "name": "tail", + "private": true, + "devDependencies": { + "@cloudflare/workers-types": "^4.20240614.0", + "typescript": "^5", + "wrangler": "^3" + }, + "packageManager": "yarn@1.22.22" +} diff --git a/infra/workers/tail/src/index.ts b/infra/workers/tail/src/index.ts new file mode 100644 index 0000000000..3f73e870b0 --- /dev/null +++ b/infra/workers/tail/src/index.ts @@ -0,0 +1,19 @@ +/** + * A tail worker that forwards all `console.log`s to Loki. + * + * https://developers.cloudflare.com/workers/observability/logging/tail-workers/ + */ +export default { + async tail(events: TailItems[], env: Env) { + // If the tail worker itself throws an exception (it shouldn't, unless + // Loki is down), we don't catch it so that it counts as an "error" in + // the worker stats. + return handleTail(events, env.LOKI_URL); + }, +} satisfies ExportedHander; + +interface Env { + LOKI_URL: string; +} + +const handleTail = (events: TailItems[], lokiURL: string) => {}; diff --git a/infra/workers/tail/tsconfig.json b/infra/workers/tail/tsconfig.json new file mode 100644 index 0000000000..a65b752070 --- /dev/null +++ b/infra/workers/tail/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "../tsconfig.base.json", "include": ["src"] } diff --git a/infra/workers/tail/wrangler.toml b/infra/workers/tail/wrangler.toml new file mode 100644 index 0000000000..14c8b92083 --- /dev/null +++ b/infra/workers/tail/wrangler.toml @@ -0,0 +1,7 @@ +name = "tail" +main = "src/index.ts" +compatibility_date = "2024-06-14" + +[vars] +# Added as a secret via the Cloudflare dashboard +# LOKI_URL = "https://discord.com/api/webhooks/{webhook.id}/{webhook.token}"