@@ -42,54 +42,58 @@ 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 ()
73
+ sdk .start ()
74
+ module .exports = { sdk }
72
75
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
+ )
75
83
})
76
- fastifyOtelInstrumentation .setTracerProvider (trace .getTracerProvider ())
77
-
78
- new HostMetrics ({ meterProvider: metrics .getMeterProvider () }).start ()
79
-
80
- module .exports = { sdk, fastifyOtelInstrumentation }
81
84
```
82
85
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.
84
88
85
89
[ Fastify-cli] ( https://github.com/fastify/fastify-cli ) :
86
90
87
- - ` fastify start --import otel.js` for esm
91
+ - ` NODE_OPTIONS=' --import otel.js --experimental-loader=@opentelemetry/instrumentation/hook.mjs' fastify start ` for esm
88
92
- ` fastify start --require otel.js ` for cjs
89
93
90
94
Node.js:
91
95
92
- - ` node --import ./otel.js ./app.js ` for esm
96
+ - ` node --experimental-loader=@opentelemetry/instrumentation/hook.mjs -- import ./otel.js ./app.js ` for esm
93
97
- ` node --require ./otel.js ./app.js ` for cjs
94
98
95
99
``` js
@@ -121,7 +125,26 @@ console.log('⚡ Fastify listening on http://localhost:3000');
121
125
122
126
### Manual plugin registration
123
127
124
- If ` registerOnInitialization=false` , you must register the fastify plugin before defining any routes.
128
+ You can also export your fastifyOtelInstrumentation to register the plugin on specific routes instead of all routes .
129
+
130
+ ` ` ` js
131
+ // otel.js
132
+ const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({
133
+ // Set this to false to not automatically install the fastify plugin on all routes
134
+ registerOnInitialization: false,
135
+ })
136
+
137
+ const sdk = new NodeSDK({
138
+ // ...
139
+ instrumentations: [
140
+ // ...
141
+ fastifyOtelInstrumentation
142
+ ],
143
+ })
144
+
145
+ sdk.start()
146
+ module.exports = { sdk, fastifyOtelInstrumentation }
147
+ ` ` `
125
148
126
149
` ` ` js
127
150
import Fastify from 'fastify';
0 commit comments