Zipkin HTTP Transport

0.22.0 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

Demonstrates initializing `HttpLogger` with `node-fetch`, integrating it into a `BatchRecorder`, and creating a `Tracer` to send a simple span to a Zipkin collector.

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).

view raw JSON →