Skip to content

Commit

Permalink
chore: update examples to use register (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Mar 12, 2020
1 parent c842117 commit 02c1d66
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 114 deletions.
12 changes: 10 additions & 2 deletions examples/basic-tracer-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ const exporter = new JaegerExporter({ serviceName: 'basic-service' });
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
/**
* Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings.
*
* This registers the tracer provider with the OpenTelemetry API as the global
* tracer provider. This means when you call API methods like
* `opentelemetry.trace.getTracer`, they will use this tracer provider. If you
* do not register a global tracer provider, instrumentation which calls these
* methods will recieve no-op implementations.
*/
provider.register();
const tracer = opentelemetry.trace.getTracer('example-basic-tracer-node');

// Create a span. A span must be closed.
Expand Down
67 changes: 0 additions & 67 deletions examples/basic-tracer-node/multi_exporter.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/dns/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = (serviceName) => {
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

return opentelemetry.trace.getTracer('dns-example');
};
2 changes: 1 addition & 1 deletion examples/express/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = (serviceName) => {
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

return opentelemetry.trace.getTracer('express-example');
};
4 changes: 2 additions & 2 deletions examples/grpc/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = (serviceName) => {

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('grpc-example');
};
4 changes: 2 additions & 2 deletions examples/grpc_dynamic_codegen/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ module.exports = (serviceName) => {
// and optimization, especially in production.
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('grpc-dynamic-codegen-example');
};
4 changes: 2 additions & 2 deletions examples/http/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module.exports = (serviceName) => {

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('http-example');
};
4 changes: 2 additions & 2 deletions examples/https/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = (serviceName) => {

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('https-example');
};
2 changes: 1 addition & 1 deletion examples/ioredis/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ const exporter = new JaegerExporter({ serviceName: 'ioredis-example' });
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

module.exports = opentelemetry.trace.getTracer('ioredis-example');
4 changes: 2 additions & 2 deletions examples/mysql/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = (serviceName) => {
serviceName,
})));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('mysql-example');
};
2 changes: 1 addition & 1 deletion examples/postgres/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module.exports = (serviceName) => {
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

return opentelemetry.trace.getTracer('example-postgres');
};
4 changes: 2 additions & 2 deletions examples/redis/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = (serviceName) => {

provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();

return opentelemetry.trace.getTracer('redis-example');
};
4 changes: 2 additions & 2 deletions examples/stackdriver-trace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const provider = new BasicTracerProvider();
// Configure span processor to send spans to the provided exporter
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

// Initialize the OpenTelemetry APIs to use the BasicTracerProvider bindings
opentelemetry.trace.setGlobalTracerProvider(provider);
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings
provider.register();
const tracer = opentelemetry.trace.getTracer('stackdriver-basic');

// Create a span. A span must be closed.
Expand Down
9 changes: 4 additions & 5 deletions getting-started/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ Create a file named `tracing.js` and add the following code:
```javascript
'use strict';

const opentelemetry = require("@opentelemetry/api");
const { LogLevel } = require("@opentelemetry/core");
const { NodeTracerProvider } = require("@opentelemetry/node");

const provider = new NodeTracerProvider({
logLevel: opentelemetry.LogLevel.ERROR
logLevel: LogLevel.ERROR
});

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();
```

