[![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] [![NPM Published Version][npm-img]][npm] ![Node Version][node-img] [![Join the chat at https://gitter.im/opentracing/opentracing-javascript](https://badges.gitter.im/opentracing/opentracing-javascript.svg)](https://gitter.im/opentracing/opentracing-javascript?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) # OpenTracing API for JavaScript This library is a JavaScript implementation of Open Tracing API. It is intended for use both on the server and in the browser. ## Required Reading To fully understand this platform API, it's helpful to be familiar with the [OpenTracing project](http://opentracing.io) and [terminology](http://opentracing.io/documentation/pages/spec.html) more specifically. ## Quick Start Install the package using `npm`: ```bash npm install --save opentracing ``` ### Example The package contains an example using a naive `MockTracer` implementation. To run the example: ```bash npm run example ``` The output should look something like: ``` Spans: parent_span - 1521ms tag 'custom':'tag value' tag 'alpha':'1000' child_span - 503ms tag 'alpha':'200' tag 'beta':'50' ``` ### Code snippet In your JavaScript code, add instrumentation to the operations to be tracked. This is composed primarily of using "spans" around operations of interest and adding log statements to capture useful data relevant to those operations. ```js const http = require('http'); const opentracing = require('opentracing'); // NOTE: the default OpenTracing tracer does not record any tracing information. // Replace this line with the tracer implementation of your choice. const tracer = new opentracing.Tracer(); const span = tracer.startSpan('http_request'); const opts = { host : 'example.com', method: 'GET', port : '80', path: '/', }; http.request(opts, res => { res.setEncoding('utf8'); res.on('error', err => { // assuming no retries, mark the span as failed span.setTag(opentracing.Tags.ERROR, true); span.log({'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack}); span.finish(); }); res.on('data', chunk => { span.log({'event': 'data_received', 'chunk_length': chunk.length}); }); res.on('end', () => { span.log({'event': 'request_end'}); span.finish(); }); }).end(); ``` As noted in the source snippet, the default behavior of the `opentracing` package is to act as a "no op" implementation. To capture and make the tracing data actionable, the `tracer` object should be initialized with the OpenTracing implementation of your choice as in the pseudo-code below: ```js const CustomTracer = require('tracing-implementation-of-your-choice'); const tracer = new CustomTracer(); ``` ### Usage in the browser The package contains two bundles built with webpack that can be included using a standard `