Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 6eaeb00

Browse files
authored
docs: document latest manual tracing (#20)
1 parent c80af3d commit 6eaeb00

File tree

7 files changed

+292
-124
lines changed

7 files changed

+292
-124
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ typings/
6262
yarn.lock
6363
package-lock.json
6464

65-
# docs files
66-
docs
65+
# generated gh-pages files
66+
docs/out
6767

6868
.nyc_output
6969

README.md

Lines changed: 51 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -7,140 +7,71 @@
77

88
This 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.
99

10-
## Quick Start
10+
The 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].
1111

12-
To get started you need to install the SDK and plugins, create a TracerProvider, and register it with the API.
12+
## Tracing Quick Start
1313

14-
### Install Dependencies
15-
16-
```sh
17-
$ # Install tracing dependencies
18-
$ npm install \
19-
@opentelemetry/api \
20-
@opentelemetry/core \
21-
@opentelemetry/node \
22-
@opentelemetry/tracing \
23-
@opentelemetry/exporter-jaeger \ # add exporters as needed
24-
@opentelemetry/plugin-http # add plugins as needed
25-
```
26-
27-
> Note: this example is for node.js. See [examples/tracer-web](https://github.com/open-telemetry/opentelemetry-js/tree/main/examples/tracer-web) for a browser example.
14+
### You Will Need
2815

29-
### Initialize the SDK
16+
- An application you wish to instrument
17+
- [OpenTelemetry JS SDK][opentelemetry-js]
18+
- Node.js >=8.5.0 (14+ is preferred) or an ECMAScript 5+ compatible browser
3019

31-
Before any other module in your application is loaded, you must initialize the global tracer and meter providers. If you fail to initialize a provider, no-op implementations will be provided to any library which acquires them from the API.
20+
**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.
3221

33-
To collect traces and metrics, you will have to tell the SDK where to export telemetry data to. This example uses Jaeger and Prometheus, but exporters exist for [other tracing backends][other-tracing-backends]. If you're not sure if there is an exporter for your tracing backend, contact your tracing provider.
34-
35-
#### Tracing
36-
37-
```javascript
38-
const { NodeTracerProvider } = require("@opentelemetry/node");
39-
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
40-
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
41-
42-
const tracerProvider = new NodeTracerProvider();
43-
44-
/**
45-
* The SimpleSpanProcessor does no batching and exports spans
46-
* immediately when they end. For most production use cases,
47-
* OpenTelemetry recommends use of the BatchSpanProcessor.
48-
*/
49-
tracerProvider.addSpanProcessor(
50-
new SimpleSpanProcessor(
51-
new JaegerExporter({
52-
serviceName: 'my-service'
53-
})
54-
)
55-
);
56-
57-
/**
58-
* Registering the provider with the API allows it to be discovered
59-
* and used by instrumentation libraries. The OpenTelemetry API provides
60-
* methods to set global SDK implementations, but the default SDK provides
61-
* a convenience method named `register` which registers same defaults
62-
* for you.
63-
*
64-
* By default the NodeTracerProvider uses Trace Context for propagation
65-
* and AsyncHooksScopeManager for context management. To learn about
66-
* customizing this behavior, see API Registration Options below.
67-
*/
68-
tracerProvider.register();
69-
```
22+
**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].
7023

71-
## Version Compatibility
72-
73-
Because 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.
74-
75-
## Advanced Use
76-
77-
### API Registration Options
78-
79-
If you prefer to choose your own propagator or context manager, you may pass an options object into the `tracerProvider.register()` method. Omitted or `undefined` options will be replaced by a default value and `null` values will be skipped.
80-
81-
```javascript
82-
const { B3Propagator } = require("@opentelemetry/propagator-b3");
83-
84-
tracerProvider.register({
85-
// Use B3 Propagation
86-
propagator: new B3Propagator(),
24+
### Install Dependencies
8725

88-
// Skip registering a default context manager
89-
contextManager: null,
90-
});
26+
```sh
27+
npm install @opentelemetry/api @opentelemetry/tracing
9128
```
9229

93-
### API Methods
30+
### Trace Your Application
9431

95-
If you are writing an instrumentation library, or prefer to call the API methods directly rather than using the `register` method on the Tracer/Meter Provider, OpenTelemetry provides direct access to the underlying API methods through the `@opentelemetry/api` package. API entry points are defined as global singleton objects `trace`, `metrics`, `propagation`, and `context` which contain methods used to initialize SDK implementations and acquire resources from the API.
32+
In 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].
9633

97-
- [Trace API Documentation][trace-api-docs]
98-
- [Propagation API Documentation][propagation-api-docs]
99-
- [Context API Documentation][context-api-docs]
34+
Once 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].
10035

10136
```javascript
102-
const api = require("@opentelemetry/api");
103-
104-
/* Initialize TracerProvider */
105-
api.trace.setGlobalTracerProvider(tracerProvider);
106-
/* returns tracerProvider (no-op if a working provider has not been initialized) */
107-
api.trace.getTracerProvider();
108-
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized) */
109-
api.trace.getTracer(name, version);
37+
const { trace } = require("@opentelemetry/api");
38+
const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/tracing");
39+
40+
// Create and register an SDK
41+
const provider = new BasicTracerProvider();
42+
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
43+
trace.setGlobalTracerProvider(provider);
44+
45+
// Acquire a tracer from the global tracer provider which will be used to trace the application
46+
const name = 'my-application-name';
47+
const version = '0.1.0';
48+
const tracer = trace.getTracer(name, version);
49+
50+
// Trace your application by creating spans
51+
async function operation() {
52+
const span = tracer.startSpan("do operation");
53+
54+
// mock some work by sleeping 1 second
55+
await new Promise((resolve, reject) => {
56+
setTimeout(resolve, 1000);
57+
})
58+
59+
span.end();
60+
return output;
61+
}
11062

