UNPKG

8.4 kBMarkdownView Raw
1# OpenTelemetry API for JavaScript
2
3<p align="center">
4<strong>
5<a href="https://open-telemetry.github.io/opentelemetry-js/modules/_opentelemetry_api.html">API Reference</a>
6&nbsp;&nbsp;&bull;&nbsp;&nbsp;
7<a href="https://opentelemetry.io/docs/instrumentation/js/">Documentation</a>
8</br>
9<a href="https://github.com/open-telemetry/opentelemetry-js/releases">
10 <img alt="NPM Release" src="https://img.shields.io/npm/v/@opentelemetry/api?color=brightgreen&logo=data%3Aimage%2Fpng%3Bbase64%2CiVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAIRlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgEoAAMAAAABAAIAAIdpAAQAAAABAAAAWgAAAAAAAACQAAAAAQAAAJAAAAABAAOgAQADAAAAAQABAACgAgAEAAAAAQAAABigAwAEAAAAAQAAABgAAAAA8A2UOAAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAVlpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IlhNUCBDb3JlIDUuNC4wIj4KICAgPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICAgICAgPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIKICAgICAgICAgICAgeG1sbnM6dGlmZj0iaHR0cDovL25zLmFkb2JlLmNvbS90aWZmLzEuMC8iPgogICAgICAgICA8dGlmZjpPcmllbnRhdGlvbj4xPC90aWZmOk9yaWVudGF0aW9uPgogICAgICA8L3JkZjpEZXNjcmlwdGlvbj4KICAgPC9yZGY6UkRGPgo8L3g6eG1wbWV0YT4KTMInWQAABK5JREFUSA2dVm1sFEUYfmd2b%2Ff2Pkqgh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vQAGEo4zvKq2w5G1NxENN7jrft1oxMdekETjxdH2Z3x%2BVTVYsPb%2BO0C%2F9%2FauN6v2hNZw5b2UOmSbG5%2FrkC3LBA%2B1PdxFxORjxpQ81GcxKc%2BybVjEBvUJvaGJ7p7n5A5KSwe4AzkasA%2BcrmzFtowoIVTiLjANm8GDsrWW35ScI3JY8Urv83tnkF8JR0yLvEt2hO%2F0qNyy3Jb3YKeHeHeLeOuVLRpNF%2Bpkf85OW7%2FzJxWdXsbsKBUk2TC0BCPwMq5Q%2FCPvaJFkNS%2F1l1qUPe%2BuH3oD59erYGI%2FY4sce6KaXYElAIOLt%2B0O3t2%2B%2FxJDF1XvOlWGC1W1B8VMszbGfOvT5qaRRAIFK3BCO164nZ0uYLH2YjNN8thXS2v2BK9gTfD7jHVxzHr4roOlEvYYz9QIz%2BVl%2FsLDXInsctFsXjqIRnO2ZO387lxmIboLDZCJ59KLFliNIgh9ipt6tLg9SihpRPDO1ia5byw7de1aCQmF5geOQtK509rzfdwxaKOIq%2B73AvwCC5%2F5fcV4vo3%2B3LpMdtWHh0ywsJC%2FZGoCb8%2F9D8F%2FifgLLl8S8QWfU8cAAAAASUVORK5CYII%3D">
11 </a>
12</strong>
13</p>
14
15This package provides everything needed to interact with the OpenTelemetry API, including all TypeScript interfaces, enums, and no-op implementations. It is intended for use both on the server and in the browser.
16
17The methods in this package perform no operations by default. This means they can be safely called by a library or end-user application whether there is an SDK registered or not. In order to generate and export telemetry data, you will also need an SDK such as the [OpenTelemetry JS SDK][opentelemetry-js].
18
19## Tracing Quick Start
20
21### You Will Need
22
23- An application you wish to instrument
24- [OpenTelemetry JS SDK][opentelemetry-js]
25- Node.js >=8.5.0 (14+ is preferred) or an ECMAScript 5+ compatible browser
26
27**Note:** ECMAScript 5+ compatibility is for this package only. Please refer to the documentation for the SDK you are using to determine its minimum ECMAScript version.
28
29**Note for library authors:** Only your end users will need an OpenTelemetry SDK. If you wish to support OpenTelemetry in your library, you only need to use the OpenTelemetry API. For more information, please read the [tracing documentation][docs-tracing].
30
31### Install Dependencies
32
33```sh
34npm install @opentelemetry/api @opentelemetry/sdk-trace-base
35```
36
37### Trace Your Application
38
39In order to get started with tracing, you will need to first register an SDK. The SDK you are using may provide a convenience method which calls the registration methods for you, but if you would like to call them directly they are documented here: [sdk registration methods][docs-sdk-registration].
40
41Once you have registered an SDK, you can start and end spans. A simple example of basic SDK registration and tracing a simple operation is below. The example should export spans to the console once per second. For more information, see the [tracing documentation][docs-tracing].
42
43```javascript
44const { trace } = require("@opentelemetry/api");
45const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/sdk-trace-base");
46
47// Create and register an SDK
48const provider = new BasicTracerProvider();
49provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
50trace.setGlobalTracerProvider(provider);
51
52// Acquire a tracer from the global tracer provider which will be used to trace the application
53const name = 'my-application-name';
54const version = '0.1.0';
55const tracer = trace.getTracer(name, version);
56
57// Trace your application by creating spans
58async function operation() {
59 const span = tracer.startSpan("do operation");
60
61 // mock some work by sleeping 1 second
62 await new Promise((resolve, reject) => {
63 setTimeout(resolve, 1000);
64 })
65
66 span.end();
67}
68
69async function main() {
70 while (true) {
71 await operation();
72 }
73}
74
75main();
76```
77
78## Version Compatibility
79
80Because the npm installer and node module resolution algorithm could potentially allow two or more copies of any given package to exist within the same `node_modules` structure, the OpenTelemetry API takes advantage of a variable on the `global` object to store the global API. When an API method in the API package is called, it checks if this `global` API exists and proxies calls to it if and only if it is a compatible API version. This means if a package has a dependency on an OpenTelemetry API version which is not compatible with the API used by the end user, the package will receive a no-op implementation of the API.
81
82## Upgrade Guidelines
83
84### 0.21.0 to 1.0.0
85
86No breaking changes
87
88### 0.20.0 to 0.21.0
89
90- [#78](https://github.com/open-telemetry/opentelemetry-js-api/issues/78) `api.context.bind` arguments reversed and `context` is now a required argument.
91- [#46](https://github.com/open-telemetry/opentelemetry-js-api/issues/46) Noop classes and singletons are no longer exported. To create a noop span it is recommended to use `api.trace.wrapSpanContext` with `INVALID_SPAN_CONTEXT` instead of using the `NOOP_TRACER`.
92
93### 1.0.0-rc.3 to 0.20.0
94
95- Removing `TimedEvent` which was not part of spec
96- `HttpBaggage` renamed to `HttpBaggagePropagator`
97- [#45](https://github.com/open-telemetry/opentelemetry-js-api/pull/45) `Span#context` renamed to `Span#spanContext`
98- [#47](https://github.com/open-telemetry/opentelemetry-js-api/pull/47) `getSpan`/`setSpan`/`getSpanContext`/`setSpanContext` moved to `trace` namespace
99- [#55](https://github.com/open-telemetry/opentelemetry-js-api/pull/55) `getBaggage`/`setBaggage`/`createBaggage` moved to `propagation` namespace
100
101## Useful links
102
103- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
104- For more about OpenTelemetry JavaScript: <https://github.com/open-telemetry/opentelemetry-js>
105- For help or feedback on this project, join us in [GitHub Discussions][discussions-url]
106
107## License
108
109Apache 2.0 - See [LICENSE][license-url] for more information.
110
111[opentelemetry-js]: https://github.com/open-telemetry/opentelemetry-js
112
113[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
114[license-url]: https://github.com/open-telemetry/opentelemetry-js/blob/main/api/LICENSE
115[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
116[docs-tracing]: https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/tracing.md
117[docs-sdk-registration]: https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/sdk-registration.md