Skip to content

Commit 7b37161

Browse files
dyladanmayurkale22
andauthored
Update API docs for beta (#855)
* docs: update API docs for beta * chore: remove duplicate wording * chore: review comments * Update packages/opentelemetry-api/README.md Co-Authored-By: Mayur Kale <[email protected]> Co-authored-by: Mayur Kale <[email protected]>
1 parent e5e7179 commit 7b37161

File tree

1 file changed

+112
-33
lines changed

1 file changed

+112
-33
lines changed

packages/opentelemetry-api/README.md

Lines changed: 112 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,115 @@
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-
## Basic Use
10+
## Quick Start
1111

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

14-
API entry points are defined as global singleton objects `trace` and `metrics` which contain methods used to initialize SDK implementations and acquire resources from the API.
14+
### Install Dependencies
15+
16+
```sh
17+
$ # Install tracing dependencies
18+
$ npm install \
19+
@opentelemetry/core \
20+
@opentelemetry/node \
21+
@opentelemetry/tracing \
22+
@opentelemetry/exporter-jaeger \ # add exporters as needed
23+
@opentelemetry/plugin-http # add plugins as needed
24+
25+
$ # Install metrics dependencies
26+
$ npm install \
27+
@opentelemetry/metrics \
28+
@opentelemetry/exporter-prometheus # add exporters as needed
29+
```
30+
31+
### Initialize the SDK
32+
33+
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.
34+
35+
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.
36+
37+
#### Tracing
38+
39+
```javascript
40+
const { NodeTracerProvider } = require("@opentelemetry/node");
41+
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
42+
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");
43+
44+
const tracerProvider = new NodeTracerProvider();
45+
46+
/**
47+
* The SimpleSpanProcessor does no batching and exports spans
48+
* immediately when they end. For most production use cases,
49+
* OpenTelemetry recommends use of the BatchSpanProcessor.
50+
*/
51+
tracerProvider.addSpanProcessor(
52+
new SimpleSpanProcessor(
53+
new JaegerExporter(
54+
/* export options */
55+
)
56+
)
57+
);
58+
59+
/**
60+
* Registering the provider with the API allows it to be discovered
61+
* and used by instrumentation libraries. The OpenTelemetry API provides
62+
* methods to set global SDK implementations, but the default SDK provides
63+
* a convenience method named `register` which registers sane defaults
64+
* for you.
65+
*
66+
* By default the NodeTracerProvider uses Trace Context for propagation
67+
* and AsyncHooksScopeManager for context management. To learn about
68+
* customizing this behavior, see API Registration Options below.
69+
*/
70+
tracerProvider.register();
71+
```
72+
73+
#### Metrics
74+
75+
```javascript
76+
const api = require("@opentelemetry/api");
77+
const { MeterProvider } = require("@opentelemetry/metrics");
78+
const { PrometheusExporter } = require("@opentelemetry/exporter-prometheus");
79+
80+
const meterProvider = new MeterProvider({
81+
// The Prometheus exporter runs an HTTP server which
82+
// the Prometheus backend scrapes to collect metrics.
83+
exporter: new PrometheusExporter({ startServer: true }),
84+
interval: 1000,
85+
});
86+
87+
/**
88+
* Registering the provider with the API allows it to be discovered
89+
* and used by instrumentation libraries.
90+
*/
91+
api.metrics.setGlobalMeterProvider(meterProvider);
92+
```
93+
94+
## Advanced Use
95+
### API Registration Options
96+
97+
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.
98+
99+
```javascript
100+
const { B3Propagator } = require("@opentelemetry/core");
101+
102+
tracerProvider.register({
103+
// Use B3 Propagation
104+
propagator: new B3Propagator(),
105+
106+
// Skip registering a default context manager
107+
contextManager: null,
108+
})
109+
```
110+
111+
### API Methods
112+
113+
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.
15114

16115
- [Trace API Documentation][trace-api-docs]
17116
- [Metrics API Documentation][metrics-api-docs]
117+
- [Propagation API Documentation][propagation-api-docs]
118+
- [Context API Documentation][context-api-docs]
18119

19120
```javascript
20121
const api = require("@opentelemetry/api")
@@ -32,38 +133,12 @@ api.metrics.setGlobalMeterProvider(meterProvider);
32133
api.metrics.getMeterProvider();
33134
/* returns a meter from the registered global meter provider (no-op if a working provider has not been initialized); */
34135
api.metrics.getMeter(name, version);
35-
```
36-
37-
### Application Owners
38-
39-
Application owners will also need a working OpenTelemetry SDK implementation. OpenTelemetry provides working SDK implementations for [web] and [node] for both [tracing] and [metrics].
40-
41-
#### Simple NodeJS Example
42136

43-
Before any other module in your application is loaded, you must initialize the global tracer and meter registries. If you fail to initialize a provider, no-op implementations will be provided to any library which acquires them from the API.
137+
/* Initialize Propagator */
138+
api.propagator.setGlobalPropagator(httpTraceContextPropagator)
44139

45-
```javascript
46-
const api = require("@opentelemetry/api");
47-
const sdk = require("@opentelemetry/node");
48-
49-
const { SimpleSpanProcessor } = require('@opentelemetry/tracing');
50-
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
51-
52-
// Initialize an exporter
53-
const exporter = new JaegerExporter({
54-
serviceName: 'basic-service'
55-
});
56-
57-
// Create a provider which we will configure as the global tracer provider
58-
const provider = new sdk.NodeTracerProvider();
59-
60-
// Configure span processor to send spans to the exporter
61-
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
62-
63-
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
64-
api.trace.setGlobalTracerProvider(provider);
65-
66-
// your application code below this line
140+
/* Initialize Context Manager */
141+
api.context.setGlobalContextManager(asyncHooksContextManager);
67142
```
68143

69144
### Library Authors
@@ -116,8 +191,12 @@ Apache 2.0 - See [LICENSE][license-url] for more information.
116191

117192
[trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/traceapi.html
118193
[metrics-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/metricsapi.html
194+
[propagation-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/propagationapi.html
195+
[context-api-docs]: https://open-telemetry.github.io/opentelemetry-js/classes/contextapi.html
119196

120197
[web]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-web
121198
[tracing]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing
122199
[node]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-node
123200
[metrics]: https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-metrics
201+
202+
[other-tracing-backends]: https://github.com/open-telemetry/opentelemetry-js#trace-exporters

0 commit comments

Comments
 (0)