What is opentracing?
The opentracing npm package provides a standard, vendor-neutral API for distributed tracing. It allows developers to add instrumentation to their applications, which is essential for understanding the performance and behavior of complex distributed systems. The package enables the tracking of request flows across various services and systems, making it easier to diagnose and optimize performance issues.
What are opentracing's main functionalities?
Starting a new trace
This code demonstrates how to start a new trace with a span named 'my_span' and then finish the span. This is the basic building block of distributed tracing.
const opentracing = require('opentracing');
const tracer = opentracing.globalTracer();
const span = tracer.startSpan('my_span');
span.finish();
Injecting and extracting span context
This code snippet shows how to inject a span context into a carrier (e.g., HTTP headers) and then extract it. This is crucial for propagating trace context across process boundaries.
const opentracing = require('opentracing');
const tracer = opentracing.globalTracer();
const spanContext = span.context();
const carrier = {};
tracer.inject(spanContext, opentracing.FORMAT_HTTP_HEADERS, carrier);
const extractedContext = tracer.extract(opentracing.FORMAT_HTTP_HEADERS, carrier);
Setting tags and logs
This example illustrates how to set tags and logs on a span. Tags are key-value pairs that provide additional context, such as HTTP method, while logs record timed events within a span.
const opentracing = require('opentracing');
const span = opentracing.globalTracer().startSpan('my_span');
span.setTag(opentracing.Tags.HTTP_METHOD, 'GET');
span.log({ event: 'request_sent' });
span.finish();
Other packages similar to opentracing
jaeger-client
Jaeger Client is an open-source, end-to-end distributed tracing system that works with the OpenTracing API. It is similar to opentracing but is specifically designed to work with the Jaeger backend. It provides more specific implementations for trace collection and reporting.
zipkin
Zipkin is another distributed tracing system that captures timing data needed to troubleshoot latency problems in service architectures. It has a different API compared to opentracing but serves a similar purpose in tracing requests through distributed systems.
lightstep-tracer
LightStep Tracer is a distributed tracing library that implements the OpenTracing API, similar to opentracing. It is designed to integrate with LightStep's real-time analysis platform, providing more advanced features and integrations for monitoring and diagnosing distributed systems.
OpenTracing API for JavaScript
This library is a JavaScript implementation of Open Tracing API intended for use both on the server and in the browser.
Objectives
Distributed tracing and context propagation have become important analysis tools for today's multi-layer distributed systems comprised of numerous micro-services implemented in different languages. The success of these tools is dependent on pervasive instrumentation of applications and libraries with trace context propagation support.
The OpenTracing project (http://opentracing.github.io) provides a multi-lingual standard for application-level instrumentation that's loosely coupled to any particular downstream tracing or monitoring system. In this way, adding or switching tracing implementations becomes a single-step code change.
Status
In the current version, the opentracing
package provides the core API and a default no-op implementation.
A reference implementation and a set of simple examples are planned.
See DEV.md for further development status and notes.
Quick Start
Install the package:
npm install --save opentracing
In the JS code, add instrumentation to the operations to be tracked:
Concepts
Trace is a virtual representation of the path a request takes through the layers and services of a (potentially distributed) system.
Span is a representation of any logical unit of work in the system. Spans can be nested and ordered to model parent-child and casual relationships. A Trace is tree of Spans.
API
TBD.
Usage Examples
TBD.
Development
Unit tests
To run the unit tests:
npm test
Publish
To publish a new version of the package to NPM:
npm publish
See DEV.md for additional detail.