npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@else42/cf-worker-otel

v0.2.2

Published

Lightweight OTLP metrics client for Cloudflare Workers

Readme

@elsbrock/cf-worker-otel

Push metrics from Cloudflare Workers to Prometheus, Grafana Cloud, or any OTLP-compatible backend — without pulling in the full OpenTelemetry SDK.

  • ~200 lines, zero runtime dependencies
  • Counters, gauges, and histograms
  • OTLP/HTTP JSON with delta temporality
  • Built for waitUntil() — never blocks your response
  • No-ops silently without config — safe to always instrument

Install

npm install @elsbrock/cf-worker-otel

Published to GitHub Packages. Add @elsbrock:registry=https://npm.pkg.github.com to your .npmrc.

Quick Start

import { createMetrics } from "@elsbrock/cf-worker-otel";

export default {
  async fetch(request, env, ctx) {
    const metrics = createMetrics({
      serviceName: "my-worker",
      endpoint: env.OTLP_ENDPOINT,
      token: env.OTLP_AUTH_TOKEN,
    });
    const start = Date.now();

    const response = await handleRequest(request);

    metrics.counter("http_requests_total", 1, {
      method: request.method,
      status: String(response.status),
    });
    metrics.histogram("http_request_duration_ms", Date.now() - start);

    ctx.waitUntil(metrics.flush());
    return response;
  },
};

SvelteKit on Cloudflare

// src/hooks.server.ts
import { createMetrics } from "@elsbrock/cf-worker-otel";
import { sequence } from "@sveltejs/kit/hooks";
import type { Handle } from "@sveltejs/kit";

const metricsHandle: Handle = async ({ event, resolve }) => {
  const env = event.platform?.env;
  const metrics = createMetrics({
    serviceName: "my-app",
    endpoint: env?.OTLP_ENDPOINT,
    token: env?.OTLP_AUTH_TOKEN,
  });
  const start = Date.now();
  let status = "500";
  try {
    const response = await resolve(event);
    status = String(response.status);
    return response;
  } finally {
    metrics.counter("http_requests_total", 1, {
      method: event.request.method,
      status,
      route: event.route.id ?? "unknown",
    });
    metrics.histogram("http_request_duration_ms", Date.now() - start);
    event.platform?.context?.waitUntil(metrics.flush());
  }
};

export const handle = sequence(metricsHandle, sessionHandle);

API

createMetrics(config)

Returns a per-request collector. Create one per invocation, flush at the end.

| Option | Type | Required | Description | |---|---|---|---| | serviceName | string | yes | Maps to service.name in OTLP | | endpoint | string | no | OTLP HTTP endpoint URL | | token | string | no | Bearer token for the Authorization header | | defaultAttributes | Record<string, string> | no | Merged into every data point | | histogramBounds | number[] | no | Custom bucket boundaries (default: HTTP latency in ms) |

If endpoint or token is missing, flush() does nothing.

metrics.counter(name, value, attributes?)

Monotonic counter. Same name + same attributes are aggregated within the request.

metrics.gauge(name, value, attributes?)

Point-in-time value. Last write wins per attribute set.

metrics.histogram(name, value, attributes?)

Single observation placed into a bucket. Default bounds: [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000] ms.

metrics.flush()

Serializes everything to OTLP JSON and POSTs it. Pass to ctx.waitUntil().

Prometheus Setup

Enable the OTLP receiver (v2.47+):

--web.enable-otlp-receiver

For delta temporality support (v3+):

--enable-feature=otlp-deltatocumulative

The receiver listens at POST /api/v1/otlp/v1/metrics on the Prometheus port.

How It Works

Each createMetrics() call creates an isolated collector. Metrics are accumulated in memory during request handling, then serialized to a single OTLP/HTTP JSON payload and POSTed in waitUntil(). One fetch per worker invocation — no batching across requests, no persistent state.

Delta temporality means each push contains only what happened in this request. Prometheus converts deltas to cumulative counters server-side.

License

MIT