• This repository has been archived on 23/May/2023
  • Stars
    star
    1,089
  • Rank 41,591 (Top 0.9 %)
  • Language
    TypeScript
  • License
    Apache License 2.0
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OpenTracing API for Javascript (both Node and browser). 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163

Build Status Coverage Status NPM Published Version Node Version Join the chat at https://gitter.im/opentracing/opentracing-javascript

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 and terminology more specifically.

Quick Start

Install the package using npm:

npm install --save opentracing

Example

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

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.

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:

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 <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:

opentracing.initGlobalTracer(new CustomTracer());

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.Tracer, 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.

const apiCompatibilityChecks = require('opentracing/lib/test/api_compatibility.js').default;
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.

More Repositories

1

opentracing-go

OpenTracing API for Go. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
Go
3,489
star
2

opentracing-java

OpenTracing API for Java. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
Java
1,679
star
3

specification

A place to document (and discuss) the OpenTracing specification. 🛑 This project is DEPRECATED! https://github.com/opentracing/specification/issues/163
1,173
star
4

opentracing-python

OpenTracing API for Python. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
Python
755
star
5

opentracing-csharp

OpenTracing API for C# (.NET). 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
C#
520
star
6

opentracing-php

OpenTracing API for PHP
PHP
505
star
7

opentracing-cpp

OpenTracing API for C++. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
C++
319
star
8

opentracing-ruby

OpenTracing API for Ruby
Ruby
172
star
9

opentracing.io

OpenTracing website
SCSS
116
star
10

basictracer-go

Basic implementation of the OpenTracing API for Go. 🛑 This library is DEPRECATED!
Go
81
star
11

opentracing-rust

OpenTracing API for Rust
Rust
77
star
12

opentracing-c

ANSI C API for OpenTracing
C
33
star
13

opentracing-lua

OpenTracing API for Lua
Lua
28
star
14

opentracing-objc

OpenTracing API for Objective-C. 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
Objective-C
28
star
15

basictracer-python

The Python implementation of the "BasicTracer" reference implementation. 🛑 This library is DEPRECATED!
Python
26
star
16

opentracing-swift

This is a prototype OpenTracing API for the Swift language. This is intended to explore and validate decisions about implementing the OpenTracing API in Swift.
Swift
17
star
17

lua-bridge-tracer

Provides an implementation of the Lua OpenTracing API on top of the C++ API
C++
14
star
18

basictracer-csharp

Reference implementation of OpenTracing API in C#
12
star
19

documentation

Please see opentracing.io repo instead
7
star
20

basictracer-javascript

JavaScript
7
star
21

contrib

A place (or, really, a pointer) for community contributions to OpenTracing
5
star
22

opentracing-java-v030

Backwards compatibility layer for Java v0.30
Java
3
star
23

c-bridge-tracer

Experimental C binding for C++ tracers
1
star