111-
/* Initialize Propagator */
112-
api.propagation.setGlobalPropagator(httpTraceContextPropagator);
63+
async function main() {
64+
while (true) {
65+
await operation();
66+
}
67+
}
11368

114-
/* Initialize Context Manager */
115-
api.context.setGlobalContextManager(asyncHooksContextManager);
69+
main();
11670
```
11771

118-
### Library Authors
119-
120-
Library authors need only to depend on the `@opentelemetry/api` package and trust that the application owners which use their library will initialize an appropriate SDK.
72+
## Version Compatibility
12173

122-
```javascript
123-
const api = require("@opentelemetry/api");
124-
125-
const tracer = api.trace.getTracer("my-library-name", "0.2.3");
126-
127-
async function doSomething() {
128-
const span = tracer.startSpan("doSomething");
129-
try {
130-
const result = await doSomethingElse();
131-
span.end();
132-
return result;
133-
} catch (err) {
134-
span.setStatus({
135-
// use an appropriate status code here
136-
code: api.SpanStatusCode.ERROR,
137-
message: err.message,
138-
});
139-
span.end();
140-
return null;
141-
}
142-
}
143-
```
74+
Because 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.
14475

14576
## Useful links
14677

@@ -152,6 +83,8 @@ async function doSomething() {
15283

15384
Apache 2.0 - See [LICENSE][license-url] for more information.
15485

86+
[opentelemetry-js]: https://github.com/open-telemetry/opentelemetry-js
87+
15588
[discussions-url]: https://github.com/open-telemetry/opentelemetry-js/discussions
15689
[license-url]: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/LICENSE
15790
[license-image]: https://img.shields.io/badge/license-Apache_2.0-green.svg?style=flat
@@ -162,9 +95,5 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
16295
[npm-url]: https://www.npmjs.com/package/@opentelemetry/api
16396
[npm-img]: https://badge.fury.io/js/%40opentelemetry%2Fapi.svg
16497

165-
[trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/traceapi.html
166-
[metrics-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/metricsapi.html
167-
[propagation-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html
168-
[context-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/contextapi.html
169-
170-
[other-tracing-backends]: https://github.com/open-telemetry/opentelemetry-js#trace-exporters
98+
[docs-tracing]: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/docs/tracing.md
99+
[docs-sdk-registration]: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/docs/sdk-registration.md

docs/context.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Context
2+
3+
TODO
4+
5+
_Context API reference: <https://open-telemetry.github.io/opentelemetry-js-api/classes/contextapi.html>_

docs/library-author.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# OpenTelemetry for Library Authors
2+
3+
TODO

docs/propagation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Propagation
2+
3+
TODO
4+
5+
_Propagation API reference: <https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html>_

docs/sdk-registration.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SDK Registration Methods
2+
3+
These methods are used to register a compatible OpenTelemetry SDK. Some SDKs like the [OpenTelemetry JS SDK][opentelemetry-js] provide convenience methods which call these registration methods for you.
4+
5+
- [Trace API Documentation][trace-api-docs]
6+
- [Propagation API Documentation][propagation-api-docs]
7+
- [Context API Documentation][context-api-docs]
8+
9+
```javascript
10+
const api = require("@opentelemetry/api");
11+
12+
/* Register a global TracerProvider */
13+
api.trace.setGlobalTracerProvider(tracerProvider);
14+
/* returns tracerProvider (no-op if a working provider has not been initialized) */
15+
api.trace.getTracerProvider();
16+
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized) */
17+
api.trace.getTracer(name, version);
18+
19+
/* Register a global Propagator */
20+
api.propagation.setGlobalPropagator(httpTraceContextPropagator);
21+
22+
/* Register a global Context Manager */
23+
api.context.setGlobalContextManager(asyncHooksContextManager);
24+
```
25+
26+
[trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/traceapi.html
27+
[propagation-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html
28+
[context-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/contextapi.html

0 commit comments

Comments
 (0)