Skip to content

Commit 4d31cb7

Browse files
committed
Clarify example
1 parent 7d2932a commit 4d31cb7

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

README.md

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,56 +42,62 @@ 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

99+
See [opentelemetry-js/doc/esm-support.md](https://github.com/open-telemetry/opentelemetry-js/blob/main/doc/esm-support.md) for more information around loading otel inside of ESM.
100+
95101
```js
96102
// app.js
97103
import Fastify from 'fastify';
@@ -116,12 +122,31 @@ app.register(async (instance) => {
116122
}, { prefix: '/nested' });
117123

118124
await app.listen({ port: 3000 });
119-
console.log('⚡ Fastify listening on http://localhost:3000');
120125
````
121126

122127
### Manual plugin registration
123128

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

126151
```js
127152
import Fastify from 'fastify';

0 commit comments

Comments
 (0)