-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add diagnostics_channel TracingChannel support #3624
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
logaretm
wants to merge
7
commits into
brianc:master
Choose a base branch
from
logaretm:worktree-proud-squishing-wreath
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b7f80ac
Add diagnostics_channel TracingChannel support to pg and pg-pool
logaretm 91ace72
Skip TracingChannel tests on Node < 19.9
logaretm 5b50561
fix: format
logaretm 73c9946
fix: use shouldTrace guard for Node 18 compatibility
logaretm c31eab5
Skip TracingChannel tests on Node < 19.9
logaretm 3957e3c
fix: format
logaretm b922f0d
fix: use traceCallback for connect promise path to preserve custom Pr…
logaretm 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| 'use strict' | ||
|
|
||
| const noopChannel = { hasSubscribers: false } | ||
|
|
||
| let poolConnectChannel = noopChannel | ||
| let poolReleaseChannel = noopChannel | ||
| let poolRemoveChannel = noopChannel | ||
|
|
||
| try { | ||
| let dc | ||
| if (typeof process.getBuiltInModule === 'function') { | ||
| dc = process.getBuiltInModule('diagnostics_channel') | ||
| } else { | ||
| dc = require('diagnostics_channel') | ||
| } | ||
| if (typeof dc.tracingChannel === 'function') { | ||
| poolConnectChannel = dc.tracingChannel('pg:pool:connect') | ||
| } | ||
| if (typeof dc.channel === 'function') { | ||
| poolReleaseChannel = dc.channel('pg:pool:release') | ||
| poolRemoveChannel = dc.channel('pg:pool:remove') | ||
| } | ||
| } catch (e) { | ||
| // diagnostics_channel not available (non-Node environment) | ||
| } | ||
|
|
||
| // Check explicitly for `false` rather than truthiness because the aggregated | ||
| // `hasSubscribers` getter on TracingChannel is `undefined` on Node 18 (which | ||
| // backported TracingChannel but not the getter). When `undefined`, we assume | ||
| // there may be subscribers and trace unconditionally. | ||
| function shouldTrace(channel) { | ||
| return channel.hasSubscribers !== false | ||
| } | ||
|
|
||
| module.exports = { poolConnectChannel, poolReleaseChannel, poolRemoveChannel, shouldTrace } | ||
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 expect = require('expect.js') | ||
| const EventEmitter = require('events').EventEmitter | ||
| const describe = require('mocha').describe | ||
| const it = require('mocha').it | ||
| const dc = require('diagnostics_channel') | ||
| const Pool = require('../') | ||
|
|
||
| // TracingChannel exists on Node 18+ but the aggregated hasSubscribers getter | ||
| // and stable unsubscribe behavior require Node 19.9+/20.5+. Skip tracing | ||
| // tests on older versions where TracingChannel is missing or has internal bugs. | ||
| const hasStableTracingChannel = | ||
| typeof dc.tracingChannel === 'function' && typeof dc.tracingChannel('pg:pool:test:probe').hasSubscribers === 'boolean' | ||
|
|
||
| function mockClient(methods) { | ||
| return function () { | ||
| const client = new EventEmitter() | ||
| client.end = function (cb) { | ||
| if (cb) process.nextTick(cb) | ||
| } | ||
| client._queryable = true | ||
| client._ending = false | ||
| client.processID = 12345 | ||
| Object.assign(client, methods) | ||
| return client | ||
| } | ||
| } | ||
|
|
||
| describe('diagnostics channels', function () { | ||
| describe('pg:pool:connect', function () { | ||
| ;(hasStableTracingChannel ? it : it.skip)('publishes start event when connect is called', function (done) { | ||
| const pool = new Pool({ | ||
| Client: mockClient({ | ||
| connect: function (cb) { | ||
| process.nextTick(() => cb(null)) | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
logaretm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let capturedContext | ||
| const channel = dc.tracingChannel('pg:pool:connect') | ||
| const subs = { | ||
| start: (ctx) => { | ||
| capturedContext = ctx | ||
| }, | ||
| end: () => {}, | ||
| asyncStart: () => {}, | ||
| asyncEnd: () => {}, | ||
| error: () => {}, | ||
| } | ||
|
|
||
| channel.subscribe(subs) | ||
|
|
||
| pool.connect(function (err, client, release) { | ||
| if (err) return done(err) | ||
| release() | ||
| pool.end(() => { | ||
| expect(capturedContext).to.be.ok() | ||
| expect(capturedContext.pool).to.be.ok() | ||
| expect(capturedContext.pool.maxSize).to.be(10) | ||
| expect(capturedContext.pool.totalCount).to.be.a('number') | ||
|
|
||
| channel.unsubscribe(subs) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| ;(hasStableTracingChannel ? it : it.skip)('enriches context with client info on asyncEnd', function (done) { | ||
| const pool = new Pool({ | ||
| Client: mockClient({ | ||
| connect: function (cb) { | ||
| process.nextTick(() => cb(null)) | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
| const channel = dc.tracingChannel('pg:pool:connect') | ||
| const subs = { | ||
| start: () => {}, | ||
| end: () => {}, | ||
| asyncStart: () => {}, | ||
| asyncEnd: (ctx) => { | ||
| expect(ctx.client).to.be.ok() | ||
| expect(ctx.client.processID).to.be(12345) | ||
|
|
||
| channel.unsubscribe(subs) | ||
| done() | ||
| }, | ||
| error: () => {}, | ||
| } | ||
|
|
||
| channel.subscribe(subs) | ||
|
|
||
| pool.connect(function (err, client, release) { | ||
| if (err) return done(err) | ||
| release() | ||
| pool.end() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('pg:pool:release', function () { | ||
| it('publishes when a client is released', function (done) { | ||
| const pool = new Pool({ | ||
| Client: mockClient({ | ||
| connect: function (cb) { | ||
| process.nextTick(() => cb(null)) | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
| let releaseMessage | ||
| const channel = dc.channel('pg:pool:release') | ||
| const onMessage = (msg) => { | ||
| releaseMessage = msg | ||
| } | ||
| channel.subscribe(onMessage) | ||
|
|
||
| pool.connect(function (err, client, release) { | ||
| if (err) return done(err) | ||
| release() | ||
| pool.end(() => { | ||
| expect(releaseMessage).to.be.ok() | ||
| expect(releaseMessage.client).to.be.ok() | ||
| expect(releaseMessage.client.processID).to.be(12345) | ||
|
|
||
| channel.unsubscribe(onMessage) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| it('includes error when released with error', function (done) { | ||
| const pool = new Pool({ | ||
| Client: mockClient({ | ||
| connect: function (cb) { | ||
| process.nextTick(() => cb(null)) | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
| let releaseMessage | ||
| const channel = dc.channel('pg:pool:release') | ||
| const onMessage = (msg) => { | ||
| releaseMessage = msg | ||
| } | ||
| channel.subscribe(onMessage) | ||
|
|
||
| pool.connect(function (err, client, release) { | ||
| if (err) return done(err) | ||
| const releaseError = new Error('test error') | ||
| release(releaseError) | ||
| pool.end(() => { | ||
| expect(releaseMessage).to.be.ok() | ||
| expect(releaseMessage.error).to.be(releaseError) | ||
|
|
||
| channel.unsubscribe(onMessage) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('pg:pool:remove', function () { | ||
| it('publishes when a client is removed', function (done) { | ||
| const pool = new Pool({ | ||
| Client: mockClient({ | ||
| connect: function (cb) { | ||
| process.nextTick(() => cb(null)) | ||
| }, | ||
| }), | ||
| }) | ||
|
|
||
| let removeMessage | ||
| const channel = dc.channel('pg:pool:remove') | ||
| const onMessage = (msg) => { | ||
| removeMessage = msg | ||
| } | ||
| channel.subscribe(onMessage) | ||
|
|
||
| pool.connect(function (err, client, release) { | ||
| if (err) return done(err) | ||
| // release with error to trigger removal | ||
| release(new Error('force remove')) | ||
| pool.end(() => { | ||
| expect(removeMessage).to.be.ok() | ||
| expect(removeMessage.client).to.be.ok() | ||
| expect(removeMessage.client.processID).to.be(12345) | ||
|
|
||
| channel.unsubscribe(onMessage) | ||
| done() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.