Skip to content

Commit cd2e427

Browse files
committed
Clarify example
1 parent 0e58343 commit cd2e427

File tree

1 file changed

+41
-18
lines changed

1 file changed

+41
-18
lines changed

README.md

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,58 @@ const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node')
4242
const { SimpleSpanProcessor } = require('@opentelemetry/sdk-trace-base')
4343
const { PrometheusExporter } = require('@opentelemetry/exporter-prometheus')
4444
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http')
45-
const { RuntimeNodeInstrumentation } = require('@opentelemetry/instrumentation-runtime-node')
46-
const { HostMetrics } = require('@opentelemetry/host-metrics')
4745
const { FastifyOtelInstrumentation } = require('@fastify/otel')
4846
const { metrics, trace } = require('@opentelemetry/api')
4947
const { resourceFromAttributes } = require('@opentelemetry/resources')
5048
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions')
5149

52-
// Console exporter for development-time span inspection
53-
// We could use '@opentelemetry/exporter-trace-otlp-http' instead
50+
// Set up your preferred processors and exporters
5451
const traceExporter = new ConsoleSpanExporter()
5552
const spanProcessor = new SimpleSpanProcessor(traceExporter)
56-
5753
const prometheusExporter = new PrometheusExporter({ port: 9091 })
5854

5955
const sdk = new NodeSDK({
6056
resource: resourceFromAttributes({
57+
// This can also be set by OTEL_SERVICE_NAME
58+
// Instruments inherit from the SDK resource attributes
6159
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service-name',
6260
}),
6361
spanProcessor,
6462
metricReader: prometheusExporter,
6563
instrumentations: [
64+
// HttpInstrumentation is required for FastifyOtelInstrumentation to work
6665
new HttpInstrumentation(),
67-
new RuntimeNodeInstrumentation(),
66+
new FastifyOtelInstrumentation({
67+
// Automatically register the @fastify/otel fastify plugin for all routes
68+
registerOnInitialization: true,
69+
})
6870
],
6971
})
7072

71-
await sdk.start()
73+
sdk.start()
74+
module.exports = { sdk }
7275

73-
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({
74-
registerOnInitialization: true,
76+
process.on('SIGTERM', () => {
77+
// @fastify/otel doesn't set up graceful shutdown of span processing
78+
sdk.shutdown()
79+
.then(
80+
() => console.log('SDK shut down successfully'),
81+
(err) => console.log(new Error('Error shutting down SDK', { cause: err }))
82+
)
7583
})
76-
fastifyOtelInstrumentation.setTracerProvider(trace.getTracerProvider())
77-
78-
new HostMetrics({ meterProvider: metrics.getMeterProvider() }).start()
79-
80-
module.exports = { sdk, fastifyOtelInstrumentation }
8184
```
8285

83-
If `registerOnInitialization=true`, use a loader to load your otel.js before everything else.
86+
Otel recommends using the loader/require flag for loading your otel setup file.
87+
For ESM ("type"="module"), you must also pass a `--experimental-loader` flag.
8488

8589
[Fastify-cli](https://github.com/fastify/fastify-cli):
8690

87-
- `fastify start --import otel.js` for esm
91+
- `NODE_OPTIONS='--import otel.js --experimental-loader=@opentelemetry/instrumentation/hook.mjs' fastify start` for esm
8892
- `fastify start --require otel.js` for cjs
8993

9094
Node.js:
9195

92-
- `node --import ./otel.js ./app.js` for esm
96+
- `node --experimental-loader=@opentelemetry/instrumentation/hook.mjs --import ./otel.js ./app.js` for esm
9397
- `node --require ./otel.js ./app.js` for cjs
9498

9599
```js
@@ -121,7 +125,26 @@ console.log('⚡ Fastify listening on http://localhost:3000');
121125

122126
### Manual plugin registration
123127

124-
If `registerOnInitialization=false`, you must register the fastify plugin before defining any routes.
128+
You can also export your fastifyOtelInstrumentation to register the plugin on specific routes instead of all routes.
129+
130+
```js
131+
// otel.js
132+
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({
133+
// Set this to false to not automatically install the fastify plugin on all routes
134+
registerOnInitialization: false,
135+
})
136+
137+
const sdk = new NodeSDK({
138+
// ...
139+
instrumentations: [
140+
// ...
141+
fastifyOtelInstrumentation
142+
],
143+
})
144+
145+
sdk.start()
146+
module.exports = { sdk, fastifyOtelInstrumentation }
147+
```
125148

126149
```js
127150
import Fastify from 'fastify';

0 commit comments

Comments
 (0)