@@ -42,54 +42,49 @@ const { ConsoleSpanExporter } = require('@opentelemetry/sdk-trace-node')
42
42
const { SimpleSpanProcessor } = require (' @opentelemetry/sdk-trace-base' )
43
43
const { PrometheusExporter } = require (' @opentelemetry/exporter-prometheus' )
44
44
const { HttpInstrumentation } = require (' @opentelemetry/instrumentation-http' )
45
- const { RuntimeNodeInstrumentation } = require (' @opentelemetry/instrumentation-runtime-node' )
46
- const { HostMetrics } = require (' @opentelemetry/host-metrics' )
47
45
const { FastifyOtelInstrumentation } = require (' @fastify/otel' )
48
46
const { metrics , trace } = require (' @opentelemetry/api' )
49
47
const { resourceFromAttributes } = require (' @opentelemetry/resources' )
50
48
const { SemanticResourceAttributes } = require (' @opentelemetry/semantic-conventions' )
51
49
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
54
51
const traceExporter = new ConsoleSpanExporter ()
55
52
const spanProcessor = new SimpleSpanProcessor (traceExporter)
56
-
57
53
const prometheusExporter = new PrometheusExporter ({ port: 9091 })
58
54
59
55
const sdk = new NodeSDK ({
60
56
resource: resourceFromAttributes ({
57
+ // This can also be set by OTEL_SERVICE_NAME
58
+ // Instruments inherit from the SDK resource attributes
61
59
[SemanticResourceAttributes .SERVICE_NAME ]: ' my-service-name' ,
62
60
}),
63
61
spanProcessor,
64
62
metricReader: prometheusExporter,
65
63
instrumentations: [
64
+ // HttpInstrumentation is required for FastifyOtelInstrumentation to work
66
65
new HttpInstrumentation (),
67
- new RuntimeNodeInstrumentation (),
66
+ new FastifyOtelInstrumentation ({
67
+ // Automatically register the @fastify/otel fastify plugin for all routes
68
+ registerOnInitialization: true ,
69
+ })
68
70
],
69
71
})
70
72
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 }
81
75
```
82
76
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.
84
79
85
80
[ Fastify-cli] ( https://github.com/fastify/fastify-cli ) :
86
81
87
- - ` fastify start --import otel.js` for esm
82
+ - ` NODE_OPTIONS=' --import otel.js --experimental-loader=@opentelemetry/instrumentation/hook.mjs' fastify start ` for esm
88
83
- ` fastify start --require otel.js ` for cjs
89
84
90
85
Node.js:
91
86
92
- - ` node --import ./otel.js ./app.js ` for esm
87
+ - ` node --experimental-loader=@opentelemetry/instrumentation/hook.mjs -- import ./otel.js ./app.js ` for esm
93
88
- ` node --require ./otel.js ./app.js ` for cjs
94
89
95
90
``` js
@@ -121,7 +116,26 @@ console.log('⚡ Fastify listening on http://localhost:3000');
121
116
122
117
### Manual plugin registration
123
118
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
+ ` ` `
125
139
126
140
` ` ` js
127
141
import Fastify from 'fastify';
0 commit comments