@@ -10,6 +10,10 @@ class TestStoragePgDB extends StoragePgDB {
1010 return this . runUnscopedQuery ( 'MetricWithoutTenantAttribute' , async ( ) => 'ok' )
1111 }
1212
13+ runScopedMetricProbe ( ) : Promise < string > {
14+ return this . runQuery ( 'ScopedMetricDuration' , async ( ) => 'ok' )
15+ }
16+
1317 runErrorMappingProbe ( ) : Promise < unknown > {
1418 return this . runQuery ( 'FindBucketById' , ( db ) => {
1519 return this . query ( db , 'SELECT * FROM storage.buckets WHERE id = $1' )
@@ -47,18 +51,59 @@ describe('StoragePgDB metrics', () => {
4751 host : 'localhost' ,
4852 } )
4953 const recordSpy = vi . spyOn ( dbQueryPerformance , 'record' )
54+ const performanceNowSpy = vi
55+ . spyOn ( performance , 'now' )
56+ . mockReturnValueOnce ( 10 )
57+ . mockReturnValueOnce ( 15 )
5058
5159 try {
5260 await expect ( storage . runMetricProbe ( ) ) . resolves . toBe ( 'ok' )
5361
54- expect ( recordSpy ) . toHaveBeenCalledWith ( expect . any ( Number ) , {
62+ expect ( recordSpy ) . toHaveBeenCalledWith ( 0.005 , {
5563 name : 'MetricWithoutTenantAttribute' ,
5664 requestAborted : false ,
5765 requestAbortedBeforeStart : false ,
5866 requestAbortedAfterStart : false ,
5967 } )
6068 expect ( recordSpy . mock . calls [ 0 ] ?. [ 1 ] ) . not . toHaveProperty ( 'tenantId' )
6169 } finally {
70+ performanceNowSpy . mockRestore ( )
71+ recordSpy . mockRestore ( )
72+ }
73+ } )
74+
75+ test ( 'records scoped DB query duration from numeric monotonic timestamps' , async ( ) => {
76+ const transaction = {
77+ commit : vi . fn ( ) ,
78+ rollback : vi . fn ( ) ,
79+ isCompleted : vi . fn ( ) . mockReturnValue ( false ) ,
80+ }
81+ const connection = {
82+ getAbortSignal : vi . fn ( ) . mockReturnValue ( undefined ) ,
83+ transaction : vi . fn ( ) . mockResolvedValue ( transaction ) ,
84+ setScope : vi . fn ( ) ,
85+ } as unknown as PgTenantConnection
86+ const storage = new TestStoragePgDB ( connection , {
87+ tenantId : 'metric-cardinality-tenant' ,
88+ host : 'localhost' ,
89+ } )
90+ const recordSpy = vi . spyOn ( dbQueryPerformance , 'record' )
91+ const performanceNowSpy = vi
92+ . spyOn ( performance , 'now' )
93+ . mockReturnValueOnce ( 2 )
94+ . mockReturnValueOnce ( 9 )
95+
96+ try {
97+ await expect ( storage . runScopedMetricProbe ( ) ) . resolves . toBe ( 'ok' )
98+
99+ expect ( recordSpy ) . toHaveBeenCalledWith ( 0.007 , {
100+ name : 'ScopedMetricDuration' ,
101+ requestAborted : false ,
102+ requestAbortedBeforeStart : false ,
103+ requestAbortedAfterStart : false ,
104+ } )
105+ } finally {
106+ performanceNowSpy . mockRestore ( )
62107 recordSpy . mockRestore ( )
63108 }
64109 } )
0 commit comments