Skip to content

Commit b9201a2

Browse files
committed
Clarify example
1 parent 0e58343 commit b9201a2

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

README.md

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,48 @@ 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
5450
const traceExporter = new ConsoleSpanExporter()
5551
const spanProcessor = new SimpleSpanProcessor(traceExporter)
56-
5752
const prometheusExporter = new PrometheusExporter({ port: 9091 })
5853

5954
const sdk = new NodeSDK({
6055
resource: resourceFromAttributes({
56+
// This can also be set by OTEL_SERVICE_NAME
57+
// Instruments inherit from the SDK resource attributes
6158
[SemanticResourceAttributes.SERVICE_NAME]: 'my-service-name',
6259
}),
6360
spanProcessor,
6461
metricReader: prometheusExporter,
6562
instrumentations: [
63+
// HttpInstrumentation is required for FastifyOtelInstrumentation to work
6664
new HttpInstrumentation(),
67-
new RuntimeNodeInstrumentation(),
65+
new FastifyOtelInstrumentation({
66+
// Automatically register the @fastify/otel fastify plugin for all routes
67+
registerOnInitialization: true,
68+
})
6869
],
6970
})
7071

71-
await sdk.start()
72-
73-
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({
74-
registerOnInitialization: true,
75-
})
76-
fastifyOtelInstrumentation.setTracerProvider(trace.getTracerProvider())
77-
78-
new HostMetrics({ meterProvider: metrics.getMeterProvider() }).start()
79-
80-
module.exports = { sdk, fastifyOtelInstrumentation }
72+
sdk.start()
73+
module.exports = { sdk }
8174
```
8275

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

8579
[Fastify-cli](https://github.com/fastify/fastify-cli):
8680

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

9084
Node.js:
9185

92-
- `node --import ./otel.js ./app.js` for esm
86+
- `node --experimental-loader=@opentelemetry/instrumentation/hook.mjs --import ./otel.js ./app.js` for esm
9387
- `node --require ./otel.js ./app.js` for cjs
9488

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

122116
### Manual plugin registration
123117

124-
If `registerOnInitialization=false`, you must register the fastify plugin before defining any routes.
118+
You can also export your fastifyOtelInstrumentation to register the plugin on specific routes instead of all routes.
119+
120+
```js
121+
// otel.js
122+
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({
123+
// Set this to false to not automatically install the fastify plugin on all routes
124+
registerOnInitialization: false,
125+
})
126+
127+
const sdk = new NodeSDK({
128+
// ...
129+
instrumentations: [
130+
// ...
131+
fastifyOtelInstrumentation
132+
],
133+
})
134+
135+
sdk.start()
136+
module.exports = { sdk, fastifyOtelInstrumentation }
137+
```
125138

126139
```js
127140
import Fastify from 'fastify';

0 commit comments

Comments
 (0)