@@ -12,7 +12,7 @@ import { queryPath } from './query-path.js'
12
12
import type { QueryFunc } from './types.js'
13
13
import type { QueryEvent } from '../index.js'
14
14
import type { RoutingTable } from '../routing-table/index.js'
15
- import type { ComponentLogger , Metric , Metrics , PeerId , RoutingOptions , Startable } from '@libp2p/interface'
15
+ import type { ComponentLogger , Counter , Metric , Metrics , PeerId , RoutingOptions , Startable } from '@libp2p/interface'
16
16
import type { ConnectionManager } from '@libp2p/interface-internal'
17
17
import type { DeferredPromise } from 'p-defer'
18
18
@@ -52,16 +52,16 @@ export class QueryManager implements Startable {
52
52
private readonly alpha : number
53
53
private shutDownController : AbortController
54
54
private running : boolean
55
- private queries : number
56
55
private readonly logger : ComponentLogger
57
56
private readonly peerId : PeerId
58
57
private readonly connectionManager : ConnectionManager
59
58
private readonly routingTable : RoutingTable
60
59
private initialQuerySelfHasRun ?: DeferredPromise < void >
61
60
private readonly logPrefix : string
62
- private readonly metrics ?: {
63
- runningQueries : Metric
64
- queryTime : Metric
61
+ private readonly metrics : {
62
+ queries ?: Counter
63
+ errors ?: Counter
64
+ queryTime ?: Metric
65
65
}
66
66
67
67
constructor ( components : QueryManagerComponents , init : QueryManagerInit ) {
@@ -71,18 +71,16 @@ export class QueryManager implements Startable {
71
71
this . disjointPaths = disjointPaths ?? K
72
72
this . running = false
73
73
this . alpha = alpha ?? ALPHA
74
- this . queries = 0
75
74
this . initialQuerySelfHasRun = init . initialQuerySelfHasRun
76
75
this . routingTable = init . routingTable
77
76
this . logger = components . logger
78
77
this . peerId = components . peerId
79
78
this . connectionManager = components . connectionManager
80
79
81
- if ( components . metrics != null ) {
82
- this . metrics = {
83
- runningQueries : components . metrics . registerMetric ( `${ logPrefix . replaceAll ( ':' , '_' ) } _running_queries` ) ,
84
- queryTime : components . metrics . registerMetric ( `${ logPrefix . replaceAll ( ':' , '_' ) } _query_time_seconds` )
85
- }
80
+ this . metrics = {
81
+ queries : components . metrics ?. registerCounter ( `${ logPrefix . replaceAll ( ':' , '_' ) } _queries_total` ) ,
82
+ errors : components . metrics ?. registerCounter ( `${ logPrefix . replaceAll ( ':' , '_' ) } _query_errors_total` ) ,
83
+ queryTime : components . metrics ?. registerMetric ( `${ logPrefix . replaceAll ( ':' , '_' ) } _query_time_seconds` )
86
84
}
87
85
88
86
// allow us to stop queries on shut down
@@ -121,7 +119,7 @@ export class QueryManager implements Startable {
121
119
throw new Error ( 'QueryManager not started' )
122
120
}
123
121
124
- const stopQueryTimer = this . metrics ? .queryTime . timer ( )
122
+ const stopQueryTimer = this . metrics . queryTime ? .timer ( )
125
123
126
124
if ( options . signal == null ) {
127
125
// don't let queries run forever
@@ -167,8 +165,7 @@ export class QueryManager implements Startable {
167
165
}
168
166
169
167
log ( 'query:start' )
170
- this . queries ++
171
- this . metrics ?. runningQueries . update ( this . queries )
168
+ this . metrics ?. queries ?. increment ( )
172
169
173
170
const id = await convertBuffer ( key )
174
171
const peers = this . routingTable . closestPeers ( id )
@@ -223,6 +220,10 @@ export class QueryManager implements Startable {
223
220
224
221
queryFinished = true
225
222
} catch ( err : any ) {
223
+ if ( ! queryFinished ) {
224
+ this . metrics ?. errors ?. increment ( )
225
+ }
226
+
226
227
if ( ! this . running && err . name === 'QueryAbortedError' ) {
227
228
// ignore query aborted errors that were thrown during query manager shutdown
228
229
} else {
@@ -236,13 +237,7 @@ export class QueryManager implements Startable {
236
237
237
238
signal . clear ( )
238
239
239
- this . queries --
240
- this . metrics ?. runningQueries . update ( this . queries )
241
-
242
- if ( stopQueryTimer != null ) {
243
- stopQueryTimer ( )
244
- }
245
-
240
+ stopQueryTimer ?.( )
246
241
log ( 'query:done in %dms' , Date . now ( ) - startTime )
247
242
}
248
243
}
0 commit comments