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 🙏

© 2024 – Pkg Stats / Ryan Hefner

workers-tracing

v0.1.3

Published

Enable tracing within Workers with this simple package! Simply trace and send to a collector with a compatible export format

Downloads

9

Readme

Workers Tracing

Workers tracing is a small (~2.6 KB compressed), zero-dependency library for having distributed tracing within Cloudflare Workers.

There are currently 2 different formats supported:

  • OpenTelemetry is a standard tracing/metrics/logs format. It has wide support in many different services such as Jaeger.
  • Zipkin is another widely adopted format which is focused on tracing.

Warning This library is in beta, consider any minor version change a possibly breaking change. I will try to keep compatibiltiy for at least 1 version but cannot guarantee it. Please provide feedback in Issues

Note This is an opinionated library, it does not use the standard patterns and base libraries. This was done very intentionally, we believe this libary is much cleaner (and just lighter) than the standard libraries.

Install

Installing this package is easy, you simply need to install the npm package like so:

npm install --save workers-tracing

Usage

JavaScript

import { createTrace, SPAN_NAME, ATTRIBUTE_NAME } from 'workers-tracing';

export default {
	async fetch(req, env, ctx) {
		const trace = createTrace(req, env, ctx, {
			serviceName: 'basic-worker-tracing',
			collector: {
				url: 'http://localhost:4318/v1/traces',
			},
		});

		return this.handleRequest(req, env, trace);
	},

	async handleRequest(req, env, trace) {
		const { pathname } = new URL(req.url);
		const span = trace.startSpan('handleRequest', { attributes: { path: pathname } });

		await env.KV.put('abc', 'def');

		// .trace will return the value from the passed function
		// In this case, it'll return the KV value
		const val = await trace.trace(SPAN_NAME.KV_GET,
			() => env.KV.get('abc'),
			// There are a bunch of built in attribute/span names which you can use
			// This will allow you to ensure consistency in naming throughout your Workers
			{ attributes: { [ATTRIBUTE_NAME.KV_KEY]: 'abc '} },
		);
		span.addEvent({ name: 'KV lookup', timestamp: Date.now(), attributes: { [ATTRIBUTE_NAME.KV_KEY]: 'abc' } });

		span.end();
		await trace.send();
		return new Response(val);
	},
};

(see more in the examples folder)

TypeScript

import { createTrace, Trace, SPAN_NAME, ATTRIBUTE_NAME } from 'workers-tracing';

interface Env {
	KV: KVNamespace;
}

export default {
	async fetch(req: Request, env: Env, ctx: ExecutionContext) {
		const trace = createTrace(req, env, ctx, {
			serviceName: 'basic-worker-tracing',
			collector: {
				url: 'http://localhost:4318/v1/traces',
			},
		});

		return this.handleRequest(req, env, trace);
	},

	async handleRequest(req: Request, env: Env, trace: Trace) {
		const { pathname } = new URL(req.url);
		const span = trace.startSpan('handleRequest', { attributes: { path: pathname } });

		await env.KV.put('abc', 'def');

		// .trace will return the value from the passed function
		// In this case, it'll return the KV value
		const val = await trace.trace(SPAN_NAME.KV_GET,
			() => env.KV.get('abc'),
			{ attributes: { [ATTRIBUTE_NAME.KV_KEY]: 'abc '} },
		);
		span.addEvent({ name: 'KV lookup', timestamp: Date.now(), attributes: { [ATTRIBUTE_NAME.KV_KEY]: 'abc' } });

		span.end();
		await trace.send();
		return new Response(val);
	},
};

(see more in the examples folder)

Jaeger

To send traces to the Jaeger OpenTelemetry compatible collector you will need to make sure Jaeger is configured to accept OpenTelemetry or Zipkin.

For OpenTelemetry you will need to enable the compatibility support with COLLECTOR_OTLP_ENABLED=true (and make sure port 4318 is mapped).

For Zipkin you will need to enable the JSON compatible layer by setting COLLECTOR_ZIPKIN_HOST_PORT=:9411 (or a different port - make sure to map this).

Here is an example command to run the all-in-one Docker image with the OpenTelemetry and Zipkin compatible collector enabled:

$ docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
  -e COLLECTOR_OTLP_ENABLED=true \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 4317:4317 \
  -p 4318:4318 \
  -p 14250:14250 \
  -p 14268:14268 \
  -p 14269:14269 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.40

(or through their binary: https://www.jaegertracing.io/download/ - COLLECTOR_ZIPKIN_HOST_PORT=:9411 COLLECTOR_OTLP_ENABLED=true ./jaeger-all-in-one)

Once that is up, just set your collector URL in your Worker. Here's an example of sending to the OTLP compatible endpoint:

const trace = createTrace(req, env, ctx, {
	serviceName: 'basic-worker-tracing',
	collector: {
		url: 'http://localhost:4318/v1/traces',
	}
});

Support

Cloudflare Workers

This library will work out of the box for Workers but see limitations for the current limitations.

Limitations

Cloudflare Workers

There are a few limitations when using with Cloudflare Workers today, these include:

  • Env is not currently patchable, this means you'd need to do like span.trace('kv:get', () => env.KV.get('abc')) over just doing env.KV.get('abc')
  • Tracing cannot automatically resume tracing between services right now, see the service binding example for how to do it today

Future

There are a bunch of things planned for v1 including:

  • Patching env (optional thing) - this will allow you to do env.KV.get() like normal and have tracing automatically. No need to wrap it in a trace.
  • Span builder - Just a nice builder pattern for the trace (credit to repeat.dev for that idea).

I'd also like to make sure that Deno is supported. If you'd like to test this and fix it (or just modify the README and add tests) then please do PR :)

Outside of this lib, I want to have a related project of putting all tracing components (Sender, Collector and UI) all on Cloudflare (Workers, Workers/R2 and Pages). If this interests you, let me know!