Skip to content

Commit 896ba63

Browse files
authored
feat: auto register(#22)
1 parent d38a53b commit 896ba63

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ app.register((instance, opts, done) => {
6868
}, { prefix: '/nested' })
6969
```
7070

71+
### Automatic plugin registration
72+
73+
The plugin can be automatically registered with `registerOnInitialization` option set to `true`.
74+
In this case, it is necessary to await fastify instance.
75+
```js
76+
// ... in your OTEL setup
77+
const fastifyOtelInstrumentation = new FastifyOtelInstrumentation({ registerOnInitialization: true });
78+
79+
// ... in your Fastify definition
80+
const Fastify = require('fastify');
81+
const app = await fastify();
82+
```
83+
7184
> **Notes**:
7285
>
7386
> - This instrumentation requires `@opentelemetry/instrumentation-http` to be able to propagate the traces all the way back to upstream

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { FastifyInstance } from 'fastify'
66
export interface FastifyOtelOptions {}
77
export interface FastifyOtelInstrumentationOpts extends InstrumentationConfig {
88
servername?: string
9+
registerOnInitialization?: boolean
910
}
1011

1112
declare class FastifyOtelInstrumentation<Config extends FastifyOtelInstrumentationOpts = FastifyOtelInstrumentationOpts> extends InstrumentationBase<Config> {

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict'
2+
const dc = require('node:diagnostics_channel')
23
const { context, trace, SpanStatusCode } = require('@opentelemetry/api')
34
const { getRPCMetadata, RPCType } = require('@opentelemetry/core')
45
const {
@@ -56,6 +57,25 @@ class FastifyOtelInstrumentation extends InstrumentationBase {
5657
this.servername = config?.servername ?? process.env.OTEL_SERVICE_NAME ?? 'fastify'
5758
}
5859

60+
enable () {
61+
if (this._handleInitialization === undefined && this.getConfig().registerOnInitialization) {
62+
const FastifyInstrumentationPlugin = this.plugin()
63+
this._handleInitialization = (message) => {
64+
message.fastify.register(FastifyInstrumentationPlugin)
65+
}
66+
dc.subscribe('fastify.initialization', this._handleInitialization)
67+
}
68+
return super.enable()
69+
}
70+
71+
disable () {
72+
if (this._handleInitialization) {
73+
dc.unsubscribe('fastify.initialization', this._handleInitialization)
74+
this._handleInitialization = undefined
75+
}
76+
return super.disable()
77+
}
78+
5979
// We do not do patching in this instrumentation
6080
init () {
6181
return []

test/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ describe('FastifyInstrumentation', () => {
9191
memoryExporter.reset()
9292
})
9393

94+
test('should attach plugin if registerOnInitialization is true', async () => {
95+
const instrumentation = new FastifyInstrumentation({ registerOnInitialization: true })
96+
assert.notEqual(instrumentation._handleInitialization, undefined)
97+
98+
const app = await Fastify()
99+
assert.ok(app.hasPlugin('@fastify/otel'))
100+
app.close()
101+
102+
instrumentation.disable()
103+
assert.equal(instrumentation._handleInitialization, undefined)
104+
})
105+
106+
test('shouldn\'t attach plugin if registerOnInitialization isn\'t set', async () => {
107+
const instrumentation = new FastifyInstrumentation()
108+
assert.equal(instrumentation._handleInitialization, undefined)
109+
110+
const app = await Fastify()
111+
assert.equal(app.hasPlugin('@fastify/otel'), false)
112+
app.close()
113+
})
114+
94115
test('should create anonymous span (simple case)', async t => {
95116
const app = Fastify()
96117
const plugin = instrumentation.plugin()

0 commit comments

Comments
 (0)