Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nounder/cloudflare-posthog-tail-worker
https://github.com/nounder/cloudflare-posthog-tail-worker
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/nounder/cloudflare-posthog-tail-worker
- Owner: nounder
- Created: 2024-08-09T09:40:08.000Z (3 months ago)
- Default Branch: master
- Last Pushed: 2024-08-09T09:40:11.000Z (3 months ago)
- Last Synced: 2024-08-27T01:42:14.432Z (3 months ago)
- Language: TypeScript
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Capture Cloudflare Workers logs to PostHog
[Tail worker][0] listening to logs from your other workers and automatically captures it in PostHog with automatic GeoIP enrichment. Super lightweight. Starts up in 1ms and with zero-dependencies.
[0]: https://developers.cloudflare.com/workers/runtime-apis/handlers/tail/
## Why?
- Development and debugging: allows you to see PostHog events in console without actually sending them during development.
- Makes logging and error reporting decoupled from your worker.## Setup
First, deploy this worker:
1. Install packages: `[bun|npm] install`
2. Set API key: `[npx|bunx] wrangler secret put POSTHOG_API_KEY`
3. Optionally, update PostHog URL if you're using EU- or self-hosted instance.
4. `[npx|bunx] wrangler deploy`Now, in worker that produces logs, update its `wrangler.toml` and deploy it:
```toml
tail_consumers = [{service = "cloudflare-posthog-tail-worker"}]
```And you should be ready to go!
## Usage
In a worker that producers logs, you can capture PostHog events by using `console.log`
```js
console.log("event\t", JSON.stringify({
event: "user_signed_up",
}))
```First argument is a prefix, defined in `LOG_PREFIX` environmental variable.
It is used to distinguish it from other logs.