If you run your application now with `node -r ./tracing.js app.js`, your application will create and propagate traces over HTTP. If an already instrumented service that supports [Trace Context](https://www.w3.org/TR/trace-context/) headers calls your application using HTTP, and you call another application using HTTP, the Trace Context headers will be correctly propagated.
Expand Down Expand Up @@ -109,7 +109,6 @@ After these dependencies are installed, we will need to initialize and register
```javascript
'use strict';

const opentelemetry = require("@opentelemetry/api");
const { LogLevel } = require("@opentelemetry/core");
const { NodeTracerProvider } = require("@opentelemetry/node");
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
Expand All @@ -119,7 +118,7 @@ const provider = new NodeTracerProvider({
logLevel: LogLevel.ERROR
});

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

provider.addSpanProcessor(
new SimpleSpanProcessor(
Expand Down
6 changes: 3 additions & 3 deletions getting-started/traced-example/tracing.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use strict";

const opentelemetry = require("@opentelemetry/api");
const { LogLevel } = require("@opentelemetry/core");
const { NodeTracerProvider } = require("@opentelemetry/node");
const { SimpleSpanProcessor } = require("@opentelemetry/tracing");
const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin");

const provider = new NodeTracerProvider({ logLevel: opentelemetry.LogLevel.ERROR });
opentelemetry.trace.setGlobalTracerProvider(provider);
const provider = new NodeTracerProvider({ logLevel: LogLevel.ERROR });

provider.addSpanProcessor(
new SimpleSpanProcessor(
Expand All @@ -19,4 +18,5 @@ provider.addSpanProcessor(
)
);

provider.register();
console.log("tracing initialized");
8 changes: 4 additions & 4 deletions getting-started/ts-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const provider: NodeTracerProvider = new NodeTracerProvider({
logLevel: LogLevel.ERROR
});

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();
```

If you run your application now with `ts-node -r ./tracing.ts app.ts`, your application will create and propagate traces over HTTP. If an already instrumented service that supports [Trace Context](https://www.w3.org/TR/trace-context/) headers calls your application using HTTP, and you call another application using HTTP, the Trace Context headers will be correctly propagated.
Expand All @@ -107,7 +107,7 @@ $ # npm install @opentelemetry/exporter-jaeger
After these dependencies are installed, we will need to initialize and register them. Modify `tracing.ts` so that it matches the following code snippet, replacing the service name `"getting-started"` with your own service name if you wish.

```typescript
import * as opentelemetry from '@opentelemetry/api';
import { LogLevel } from '@opentelemetry/core';
import { NodeTracerProvider } from '@opentelemetry/node';

import { SimpleSpanProcessor } from '@opentelemetry/tracing';
Expand All @@ -116,10 +116,10 @@ import { ZipkinExporter } from '@opentelemetry/exporter-zipkin';
// import { JaegerExporter } from '@opentelemetry/exporter-jaeger';

const provider: NodeTracerProvider = new NodeTracerProvider({
logLevel: opentelemetry.LogLevel.ERROR
logLevel: LogLevel.ERROR
});

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

provider.addSpanProcessor(
new SimpleSpanProcessor(
Expand Down
6 changes: 3 additions & 3 deletions getting-started/ts-example/tracing.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as opentelemetry from "@opentelemetry/api";
import { LogLevel } from '@opentelemetry/core';
import { NodeTracerProvider } from "@opentelemetry/node";
import { SimpleSpanProcessor } from "@opentelemetry/tracing";
import { ZipkinExporter } from "@opentelemetry/exporter-zipkin";

const provider: NodeTracerProvider = new NodeTracerProvider({
logLevel: opentelemetry.LogLevel.ERROR
logLevel: LogLevel.ERROR
});

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

provider.addSpanProcessor(
new SimpleSpanProcessor(
Expand Down
6 changes: 2 additions & 4 deletions packages/opentelemetry-exporter-collector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ npm install --save @opentelemetry/exporter-collector

## Usage in Web
```js
import * as opentelemetry from '@opentelemetry/api';
import { SimpleSpanProcessor } from '@opentelemetry/tracing';
import { WebTracerProvider } from '@opentelemetry/web';
import { CollectorExporter } from '@opentelemetry/exporter-collector'
Expand All @@ -28,13 +27,12 @@ const provider = new WebTracerProvider();
const exporter = new CollectorExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

```

## Usage in Node
```js
const opentelemetry = require('@opentelemetry/api');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

Expand All @@ -46,7 +44,7 @@ const provider = new BasicTracerProvider();
const exporter = new CollectorExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register();

```

Expand Down
10 changes: 4 additions & 6 deletions packages/opentelemetry-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,22 @@ npm install --save @opentelemetry/plugin-https
The following code will configure the `NodeTracerProvider` to instrument `http` using `@opentelemetry/plugin-http`.

```js
const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');
const { NodeTracerProvider } = require('@/node');

// Create and configure NodeTracerProvider
const provider = new NodeTracerProvider({
plugins: {
http: {
enabled: true,
// You may use a package name or absolute path to the file.
path: '@opentelemetry/plugin-http',
path: '@/plugin-http',
// http plugin options
}
}
});

// Initialize the provider
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register()

// Your application code - http will automatically be instrumented if
// @opentelemetry/plugin-http is present
Expand All @@ -78,14 +77,13 @@ const http = require('http');
To enable instrumentation for all [supported modules](https://github.com/open-telemetry/opentelemetry-js#plugins), create an instance of `NodeTracerProvider` without providing any plugin configuration to the constructor.

```js
const opentelemetry = require('@opentelemetry/api');
const { NodeTracerProvider } = require('@opentelemetry/node');

// Create and initialize NodeTracerProvider
const provider = new NodeTracerProvider();

// Initialize the provider
opentelemetry.trace.setGlobalTracerProvider(provider);
provider.register()

// Your application code
// ...
Expand Down
5 changes: 3 additions & 2 deletions packages/opentelemetry-tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const opentelemetry = require('@opentelemetry/api');
const { BasicTracerProvider } = require('@opentelemetry/tracing');

// To start a trace, you first need to initialize the Tracer provider.
// NOTE: the default OpenTelemetry tracer provider does not record any tracing information.
opentelemetry.trace.setGlobalTracerProvider(new BasicTracerProvider());
// NOTE: The default OpenTelemetry tracer provider does not record any tracing information.
// Registering a working tracer provider allows the API methods to record traces.
new BasicTracerProvider().register();

// To create a span in a trace, we used the global singleton tracer to start a new span.
const span = opentelemetry.trace.getTracer('default').startSpan('foo');
Expand Down

0 comments on commit 02c1d66

Please sign in to comment.