-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add diagnostic channels #4429
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
Open
YoannMa
wants to merge
10
commits into
hapijs:master
Choose a base branch
from
YoannMa:feat/diagnostic_channels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add diagnostic channels #4429
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6680441
feat: add diagnostic channel for server, route, request creation and …
YoannMa 2606bb5
style: Apply suggestions from code review
YoannMa 890062d
style: remove empty line
YoannMa e046e26
doc: update documentation to add exemple and description to diagnosti…
YoannMa cf9c82d
fix: apply change asked in review
YoannMa 5eb9907
fix: move channels event before user-facing hooks
YoannMa 35ea6ef
test: add test insuring request is not routed on hapi.onRequest
YoannMa f21651d
feat: add hapi.onRequestLifecycle and hapi.onError diagnostic channels
YoannMa a246101
doc: change hapi.onError channel description
YoannMa 34f3f35
style: missing semi
YoannMa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const DC = require('diagnostics_channel'); | ||
|
||
exports.server = DC.channel('hapi.onServer'); | ||
|
||
exports.route = DC.channel('hapi.onRoute'); | ||
|
||
exports.response = DC.channel('hapi.onResponse'); | ||
|
||
exports.request = DC.channel('hapi.onRequest'); | ||
|
||
exports.requestLifecycle = DC.channel('hapi.onRequestLifecycle'); | ||
|
||
exports.error = DC.channel('hapi.onError'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
'use strict'; | ||
|
||
const DC = require('diagnostics_channel'); | ||
|
||
const Code = require('@hapi/code'); | ||
const Lab = require('@hapi/lab'); | ||
const Hoek = require('@hapi/hoek'); | ||
|
||
const Hapi = require('..'); | ||
|
||
const { describe, it } = exports.lab = Lab.script(); | ||
const expect = Code.expect; | ||
|
||
describe('DiagnosticChannel', () => { | ||
|
||
describe('hapi.onServer', () => { | ||
|
||
const channel = DC.channel('hapi.onServer'); | ||
|
||
it('server should be exposed on creation through the channel hapi.onServer', async () => { | ||
|
||
let server; | ||
|
||
const exposedServer = await new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
|
||
server = Hapi.server(); | ||
}); | ||
|
||
expect(exposedServer).to.shallow.equal(server); | ||
}); | ||
}); | ||
|
||
describe('hapi.onRoute', () => { | ||
|
||
const channel = DC.channel('hapi.onRoute'); | ||
|
||
it('route should be exposed on creation through the channel hapi.onRoute', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
YoannMa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const exposedRoute = await new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
options: { app: { x: 'o' } }, | ||
handler: () => 'ok' | ||
}); | ||
}); | ||
|
||
expect(exposedRoute).to.be.an.object(); | ||
expect(exposedRoute.settings.app.x).to.equal('o'); | ||
}); | ||
}); | ||
|
||
describe('hapi.onResponse', () => { | ||
|
||
const channel = DC.channel('hapi.onResponse'); | ||
|
||
it('response should be exposed on creation through the channel hapi.onResponse', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
server.route({ method: 'GET', path: '/', handler: () => 'ok' }); | ||
|
||
const event = new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
}); | ||
|
||
const response = await server.inject('/'); | ||
const responseExposed = await event; | ||
expect(response.request.response).to.shallow.equal(responseExposed); | ||
}); | ||
}); | ||
|
||
describe('hapi.onRequest', () => { | ||
|
||
const channel = DC.channel('hapi.onRequest'); | ||
|
||
it('request should be exposed on creation through the channel hapi.onRequest', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
server.route({ method: 'GET', path: '/', handler: () => 'ok' }); | ||
|
||
const event = new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
}); | ||
|
||
const response = await server.inject('/'); | ||
const requestExposed = await event; | ||
expect(response.request).to.shallow.equal(requestExposed); | ||
}); | ||
|
||
it('request should not have been routed when hapi.onRequest is triggered', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
server.route({ method: 'GET', path: '/test/{p}', handler: () => 'ok' }); | ||
|
||
server.ext('onRequest', async (request, h) => { | ||
|
||
await Hoek.wait(10); | ||
return h.continue; | ||
}); | ||
|
||
const event = new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
}); | ||
|
||
const request = server.inject('/test/foo'); | ||
const requestExposed = await event; | ||
expect(requestExposed.params).to.be.null(); | ||
|
||
const response = await request; | ||
expect(response.request).to.shallow.equal(requestExposed); | ||
}); | ||
}); | ||
|
||
describe('hapi.onRequestLifecycle', () => { | ||
|
||
const channel = DC.channel('hapi.onRequestLifecycle'); | ||
|
||
it('request should be exposed after routing through the channel hapi.onRequestLifecycle', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
server.route({ | ||
method: 'POST', | ||
path: '/test/{p}', | ||
options: { app: { x: 'o' } }, | ||
handler: () => 'ok' | ||
}); | ||
|
||
server.ext('onPreAuth', async (request, h) => { | ||
|
||
await Hoek.wait(10); | ||
return h.continue; | ||
}); | ||
|
||
const event = new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
}); | ||
|
||
const request = server.inject({ method: 'POST', url: '/test/foo', payload: '{"a":"b"}' }); | ||
const requestExposed = await event; | ||
expect(requestExposed.params).to.be.an.object(); | ||
expect(requestExposed.params.p).to.equal('foo'); | ||
expect(requestExposed.route).to.be.an.object(); | ||
expect(requestExposed.route.settings.app.x).to.equal('o'); | ||
expect(requestExposed.payload).to.be.undefined(); | ||
|
||
const response = await request; | ||
expect(response.request).to.shallow.equal(requestExposed); | ||
expect(response.request.payload).to.be.an.object(); | ||
expect(response.request.payload.a).to.equal('b'); | ||
}); | ||
}); | ||
|
||
describe('hapi.onError', () => { | ||
|
||
const channel = DC.channel('hapi.onError'); | ||
|
||
it('should expose the request as well as the error object when an error happens during the request lifetime', async () => { | ||
|
||
const server = Hapi.server(); | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler: () => { | ||
|
||
throw new Error('some error message'); | ||
} | ||
}); | ||
|
||
const event = new Promise((resolve) => { | ||
|
||
channel.subscribe(resolve); | ||
}); | ||
|
||
const response = await server.inject('/'); | ||
const { request: requestExposed, error: errorExposed } = await event; | ||
expect(response.request).to.shallow.equal(requestExposed); | ||
expect(errorExposed).to.be.an.instanceof(Error); | ||
expect(errorExposed.message).to.equal('some error message'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.