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