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

Commit

Permalink
Merge 7823aa3 into c80af3d
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan committed Mar 11, 2021
2 parents c80af3d + 7823aa3 commit 45b4807
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 124 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ typings/
yarn.lock
package-lock.json

# docs files
docs
# generated gh-pages files
docs/out

.nyc_output

Expand Down
173 changes: 51 additions & 122 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,140 +7,71 @@

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.

## Quick Start
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].

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

### Install Dependencies

```sh
$ # Install tracing dependencies
$ npm install \
@opentelemetry/api \
@opentelemetry/core \
@opentelemetry/node \
@opentelemetry/tracing \
@opentelemetry/exporter-jaeger \ # add exporters as needed
@opentelemetry/plugin-http # add plugins as needed
```

> 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.
### You Will Need

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

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.
**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.

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.

#### Tracing

```javascript
const { NodeTracerProvider } = require("@opentelemetry/node");
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
const { JaegerExporter } = require("@opentelemetry/exporter-jaeger");

const tracerProvider = new NodeTracerProvider();

/**
* The SimpleSpanProcessor does no batching and exports spans
* immediately when they end. For most production use cases,
* OpenTelemetry recommends use of the BatchSpanProcessor.
*/
tracerProvider.addSpanProcessor(
new SimpleSpanProcessor(
new JaegerExporter({
serviceName: 'my-service'
})
)
);

/**
* Registering the provider with the API allows it to be discovered
* and used by instrumentation libraries. The OpenTelemetry API provides
* methods to set global SDK implementations, but the default SDK provides
* a convenience method named `register` which registers same defaults
* for you.
*
* By default the NodeTracerProvider uses Trace Context for propagation
* and AsyncHooksScopeManager for context management. To learn about
* customizing this behavior, see API Registration Options below.
*/
tracerProvider.register();
```
**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].

## Version Compatibility

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.

## Advanced Use

### API Registration Options

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.

```javascript
const { B3Propagator } = require("@opentelemetry/propagator-b3");

tracerProvider.register({
// Use B3 Propagation
propagator: new B3Propagator(),
### Install Dependencies

// Skip registering a default context manager
contextManager: null,
});
```sh
npm install @opentelemetry/api @opentelemetry/tracing
```

### API Methods
### Trace Your Application

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.
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].

- [Trace API Documentation][trace-api-docs]
- [Propagation API Documentation][propagation-api-docs]
- [Context API Documentation][context-api-docs]
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].

```javascript
const api = require("@opentelemetry/api");

/* Initialize TracerProvider */
api.trace.setGlobalTracerProvider(tracerProvider);
/* returns tracerProvider (no-op if a working provider has not been initialized) */
api.trace.getTracerProvider();
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized) */
api.trace.getTracer(name, version);
const { trace } = require("@opentelemetry/api");
const { BasicTracerProvider, ConsoleSpanExporter, SimpleSpanProcessor } = require("@opentelemetry/tracing");

// Create and register an SDK
const provider = new BasicTracerProvider();
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
trace.setGlobalTracerProvider(provider);

// Acquire a tracer from the global tracer provider which will be used to trace the application
const name = 'my-application-name';
const version = '0.1.0';
const tracer = trace.getTracer(name, version);

// Trace your application by creating spans
async function operation() {
const span = tracer.startSpan("do operation");

// mock some work by sleeping 1 second
await new Promise((resolve, reject) => {
setTimeout(resolve, 1000);
})

span.end();
return output;
}

/* Initialize Propagator */
api.propagation.setGlobalPropagator(httpTraceContextPropagator);
async function main() {
while (true) {
await operation();
}
}

/* Initialize Context Manager */
api.context.setGlobalContextManager(asyncHooksContextManager);
main();
```

### Library Authors

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.
## Version Compatibility

```javascript
const api = require("@opentelemetry/api");

const tracer = api.trace.getTracer("my-library-name", "0.2.3");

async function doSomething() {
const span = tracer.startSpan("doSomething");
try {
const result = await doSomethingElse();
span.end();
return result;
} catch (err) {
span.setStatus({
// use an appropriate status code here
code: api.SpanStatusCode.ERROR,
message: err.message,
});
span.end();
return null;
}
}
```
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.

## Useful links

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

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

[opentelemetry-js]: https://github.com/open-telemetry/opentelemetry-js

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

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

[other-tracing-backends]: https://github.com/open-telemetry/opentelemetry-js#trace-exporters
[docs-tracing]: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/docs/tracing.md
[docs-sdk-registration]: https://github.com/open-telemetry/opentelemetry-js-api/blob/main/docs/sdk-registration.md
5 changes: 5 additions & 0 deletions docs/context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Context

TODO

_Context API reference: <https://open-telemetry.github.io/opentelemetry-js-api/classes/contextapi.html>_
3 changes: 3 additions & 0 deletions docs/library-author.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OpenTelemetry for Library Authors

TODO
5 changes: 5 additions & 0 deletions docs/propagation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Propagation

TODO

_Propagation API reference: <https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html>_
28 changes: 28 additions & 0 deletions docs/sdk-registration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SDK Registration Methods

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.

- [Trace API Documentation][trace-api-docs]
- [Propagation API Documentation][propagation-api-docs]
- [Context API Documentation][context-api-docs]

```javascript
const api = require("@opentelemetry/api");

/* Register a global TracerProvider */
api.trace.setGlobalTracerProvider(tracerProvider);
/* returns tracerProvider (no-op if a working provider has not been initialized) */
api.trace.getTracerProvider();
/* returns a tracer from the registered global tracer provider (no-op if a working provider has not been initialized) */
api.trace.getTracer(name, version);

/* Register a global Propagator */
api.propagation.setGlobalPropagator(httpTraceContextPropagator);

/* Register a global Context Manager */
api.context.setGlobalContextManager(asyncHooksContextManager);
```

[trace-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/traceapi.html
[propagation-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html
[context-api-docs]: https://open-telemetry.github.io/opentelemetry-js-api/classes/contextapi.html
Loading

0 comments on commit 45b4807

Please sign in to comment.