UNPKG

6.64 kBMarkdownView Raw
1[![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)
2
3
4# OpenTracing API for JavaScript
5
6This library is a JavaScript implementation of Open Tracing API. It is intended for use both on the server and in the browser.
7
8## Required Reading
9
10To fully understand this platform API, it's helpful to be familiar with the [OpenTracing project](http://opentracing.io) and
11[terminology](http://opentracing.io/documentation/pages/spec.html) more specifically.
12
13## Quick Start
14
15Install the package using `npm`:
16
17```bash
18npm install --save opentracing
19```
20
21### Example
22
23The package contains an example using a naive `MockTracer` implementation. To run the example:
24
25```bash
26npm run example
27```
28
29The output should look something like:
30
31```
32Spans:
33 parent_span - 1521ms
34 tag 'custom':'tag value'
35 tag 'alpha':'1000'
36 child_span - 503ms
37 tag 'alpha':'200'
38 tag 'beta':'50'
39```
40
41### Code snippet
42
43In 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.
44
45```js
46const http = require('http');
47const opentracing = require('opentracing');
48
49// NOTE: the default OpenTracing tracer does not record any tracing information.
50// Replace this line with the tracer implementation of your choice.
51const tracer = new opentracing.Tracer();
52
53const span = tracer.startSpan('http_request');
54const opts = {
55 host : 'example.com',
56 method: 'GET',
57 port : '80',
58 path: '/',
59};
60http.request(opts, res => {
61 res.setEncoding('utf8');
62 res.on('error', err => {
63 // assuming no retries, mark the span as failed
64 span.setTag(opentracing.Tags.ERROR, true);
65 span.log({'event': 'error', 'error.object': err, 'message': err.message, 'stack': err.stack});
66 span.finish();
67 });
68 res.on('data', chunk => {
69 span.log({'event': 'data_received', 'chunk_length': chunk.length});
70 });
71 res.on('end', () => {
72 span.log({'event': 'request_end'});
73 span.finish();
74 });
75}).end();
76```
77
78As 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:
79
80```js
81const CustomTracer = require('tracing-implementation-of-your-choice');
82const tracer = new CustomTracer();
83```
84
85### Usage in the browser
86
87The 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:
88
89* `dist/opentracing-browser.min.js` - minified, no runtime checks
90* `dist/opentracing-browser.js` - debug version with runtime checks
91
92### Usage with TypeScript
93
94Since 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.
95This is especially useful for implementors who want to type check their implementation with the base interface.
96
97### Global tracer
98
99The library also provides a global singleton tracer for convenience. This can be set and accessed via the following:
100
101```javascript
102opentracing.initGlobalTracer(new CustomTracer());
103
104const tracer = opentracing.globalTracer();
105```
106
107Note: `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.
108
109## API Documentation
110
111There is a hosted copy of the current generated [ESDoc API Documentation here](https://opentracing-javascript.surge.sh).
112
113## Contributing & developer information
114
115See the [OpenTracing website](http://opentracing.io) for general information on contributing to OpenTracing.
116
117The project is written in TypeScript and built using a npm scripts. Run:
118
119* `npm run build` creates the compiled, distributable JavaScript code in `./lib`
120* `npm run watch` incrementally compiles on file changes
121* `npm run webpack` creates the bundles for the browser in `./dist`
122* `npm test` runs the tests
123* `npm run typedoc` generates the documentation in `./typedoc`
124
125**Note:** The minimum supported Node version for development is `>=6`. Tests can however be run on any version that this project supports (`>=0.10`).
126
127## OpenTracing tracer implementations
128
129*This section is intended for developers wishing to* ***implement their own tracers***. *Developers who simply wish to* ***use OpenTracing*** *can safely ignore this information.*
130
131### Custom tracer implementation
132
133Implementations 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.
134
135### API compatibility testing
136
137If `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.
138
139```javascript
140const apiCompatibilityChecks = require('opentracing/lib/test/api_compatibility.js').default;
141apiCompatibilityChecks(() => new CustomTracer());
142```
143## LICENSE
144
145Apache License 2.0
146
147### MockTracer
148
149A minimal example tracer is provided in the `src/mock_tracer` directory of the source code.
150
151 [ci-img]: https://travis-ci.org/opentracing/opentracing-javascript.svg?branch=master
152 [cov-img]: https://coveralls.io/repos/github/opentracing/opentracing-javascript/badge.svg?branch=master
153 [npm-img]: https://badge.fury.io/js/opentracing.svg
154 [node-img]: http://img.shields.io/node/v/opentracing.svg
155 [ci]: https://travis-ci.org/opentracing/opentracing-javascript
156 [cov]: https://coveralls.io/github/opentracing/opentracing-javascript?branch=master
157 [npm]: https://www.npmjs.com/package/opentracing
158
159