http,inspector: add WebSocket upgrade observability#62933
Open
GrinZero wants to merge 1 commit intonodejs:mainfrom
Open
http,inspector: add WebSocket upgrade observability#62933GrinZero wants to merge 1 commit intonodejs:mainfrom
GrinZero wants to merge 1 commit intonodejs:mainfrom
Conversation
Collaborator
|
Review requested:
|
Author
const Koa = require('koa')
const Router = require('koa-router')
const WebSocket = require('ws')
const app = new Koa()
const router = new Router()
let ws
let opened
function ensureWebSocket() {
if (ws && ws.readyState === WebSocket.OPEN) return
Promise.resolve()
ws = new WebSocket('wss://echo.websocket.org/')
opened = new Promise((resolve) => ws.once('open', resolve))
ws.on('message', (data) => {
console.log('WebSocket message:', data.toString())
})
ws.on('close', () => {
console.log('WebSocket connection closed')
ws = null
opened = null
})
return opened
}
router.get('/ws', async (ctx) => {
await ensureWebSocket()
ws.send(ctx.query.message || 'Hello from Koa')
ctx.body = 'WebSocket: Send'
})
app.use(router.routes())
app.listen(3000, () => {
console.log('GET http://127.0.0.1:3000/ws?message=ping')
}) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Summary
This patch adds observability for HTTP client WebSocket upgrades in both
diagnostics_channeland the inspectorNetworkdomain.Today, regular HTTP client requests are surfaced through the existing
http.client.*flow, but upgrade-based WebSocket traffic is not representedcleanly: the HTTP upgrade handshake is not exposed as a first-class lifecycle,
and subsequent WebSocket frames are not emitted through inspector network
events.
This change fills that gap.
Changes
Network.webSocket*events for socket creation, handshakerequest/response, sent frames, received frames, and socket close
HTTP request/response traffic
Why This Approach
This is implemented at the HTTP upgrade / inspector plumbing layer rather than
in a specific WebSocket client surface.
That keeps the change aligned with where the underlying lifecycle actually
occurs: the initial HTTP upgrade, the handshake response, and the frame
traffic that follows on the upgraded socket. It also avoids coupling the
observability model to any single user-facing API and makes the behavior
reusable for clients built on the same upgrade path.
Scope
This PR does not add special handling or declarations for built-in
WebSocketorWebSocketStreamdirectly.Those APIs are a separate surface-area discussion. The goal here is narrower:
add the transport-level observability needed for WebSocket upgrades and frame
traffic first, so higher-level clients can benefit from the same underlying
inspector and diagnostics hooks without expanding this PR beyond that scope.
Testing
Automated:
python3 tools/test.py test/parallel/test-diagnostics-channel-http-server-upgrade-websocket.js test/parallel/test-diagnostics-channel-http-upgrade-websocket.js test/parallel/test-inspector-emit-protocol-event-errors.js test/parallel/test-inspector-emit-protocol-event.js test/parallel/test-inspector-network-http-upgrade-websocket.jsManual e2e:
/wsroute/wsopens an outboundwsconnection towss://echo.websocket.org//ws?message=...send/receive, and close lifecycle end to end
Refs: #53946