-
Notifications
You must be signed in to change notification settings - Fork 2
diag(ai-gateway): measure whether pool slots are leaked #5107
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
RSO
wants to merge
1
commit into
main
Choose a base branch
from
diag/usage-record-pool-leak-probe
base: main
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
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
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,102 @@ | ||
| // Counters attach to the real pool only outside the test environment, so the pool | ||
| // is stubbed here and the gauges are driven directly. The stub is built inside the | ||
| // factory because `jest.mock` is hoisted above this file's own declarations, so | ||
| // referencing an outer const here throws "Cannot access before initialization". | ||
| jest.mock('@/lib/drizzle', () => ({ | ||
| pool: { totalCount: 0, idleCount: 0, waitingCount: 0, on: jest.fn() }, | ||
| })); | ||
|
|
||
| import { beforeEach, describe, expect, test } from '@jest/globals'; | ||
|
|
||
| import { | ||
| isTransactionBeginFailure, | ||
| readPoolLeakStats, | ||
| recordTransactionBeginFailure, | ||
| __resetPoolLeakCountersForTest, | ||
| } from './db-pool-leak-probe'; | ||
|
|
||
| const { pool: mockPool } = jest.requireMock<{ | ||
| pool: { totalCount: number; idleCount: number }; | ||
| }>('@/lib/drizzle'); | ||
|
|
||
| function drizzleError(message: string, cause?: unknown) { | ||
| const error = new Error(message); | ||
| error.name = 'DrizzleQueryError'; | ||
| if (cause !== undefined) (error as unknown as { cause: unknown }).cause = cause; | ||
| return error; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| __resetPoolLeakCountersForTest(); | ||
| mockPool.totalCount = 0; | ||
| mockPool.idleCount = 0; | ||
| }); | ||
|
|
||
| describe('isTransactionBeginFailure', () => { | ||
| // BEGIN takes no parameters, which is what makes matching the message safe here | ||
| // and unsafe for every other statement in this path. | ||
| test.each([ | ||
| 'Failed query: begin', | ||
| 'Failed query: begin\nparams: ', | ||
| 'Failed query: BEGIN', | ||
| ' Failed query: begin isolation level serializable', | ||
| ])('recognises %j', message => { | ||
| expect(isTransactionBeginFailure(drizzleError(message))).toBe(true); | ||
| }); | ||
|
|
||
| test('recognises a begin failure nested in the cause chain', () => { | ||
| expect( | ||
| isTransactionBeginFailure(drizzleError('wrapper', drizzleError('Failed query: begin'))) | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| // These must not be counted as leaks: the statement failed inside the | ||
| // try/finally, so drizzle released the client. | ||
| test.each([ | ||
| 'Failed query: WITH microdollar_usage_ins AS (...) params: 3f5826e7,You are Kilo', | ||
| 'Failed query: commit', | ||
| 'Failed query: rollback', | ||
| 'Failed query: select 1', | ||
| 'Connection terminated unexpectedly', | ||
| ])('does not match %j', message => { | ||
| expect(isTransactionBeginFailure(drizzleError(message))).toBe(false); | ||
| }); | ||
|
|
||
| test('does not match a statement that merely mentions begin', () => { | ||
| expect(isTransactionBeginFailure(drizzleError('Failed query: select * from beginnings'))).toBe( | ||
| false | ||
| ); | ||
| }); | ||
|
|
||
| test('does not throw on null or a primitive', () => { | ||
| expect(isTransactionBeginFailure(null)).toBe(false); | ||
| expect(isTransactionBeginFailure('nope')).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('readPoolLeakStats', () => { | ||
| test('derives checked_out from the pool gauges', () => { | ||
| mockPool.totalCount = 10; | ||
| mockPool.idleCount = 3; | ||
| expect(readPoolLeakStats().checked_out).toBe(7); | ||
| }); | ||
|
|
||
| test('reports min_checked_out as null until a pool event is seen', () => { | ||
| expect(readPoolLeakStats().min_checked_out).toBeNull(); | ||
| }); | ||
|
|
||
| test('counts begin failures, the per-leak unit', () => { | ||
| recordTransactionBeginFailure(); | ||
| recordTransactionBeginFailure(); | ||
| expect(readPoolLeakStats().begin_failures).toBe(2); | ||
| }); | ||
|
|
||
| test('reports outstanding as acquires minus releases', () => { | ||
| const stats = readPoolLeakStats(); | ||
| expect(stats.outstanding).toBe(stats.acquires - stats.releases); | ||
| }); | ||
|
|
||
| test('exposes uptime so a low-water mark can be read against instance age', () => { | ||
| expect(readPoolLeakStats().instance_uptime_ms).toBeGreaterThanOrEqual(0); | ||
| }); | ||
| }); |
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.
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.
SUGGESTION: A 400 response consumes the quiet-instance observation point without emitting it
noteRequestStart()runs for every authorized request, but a request that fails schema validation returns 400 beforereportTimingis ever called. If the first request after a >=5s quiet period is a 400, it resetslastRequestAtMs, so the next (fast) request no longer meetsQUIET_INSTANCE_MSand the one moment a leaked slot is distinguishable from a busy one is lost. Rare on this internal endpoint, but if these observation points matter, consider updating the clock only on requests that can actually emit (e.g. moving the call after validation, or resetting it on the 400 path).Reply with
@kilocode-bot fix itto have Kilo Code address this issue.