Zipkin HTTP Transport
This package, `zipkin-transport-http`, provides the official HTTP transport mechanism for sending Zipkin trace data to a Zipkin collector server. It is a core component within the larger `zipkin-js` ecosystem, designed to integrate seamlessly with other `zipkin-js` packages like `zipkin` (for `BatchRecorder`) and various instrumentation libraries. The current stable version is `0.22.0`, released recently with improvements like a fallback for `fetch` and fixes. The project maintains a fairly active release cadence, frequently addressing bugs, adding new features such as configurable `fetch` API and SQS support in related packages, and improving TypeScript interfaces. Its primary differentiator is its deep integration and official support within the OpenZipkin JavaScript tracing suite, ensuring compatibility and alignment with Zipkin's tracing specifications.
Common errors
-
TypeError: fetch is not a function
cause The `node-fetch` peer dependency was not installed or not correctly passed to the `HttpLogger` constructor.fixEnsure `node-fetch` is installed (`npm install node-fetch`) and passed explicitly as the `fetch` option during `HttpLogger` instantiation: `new HttpLogger({ endpoint: '...', fetch: require('node-fetch') })` or `fetch: fetch` if using ESM. -
Error: Must be valid TraceId instance
cause This error often occurs in older versions (<0.18.6) due to issues with how `TraceId` instances were handled during transpilation.fixUpgrade `zipkin-transport-http` to version `0.18.6` or newer. If the problem persists, ensure your Babel/TypeScript configuration is not aggressively transpiling `zipkin`'s internal classes. -
No spans were sent to the Zipkin collector.
cause Spans are buffered by `BatchRecorder` and sent periodically or when the buffer is full. In short-lived processes (e.g., serverless functions, scripts), the process might exit before spans are flushed.fixFor short-lived processes, manually call `recorder.flush()` before the process exits. For example, `await new Promise(resolve => recorder.flush(() => resolve()));`
Warnings
- breaking Support for Node.js 8 was dropped starting with version `0.21.0`. Projects running on Node.js 8 or older will not receive updates and should upgrade their Node.js environment.
- breaking Support for Node.js 6 was dropped starting with version `0.17.1`. Applications relying on Node.js 6 will need to upgrade to continue using newer versions of this package.
- gotcha The `node-fetch` package is a peer dependency and must be explicitly installed alongside `zipkin-transport-http`. Failure to do so will result in runtime errors when attempting to send spans.
- gotcha Older versions of this package (prior to `0.18.6`) could throw `Error: Must be valid TraceId instance` due to transpilation issues.
Install
-
npm install zipkin-transport-http -
yarn add zipkin-transport-http -
pnpm add zipkin-transport-http
Imports
- HttpLogger
const HttpLogger = require('zipkin-transport-http').HttpLogger;import { HttpLogger } from 'zipkin-transport-http'; - BatchRecorder
import { BatchRecorder } from 'zipkin-transport-http';import { BatchRecorder } from 'zipkin'; - jsonEncoder.JSON_V2
import { jsonEncoder } from 'zipkin-transport-http';import { jsonEncoder } from 'zipkin';
Quickstart
import { HttpLogger } from 'zipkin-transport-http';
import { BatchRecorder, Tracer, jsonEncoder } from 'zipkin';
import { SomeContextManager } from 'zipkin-context-cls'; // Or 'zipkin-context-koa', etc.
import fetch from 'node-fetch'; // Peer dependency
const ZIPKIN_COLLECTOR_URL = process.env.ZIPKIN_COLLECTOR_URL ?? 'http://localhost:9411/api/v2/spans';
const SERVICE_NAME = process.env.SERVICE_NAME ?? 'my-service';
// 1. Create an HTTP Logger
const httpLogger = new HttpLogger({
endpoint: ZIPKIN_COLLECTOR_URL,
fetch: fetch,
jsonEncoder: jsonEncoder.JSON_V2,
});
// 2. Create a BatchRecorder with the HTTP Logger
const recorder = new BatchRecorder({
logger: httpLogger,
});
// 3. Create a Tracer
const tracer = new Tracer({
ctxImpl: new SomeContextManager(SERVICE_NAME), // Replace with your chosen context manager
recorder: recorder,
localServiceName: SERVICE_NAME,
});
// Example: Start a new trace and record a span
async function doSomeWork() {
const rootSpan = tracer.startRootSpan('root-operation');
tracer.scoped(() => {
// Perform some work here
console.log('Doing some traced work...');
tracer.addBinaryAnnotation('http.url', '/my-endpoint');
tracer.addBinaryAnnotation('http.status_code', '200');
});
rootSpan.finish();
console.log('Trace finished and sent to Zipkin.');
}
doSomeWork();
// In a real application, you'd likely want to ensure all spans are flushed on exit.
// recorder.flush() can be called manually if needed (e.g., in serverless environments).