Skeletal tail worker

This commit is contained in:
Manav Rathi
2024-06-15 17:51:46 +05:30
parent 08255b3f8a
commit 5e080a90e3
5 changed files with 42 additions and 0 deletions

View File

@@ -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.

View File

@@ -0,0 +1,10 @@
{
"name": "tail",
"private": true,
"devDependencies": {
"@cloudflare/workers-types": "^4.20240614.0",
"typescript": "^5",
"wrangler": "^3"
},
"packageManager": "yarn@1.22.22"
}

View File

@@ -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<Env>;
interface Env {
LOKI_URL: string;
}
const handleTail = (events: TailItems[], lokiURL: string) => {};

View File

@@ -0,0 +1 @@
{ "extends": "../tsconfig.base.json", "include": ["src"] }

View File

@@ -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}"