Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skye-31/workers-prometheus
A Cloudflare Workers prometheus exporter powered by Durable Objects & RPC
https://github.com/skye-31/workers-prometheus
cloudflare cloudflare-workers durable-objects prometheus prometheus-exporter workers
Last synced: 3 months ago
JSON representation
A Cloudflare Workers prometheus exporter powered by Durable Objects & RPC
- Host: GitHub
- URL: https://github.com/skye-31/workers-prometheus
- Owner: Skye-31
- Created: 2024-08-16T22:46:00.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-08-18T19:57:34.000Z (6 months ago)
- Last Synced: 2024-11-02T06:04:09.707Z (3 months ago)
- Topics: cloudflare, cloudflare-workers, durable-objects, prometheus, prometheus-exporter, workers
- Language: TypeScript
- Homepage: https://npmjs.com/package/workers-prometheus
- Size: 24.4 KB
- Stars: 16
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cloudflare Workers Prometheus Exporter
1. Install
`$ npm i workers-prometheus` / `$ pnpm add workers-prometheus`
2. Set up a prometheus registry
```js
import { Registry } from 'workers-prometheus/client';
import { getPrometheusExporter } from 'workers-prometheus/server';
import type { PrometheusServer } from 'workers-prometheus/server';export const PROMETHEUS = getPrometheusExporter();
```Add a Durable object to your wrangler.toml (requires workers paid plan)
```toml
[[durable_objects.bindings]]
name = "PROMETHEUS"
class_name = "PROMETHEUS"[[migrations]]
new_classes=["PROMETHEUS"]
tag = "v1"
```3. Write your worker!
```js
export default {
async fetch(request, env, ctx): Promise {
const url = new URL(request.url);const REGISTRY = new Registry(env.PROMETHEUS, ctx);
switch (url.pathname) {
case '/metrics':
return new Response(await REGISTRY.metrics());
case '/flush':
return new Response(await REGISTRY.clear());default:
const counter = REGISTRY.counter('http_requests', 'Number of HTTP requests received');
counter.inc({ method: request.method });const gauge = REGISTRY.gauge('my-gauge', 'an increasing and decreasing gauge');
gauge.inc();const histogram = REGISTRY.histogram('examplecom_latency', 'Counts latency for getting data from example.com', [50, 100, 250, 500, 1000]);
const time = Date.now();
const resp = await fetch('https://example.com');
const latency = Date.now() - time;
histogram.observe(latency, { status: resp.status });return new Response('ok');
}
},
} satisfies ExportedHandler<{ PROMETHEUS: DurableObjectNamespace }>;
```4. Deploy your worker
```
$ wrangler deploy
```5. Set up a prometheus scraper
```yml
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['..workers.dev/metrics']
```For a full example, see the `example` directory.
Planned features:
- Automatically flushing data periodically (important when using histograms)