A simple Prometheus (aggregated) push gateway allowing stateless/serverless workloads, ephemeral and batch jobs to easily expose their metrics.

Overview

Serverless Prometheus (aggregated) Push Gateway

A simple Prometheus (aggregated) push gateway allowing stateless/serverless workloads, ephemeral and batch jobs to easily expose their metrics. Aggregated metrics are exposed via /metrics endpoint and optionally pushed to a remote Prometheus instance.

Powered by Cloudflare Workers, Durable Objects, and CRON Triggers.

Prometheus metrics

Features

  • Simple PATCH counter - no need for a client libraries for a counter metric, just send PATCH /metrics/:metricName?label1=value1&label2=value2 to increment its value.
  • Aggregated metrics - received values are added up to the current (label matching) metrics. All metrics exposed on GET /metrics.
  • Automatic push to a remote prometheus server - metrics are (optionally) pushed to a remote_write prometheus endpoint on each CRON Trigger execution. See Grafana Cloud (generous free tier) for a managed Prometheus with Grafana.

Deployment

Cloudflare Account with Paid Workers is needed.

  1. yarn
  2. yarn deploy
  3. (optional) add following secrets (either via Cloudflare Dashboard or with npx wrangler@beta secret put)
    • SECRET_PROM_ENDPOINT (e.g. https://prometheus-us-central1.grafana.net/api/prom/push)
    • SECRET_PROM_USER (e.g. 23333)
    • SECRET_PROM_TOKEN (e.g. xxyy)

Send metrics

There are a couple of easy ways to push your metrics, the POST /metrics endpoint is compatible with any prometheus client library configured to use it as a push gateway.

In case Cloudflare Workers are the only clients, its recommended to deploy this Workers with no routes attached to it and use Service bindings.

  1. POST /metrics - accepts prometheus text-based format.
  2. PATCH /metrics/:metricName?label1=value1&label2=value2 - simplified counter increments that works well with no libraries needed.

Examples

curl

  1. Send curl
    curl -x PATCH https://worker.example.com/metrics/metric_name?foo=bar
    
  2. Send POST request with body in prometheus text-based format
    cat <<EOF | curl --data-binary @- https://worker.example.com/metrics
    # TYPE some_metric counter
    some_metric{label="val1"} 42
    # TYPE another_metric gauge
    # HELP another_metric Just an example.
    another_metric 2398.283
    EOF

Workers

  1. A simple worker_request counter that will keep track of request countries.
    export default {
        async fetch(request, env, ctx) {
            ctx.waitUntil(
                fetch(`https://worker.example.com/metrics/worker_request?country=${request.cf.country}&anotherLabel=value`, {
                    method: "PATCH"
                })
            )
            return new Response("Hello World!")
        }
    }
  2. promjs (or any other js library)
     import prom from 'promjs';
     export default {
         async fetch(request, env, ctx) {
             const registry = prom()
             const counter = registry.create('counter', 'my_counter', 'A counter for things')
             
             // process tasks, e.g. orders
             counter.inc({user: "user1", plan: "pro"})
             counter.add(3, {user: "user2", plan: "free"})
             counter.inc({user: "user3", plan: "pro"})
    
             ctx.waitUntil(
                 fetch(`https://worker.example.com/metrics`, {
                     method: "POST",
                     body: JSON.stringify(registry.metrics())
                 })
             )
    
         }
     }

Delete metrics

  • DELETE /metrics/__all to delete all metrics
  • DELETE /metrics/:metricName to delete a specific metric,
  • DELETE /metrics/:metricName?foo=bar to delete a metric matching specific labels only (all labels need to match)

Limitations

  • Max ~100rps, as no Durable Objects sharding is in place. (TODO)
  • counter and gauge metric types supported only. (histogram WIP)
  • No auth. If a public route needed, Cloudflare Access is highly recommended.

Thank you

Inspired by Prometheus Push Gateway and Prometheus Aggregation Gateway

You might also like...

A simple web server exposing Hetzner cloud instances for consumption by the Prometheus HTTP service discovery.

Prometheus: Hetzner Service Discovery A server to provide automatic node discovery for Hetzner Cloud to Prometheus via HTTP service discovery. In cont

Oct 10, 2022

Standartized way to expose and use Obsidian Inter-Plugin APIs

Obsidian Plugin API Standartized way of exposing and using Obsidian Plugin APIs Installation npm install --save-dev @vanakat/plugin-api Usage This mod

Jun 23, 2022

Expose top-level identifiers in Next.js app.js

recma-nextjs-static-props Expose top-level identifiers in Next.js app.js Installation npm install recma-nexjs-static-props Usage This plugin is intend

Nov 3, 2022

A Gun DB extension that ships secure* ephemeral messaging between Gun peers using Bugout, secured by Gun's SEA suite

Bugoff A Gun DB extension that ships secure* ephemeral messaging between Gun peers using Bugout, secured by Gun's SEA suite About Bugoff creates an SE

Nov 12, 2022

See a banned user's profile, their friends, their favorite games, their followers etc.

Roblox-Banned-User-Viewer AKA BanView See a banned user's profile, their friends, their favorite games, their followers etc. Ever wondered how to view

Nov 18, 2022

Functional Programming with NestJS, Prisma. immutable, pure, stateless

Functional-NestJS Functional Programming with NestJS, Prisma. immutable, pure, stateless. 1. Introduction A production ready typescript backend reposi

Dec 6, 2022

📈 A Prometheus exporter for SerenityOS

serenity_exporter A Prometheus exporter for SerenityOS. Installation Copy the cloned repository, or at minimum, src/exporter.js, src/metrics.js, and r

Jul 15, 2022

A utility package to help implement stateless CSRF protection using the Double Submit Cookie Pattern in express.

Double CSRF A utility package to help implement stateless CSRF protection using the Double Submit Cookie Pattern in express. Dos and Don'ts • Getting

Dec 28, 2022
Owner
Adam Janiš
@cloudflare developer advocate
Adam Janiš
A visual overview of Kubernetes architecture and Prometheus metrics

A visual overview of Kubernetes architecture and Prometheus metrics. Structure Navigate through the structures page to easily see your control planes

OSLabs Beta 213 Oct 11, 2022
A peer-to-peer chat app that is serverless, decentralized, and ephemeral

Chitchatter Logo provided by @ramyashreeshetty Chitchatter is a free (as in both price and freedom) communication tool. It is designed with security a

Jeremy Kahn 714 Dec 19, 2022
🪐 The IPFS gateway for NFT.Storage is not "another gateway", but a caching layer for NFTs that sits on top of existing IPFS public gateways.

nftstorage.link The IPFS gateway for nft.storage is not "another gateway", but a caching layer for NFT’s that sits on top of existing IPFS public gate

NFT.Storage 37 Dec 19, 2022
A flexible gateway for running ML inference jobs through cloud providers or your own GPU. Powered by Replicate and Cloudflare Workers.

Cogflare (Working title) Cogflare is a Cloudflare Workers application that aims to simplify running distributed ML inference jobs through a central AP

NightmareBot 14 Dec 12, 2022
Analytics and insights for data aggregated from multiple bridges and chains.

Inter-Bridge-Visualizer Analytics and insights for data aggregated from multiple bridges and chains. Website at bridgeexplorer.xyz Background Bridges

Verumlotus 8 Jul 11, 2022
A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in the corresponding Storyboard Dev Blog post.

AWS Lambda Dynamic Ephemeral Storage Example A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in

Storyboard.fm 3 Jun 14, 2022
Deploy a multi-account cloud foundation to support highly-regulated workloads and complex compliance requirements.

Landing Zone Accelerator on AWS The Landing Zone Accelerator on AWS solution helps you quickly deploy a secure, resilient, scalable, and fully automat

Amazon Web Services - Labs 130 Dec 29, 2022
A serverless AWS expense tracker API. AWS Lambda functions, API gateway, and Dynamodb are among the ingredients.

AWS-Serverless-API A serverless AWS expense tracker API. AWS Lambda functions API gateway Dynamodb Endpoints Create a new expense: Method: POST Body f

Ondiek Elijah Ochieng 1 Jul 16, 2022
awsrun 189 Jan 3, 2023
This application provides the CDK project and a frontend that allows you to build a serverless chat application based on API Gateway's WebSocket-based API feature.

Serverless chat application using ApiGateway Websockets This project lets you provision a ready-to-use fully serverless real-time chat application usi

AWS Samples 60 Jan 3, 2023