Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add diagnostics_channel publish at initialization time #3279

Merged
merged 6 commits into from Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/Hooks.md
Expand Up @@ -564,3 +564,38 @@ fastify.route({
```

**Note**: both options also accept an array of functions.

## Diagnostics Channel Hooks
mcollina marked this conversation as resolved.
Show resolved Hide resolved

Currently, one
[`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html) publish
event, `'fastify:initialized'`, happens at initialization time. The Fastify
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
event, `'fastify:initialized'`, happens at initialization time. The Fastify
event, `'fastify.initialized'`, happens at initialization time. The Fastify

instance is passed into the hook as a property of the object passed in. At this
point, the instance can be interacted with to add hooks, plugins, routes or any
other sort of modification.

For example, a tracing package might do something like the following (which is,
of course, a simplification). This would be in a file loaded in the
initialization of the tracking package, in the typical "require instrumentation
tools first" fashion.

```js
const tracer = /* retrieved from elsehwere in the package */
const dc = require('diagnostics_channel')
const channel = dc.channel('fastify:initialized')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const channel = dc.channel('fastify:initialized')
const channel = dc.channel('fastify.initialized')

const spans = new WeakMap()

channel.subscribe(function ({ fastify }) {
fastify.addHook('onRequest', (request, reply, done) => {
const span = tracer.startSpan('fastify.request')
spans.set(reqest, span)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
spans.set(reqest, span)
spans.set(request, span)

done()
})

fastify.addHook('onResponse', (request, reply, done) => {
const span = spans.get(request)
span.finish()
done()
})
})
Eomm marked this conversation as resolved.
Show resolved Hide resolved
```
11 changes: 11 additions & 0 deletions fastify.js
Expand Up @@ -399,6 +399,17 @@ function fastify (options) {
// Delay configuring clientError handler so that it can access fastify state.
server.on('clientError', options.clientErrorHandler.bind(fastify))

try {
const dc = require('diagnostics_channel')
const initChannel = dc.channel('fastify:initialized')
Eomm marked this conversation as resolved.
Show resolved Hide resolved
if (initChannel.hasSubscribers) {
initChannel.publish({ fastify })
}
} catch {
// This only happens if `diagnostics_channel` isn't available, i.e. earlier
// versions of Node.js. In that event, we don't care, so ignore the error.
}

return fastify

function throwIfAlreadyStarted (msg) {
Expand Down
29 changes: 29 additions & 0 deletions test/diagnostics-channel.test.js
@@ -0,0 +1,29 @@
'use strict'

const t = require('tap')
const test = t.test
let dc
try {
dc = require('diagnostics_channel')
} catch {
// Not available in this version of Node.js
mcollina marked this conversation as resolved.
Show resolved Hide resolved
}

test('diagnostics_channel', t => {
if (!dc) {
t.plan(1)
t.skip('diagnostics_channel is not available')
return
}
t.plan(2)

let fastifyInHook

const channel = dc.channel('fastify:initialized')
Eomm marked this conversation as resolved.
Show resolved Hide resolved
channel.subscribe(function ({ fastify }) {
t.pass('message passed to channel')
fastifyInHook = fastify
})
const fastify = require('../fastify')()
t.equal(fastifyInHook, fastify)
})