Skip to content

Commit e089984

Browse files
vreynoldsdyladan
andauthoredJul 26, 2021
docs: add quickstart code example (#2365)
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
1 parent 3bc3452 commit e089984

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed
 

‎README.md

+57-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,63 @@ The current version for each package can be found in the respective `package.jso
7171

7272
### Application Owner
7373

74-
To get started tracing your own application, see the [Getting Started Guide](getting-started/README.md). For more information about automatic instrumentation see [@opentelemetry/node][otel-node], which provides auto-instrumentation for Node.js applications. If the automatic instrumentation does not suit your needs, or you would like to create manual traces, see [@opentelemetry/tracing][otel-tracing]
74+
#### Install Dependencies
75+
76+
```shell
77+
npm install --save @opentelemetry/api
78+
npm install --save @opentelemetry/sdk-node
79+
npm install --save @opentelemetry/auto-instrumentations-node
80+
```
81+
82+
**Note:** `auto-instrumentations-node` is a meta package from [opentelemetry-js-contrib](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/metapackages/auto-instrumentations-node) that provides a simple way to initialize multiple Node.js instrumentations.
83+
84+
#### Instantiate Tracing
85+
86+
```js
87+
// tracing.js
88+
89+
'use strict'
90+
91+
const process = require('process');
92+
const opentelemetry = require('@opentelemetry/sdk-node');
93+
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
94+
const { ConsoleSpanExporter } = require('@opentelemetry/tracing');
95+
const { Resource } = require('@opentelemetry/resources');
96+
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
97+
98+
// configure the SDK to export telemetry data to the console
99+
// enable all auto-instrumentations from the meta package
100+
const traceExporter = new ConsoleSpanExporter();
101+
const sdk = new opentelemetry.NodeSDK({
102+
resource: new Resource({
103+
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service',
104+
}),
105+
traceExporter,
106+
instrumentations: [getNodeAutoInstrumentations()]
107+
});
108+
109+
// initialize the SDK and register with the OpenTelemetry API
110+
// this enables the API to record telemetry
111+
sdk.start()
112+
.then(() => console.log('Tracing initialized'))
113+
.catch((error) => console.log('Error initializing tracing', error));
114+
115+
// gracefully shut down the SDK on process exit
116+
process.on('SIGTERM', () => {
117+
sdk.shutdown()
118+
.then(() => console.log('Tracing terminated'))
119+
.catch((error) => console.log('Error terminating tracing', error))
120+
.finally(() => process.exit(0));
121+
});
122+
```
123+
124+
#### Run Your Application
125+
126+
```shell
127+
node -r ./tracing.js app.js
128+
```
129+
130+
The above example will emit auto-instrumented telemetry about your Node.js application to the console. For a more in-depth example, see the [Getting Started Guide](getting-started/README.md). For more information about automatic instrumentation see [@opentelemetry/node][otel-node], which provides auto-instrumentation for Node.js applications. If the automatic instrumentation does not suit your needs, or you would like to create manual traces, see [@opentelemetry/tracing][otel-tracing]
75131

76132
### Library Author
77133

0 commit comments

Comments
 (0)
Please sign in to comment.