JavaScript

Required Reading

To fully understand this platform API, it’s helpful to be familiar with the OpenTracing project and terminology more specifically.

Quick Start

Install the package using npm:

  1. npm install --save opentracing

Example

The package contains an example using a naive MockTracer implementation. To run the example:

  1. npm run example

The output should look something like:

  1. Spans:
  2. parent_span - 1521ms
  3. tag 'custom':'tag value'
  4. tag 'alpha':'1000'
  5. child_span - 503ms
  6. tag 'alpha':'200'
  7. 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.

  1. const http = require('http');
  2. const opentracing = require('opentracing');
  3. // NOTE: the default OpenTracing tracer does not record any tracing information.
  4. // Replace this line with the tracer implementation of your choice.
  5. const tracer = new opentracing.Tracer();
  6. const span = tracer.startSpan('http_request');
  7. const opts = {
  8. host : 'example.com',
  9. method: 'GET',
  10. port : '80',
  11. path: '/',
  12. };
  13. http.request(opts, res => {
  14. res.setEncoding('utf8');
  15. res.on('error', err => {
  16. // assuming no retries, mark the span as failed
  17. span.setTag(opentracing.Tags.ERROR, true);
  18. span.log({'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack});
  19. span.finish();
  20. });
  21. res.on('data', chunk => {
  22. span.log({'event': 'data_received', 'chunk_length': chunk.length});
  23. });
  24. res.on('end', () => {
  25. span.log({'event': 'request_end'});
  26. span.finish();
  27. });
  28. }).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:

  1. const CustomTracer = require('tracing-implementation-of-your-choice');
  2. const tracer = new CustomTracer();

Usage in the browser

The package contains two bundles built with webpack that can be included using a standard <script> tag. The library will be exposed under the global opentracing namespace:

  • dist/opentracing-browser.min.js - minified, no runtime checks
  • dist/opentracing-browser.js - debug version with runtime checks

Usage with TypeScript

Since the source is written in TypeScript, if you are using TypeScript, you can just npm install the package and it will work out of the box. This is especially useful for implementors who want to type check their implementation with the base interface.

Global tracer

The library also provides a global singleton tracer for convenience. This can be set and accessed via the following:

  1. opentracing.initGlobalTracer(new CustomTracer());
  2. const tracer = opentracing.globalTracer();

Note: globalTracer() returns a wrapper on the actual tracer object. This is done for the convenience of use as it ensures that the function will always return a non-null object. This can be helpful in cases where it is difficult or impossible to know precisely when initGlobalTracer is called (for example, when writing a utility library that does not control the initialization process). For more precise control, individual Tracer objects can be used instead of the global tracer.

API Documentation

There is a hosted copy of the current generated ESDoc API Documentation here.

Contributing & developer information

See the OpenTracing website for general information on contributing to OpenTracing.

The project is written in TypeScript and built using a npm scripts. Run:

  • npm run build creates the compiled, distributable JavaScript code in ./lib
  • npm run watch incrementally compiles on file changes
  • npm run webpack creates the bundles for the browser in ./dist
  • npm test runs the tests
  • npm run typedoc generates the documentation in ./typedoc

Note: The minimum supported Node version for development is >=6. Tests can however be run on any version that this project supports (>=0.10).

OpenTracing tracer implementations

This section is intended for developers wishing to implement their own tracers. Developers who simply wish to use OpenTracing can safely ignore this information.

Custom tracer implementation

Implementations can subclass opentracing.Trace, opentracing.Span, and the other API classes to build an OpenTracing tracer and implement the underscore prefixed methods such as _addTag to pick up a bit of common code implemented in the base classes.

API compatibility testing

If mocha is being used for unit testing, test/api_compatibility can be used to test the custom tracer. The file exports a single function that expects as an argument a function that will return a new instance of the tracer.

  1. const apiCompatibilityChecks = require('opentracing/lib/test/api_compatibility.js').default;
  2. apiCompatibilityChecks(() => new CustomTracer());

LICENSE

Apache License 2.0

MockTracer

A minimal example tracer is provided in the src/mock_tracer directory of the source code.