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
Conversation
I do not think it is the right approach, you are passing the fastify |
fastify.js
Outdated
|
||
try { | ||
const dc = require('diagnostics_channel') | ||
dc.channel('fastify:loaded').publish(fastify) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since https://github.com/fastify/fastify-diagnostics-channel already exists, we should at least use the same syntax, ie: fastify.loaded
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I'm fine with using a .
here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if we checked with hasSubscribers
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need!
I think we might aim to integrate fastify-diagnostic-channel in fastify proper for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm -1 on this as diagnostics_channel
are only available in node 15 and 16 this would slow down the startup time of any instance. In the case of serverless this is quite a change. Is there a reason why not to use https://github.com/fastify/fastify-diagnostics-channel ? This plugin might just need an addition for the onReady
hook : https://www.fastify.io/docs/latest/Hooks/#onready
This would not fit the use case as |
Fair point. One solution would be to use semver check on After checking the https://nodejs.org/en/about/releases/ schedule, 16 will be active in few months. Landing this in semver minor and integrating https://github.com/fastify/fastify-diagnostics-channel in v4 is a good plan to me. |
For reference, this is as much discussion as we have had about the specific API -- #2697 I am of two opinions:
I can be convinced of opinion 1. We have a non-trivial amount of clout/interaction with Node core. So even if the API changes, we'd at least have a heads up. However, I suspect the registration API is unlikely to change, and this PR only makes use of that part of the API. If the, I am certain, negligible start up impact is really such a concern, we can do feature detection in a different manner. @bengl has already stated he is up for such a change. Frankly, it can be as simple as: const nodeMajor = /v(\d+)\..+/.exec(process.version)[1]
if (parseInt(nodeMajor, 10) >= 15) {
// do it
} Or if the regex is still too expensive: const nodeMajor = parseInt(process.version.slice(1).split('.').shift(), 10)
if (nodeMajor >= 15) {
// do it
} |
@climba03003 Yes, I think you're right and this should be done perhaps at the end of the constructor. I'll make that change.
The thing is, fastify already has a pretty good system for hooks at lifecycle events, and adding a @jsumners @zekth You both mentioned lack of support in earlier Node.js versions. Note that a polyfill exists, and we intend to include it as a dependency of our APM library. For that reason, I'd prefer not doing a Node.js version check. |
To be clear, I am not concerned about the start up time impact. |
I understand this change walk to a "near zero configuration" installation for users, but I would like to propose something like: const dc = require('diagnostics_channel')
const fastify = require('fastify')({
logger: true,
tracing: {
loaded: dc.channel('whatever')
}
}) The in fastify core we could just write
Is this overengenieering? |
I think that it hit the same problem for APM vendors that require a user action to run the tracing, if we follow this line the vendors could create a plugin for fastify that register I'm +1 in adding And I also would like to see a usage example as @climba03003 mentioned. |
@zekth could you measure this? |
I'm generically +1 with this approach, anything that makes the APM vendors do less monkeypatching is a good a thing for me. |
I can do something. But as i stated in my comment (not clear i confess): #3279 (comment) I'm +1 on this. |
@mcollina Yes, hopefully I will have some time today. 😅 |
I understand. I'm not blocking this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a note in our docs with this new support out-of-box?
Updated the PR taking into account most of the recommendations, and added a quick bit of documentation with a contrived example/use-case. Happy to change the feature detection (or whatever else) if that's necessary to land, but as stated before, I'd like to steer clear from using Node.js versions, to make sure the polyfill can be used. Other than that, this should be ready to land, unless there are further concerns. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add suggestions for consistency
docs/Hooks.md
Outdated
|
||
Currently, one | ||
[`diagnostics_channel`](https://nodejs.org/api/diagnostics_channel.html) publish | ||
event, `'fastify:initialized'`, happens at initialization time. The Fastify |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
event, `'fastify:initialized'`, happens at initialization time. The Fastify | |
event, `'fastify.initialized'`, happens at initialization time. The Fastify |
docs/Hooks.md
Outdated
```js | ||
const tracer = /* retrieved from elsehwere in the package */ | ||
const dc = require('diagnostics_channel') | ||
const channel = dc.channel('fastify:initialized') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const channel = dc.channel('fastify:initialized') | |
const channel = dc.channel('fastify.initialized') |
Added the changes requested by @mcollina and @Eomm. Are folks happy with the name of the channel? Currently it's
Keep in mind that there may be additional channels in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Given that this registration happens at the onset of initialization, I think it better to name it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Ci is failing due to the coverage regression
We could add a test with proxyquire
to simulate the diagnostic_channel
a reach the 100%
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor typo in here. otherwise looks good!
docs/Hooks.md
Outdated
channel.subscribe(function ({ fastify }) { | ||
fastify.addHook('onRequest', (request, reply, done) => { | ||
const span = tracer.startSpan('fastify.request') | ||
spans.set(reqest, span) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
spans.set(reqest, span) | |
spans.set(request, span) |
Co-authored-by: James Sumners <james@sumners.email>
Co-authored-by: James Sumners <james@sumners.email>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Adds a
diagnostics_channel
publish atloadinitialization time. This is sufficient for APM purposes, since access to thefastify
object is all we need in order to add hooks withaddHook
. This change enables us to instrument fastify without any of the following:Since
diagnostics_channel
is not always available (e.g. on older versions of Node.js), this is wrapped in a try-catch, but I'm happy to switch to another method of feature detection (or pulling in the polyfill) if desired.Checklist
npm run test
andnpm run benchmark
and/or benchmarksare includedand the Code of conduct