@@ -13,7 +13,8 @@ export interface QuicCreateListenerOptions extends CreateListenerOptions {
1313}
1414
1515export interface QuicListenerMetrics {
16- events : CounterGroup
16+ events ?: CounterGroup
17+ errors ?: CounterGroup
1718}
1819
1920interface QuicListenerInit {
@@ -40,8 +41,9 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
4041 readonly init : QuicListenerInit
4142 readonly options : QuicCreateListenerOptions
4243 readonly log : Logger
43- readonly metrics ? : QuicListenerMetrics
44+ readonly metrics : QuicListenerMetrics
4445 private readonly shutdownController : AbortController
46+ private addr : string
4547
4648 state : QuicListenerState = { status : 'ready' }
4749
@@ -51,17 +53,36 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
5153 this . init = init
5254 this . options = init . options
5355 this . log = init . logger . forComponent ( 'libp2p:quic:listener' )
56+ this . addr = 'unknown'
5457
5558 this . shutdownController = new AbortController ( )
5659 setMaxListeners ( Infinity , this . shutdownController . signal )
5760
58- if ( init . metrics != null ) {
59- this . metrics = {
60- events : init . metrics . registerMetricGroup ( 'libp2p_quic_listener_events_total' , {
61- label : 'address' ,
62- help : 'Total count of QUIC listener events by type'
63- } )
61+ init . metrics ?. registerMetricGroup ( 'libp2p_quic_inbound_connections_total' , {
62+ label : 'address' ,
63+ help : 'Current active connections in QUIC listener' ,
64+ calculate : ( ) => {
65+ if ( this . state . status !== 'listening' ) {
66+ return {
67+ [ this . addr ] : 0
68+ }
69+ }
70+
71+ return {
72+ [ this . addr ] : this . state . connections . size
73+ }
6474 }
75+ } )
76+
77+ this . metrics = {
78+ events : init . metrics ?. registerMetricGroup ( 'libp2p_quic_listener_events_total' , {
79+ label : 'address' ,
80+ help : 'Total count of QUIC listener events by type'
81+ } ) ,
82+ errors : init . metrics ?. registerMetricGroup ( 'libp2p_quic_listener_errors_total' , {
83+ label : 'address' ,
84+ help : 'Total count of QUIC listener errors by type'
85+ } )
6586 }
6687
6788 this . log ( 'new' )
@@ -91,6 +112,7 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
91112 const addr = ma . nodeAddress ( )
92113 const controller = new AbortController ( )
93114 const listener = new napi . Server ( this . #config, addr . address , addr . port )
115+ this . addr = `${ addr . address } :${ addr . port === 0 ? listener . port ( ) : addr . port } `
94116
95117 // replace wildcard port with actual listening port
96118 if ( addr . port === 0 ) {
@@ -143,15 +165,18 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
143165 try {
144166 const listenerPromise = this . state . listener . inboundConnection ( )
145167 listenerPromise
146- . then ( ( ) => this . metrics ?. events . increment ( { connect : true } ) )
147- . catch ( ( ) => this . metrics ?. events . increment ( { error : true } ) )
168+ . then ( ( ) => this . metrics . events ?. increment ( { [ `${ this . addr } connect` ] : true } ) )
169+ . catch ( ( err ) => {
170+ this . log . error ( '%a error awaiting inbound connection - %e' , listenAddr , err )
171+ this . metrics . events ?. increment ( { [ `${ this . addr } error` ] : true } )
172+ } )
148173
149174 const connection = await raceSignal ( listenerPromise , signal )
150175 this . onInboundConnection ( connection ) . catch ( ( e ) => {
151- this . log . error ( '%s error handling inbound connection' , listenAddr . toString ( ) , e )
176+ this . log . error ( '%a error handling inbound connection - %e ' , listenAddr , e )
152177 } )
153178 } catch ( e ) {
154- this . log . error ( '%s error accepting connection' , listenAddr . toString ( ) , e )
179+ this . log . error ( '%a error accepting connection - %e ' , listenAddr , e )
155180
156181 if ( signal . aborted ) {
157182 break
@@ -170,12 +195,19 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
170195 return
171196 }
172197
173- const maConn = new QuicConnection ( {
174- connection,
175- logger : this . init . logger ,
176- direction : 'inbound' ,
177- metrics : this . metrics ?. events
178- } )
198+ let maConn : QuicConnection
199+ try {
200+ maConn = new QuicConnection ( {
201+ connection,
202+ logger : this . init . logger ,
203+ direction : 'inbound' ,
204+ metrics : this . metrics ?. events ,
205+ metricsPrefix : `${ this . addr } `
206+ } )
207+ } catch ( err ) {
208+ this . metrics . errors ?. increment ( { [ `${ this . addr } inbound_to_connection` ] : true } )
209+ throw err
210+ }
179211
180212 try {
181213 await this . options . upgrader . upgradeInbound ( maConn , {
@@ -196,6 +228,7 @@ export class QuicListener extends TypedEventEmitter<ListenerEvents> implements L
196228 } , { once : true } )
197229 } catch ( err ) {
198230 this . log . error ( '%s error handling inbound connection' , this . state . listenAddr . toString ( ) , err )
231+ this . metrics . errors ?. increment ( { [ `${ this . addr } inbound_upgrade` ] : true } )
199232 maConn . abort ( err as Error )
200233 }
201234 }
0 commit comments