Skip to content

Commit 65defc3

Browse files
committed
Clarify example
1 parent 0e58343 commit 65defc3

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

README.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,54 +42,49 @@ 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()
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 }
73+
sdk.start()
74+
module.exports = { sdk }
8175
```
8276

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

8580
[Fastify-cli](https://github.com/fastify/fastify-cli):
8681

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

9085
Node.js:
9186

92-
- `node --import ./otel.js ./app.js` for esm
87+
- `node --experimental-loader=@opentelemetry/instrumentation/hook.mjs --import ./otel.js ./app.js` for esm
9388
- `node --require ./otel.js ./app.js` for cjs
9489

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

122117
### Manual plugin registration
123118

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

126140
```js
127141
import Fastify from 'fastify';

0 commit comments

Comments
 (0)