Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

Commit 12b7927

Browse files
committed
Declare metrics locally and use MetricsRegister
1 parent fffffe4 commit 12b7927

File tree

4 files changed

+53
-25
lines changed

4 files changed

+53
-25
lines changed

src/index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import { CreateListenerOptions, DialOptions, Listener, symbol, Transport } from
1111
import type { AbortOptions, Multiaddr } from '@multiformats/multiaddr'
1212
import type { Socket, IpcSocketConnectOpts, TcpSocketConnectOpts } from 'net'
1313
import type { Connection } from '@libp2p/interface-connection'
14-
import type { TcpMetrics } from './metrics.js'
14+
import { getMetrics, Metrics, MetricsRegister } from './metrics.js'
1515

1616
const log = logger('libp2p:tcp')
1717

18-
export { TcpMetrics }
19-
2018
export interface TCPOptions {
2119
/**
2220
* An optional number in ms that is used as an inactivity timeout after which the socket will be closed
@@ -60,11 +58,11 @@ export interface TCPCreateListenerOptions extends CreateListenerOptions, TCPSock
6058

6159
class TCP implements Transport {
6260
private readonly opts: TCPOptions
63-
private readonly metrics: TcpMetrics | null
61+
private readonly metrics: Metrics | null
6462

65-
constructor (options: TCPOptions = {}, metrics?: TcpMetrics | null) {
63+
constructor (options: TCPOptions = {}, metricsRegistry?: MetricsRegister | null) {
6664
this.opts = options
67-
this.metrics = metrics ?? null
65+
this.metrics = metricsRegistry != null ? getMetrics(metricsRegistry) : null
6866
}
6967

7068
get [symbol] (): true {

src/listener.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { MultiaddrConnection, Connection } from '@libp2p/interface-connecti
1111
import type { Upgrader, Listener, ListenerEvents } from '@libp2p/interface-transport'
1212
import type { Multiaddr } from '@multiformats/multiaddr'
1313
import type { TCPCreateListenerOptions } from './index.js'
14-
import { ServerStatusMetric, TcpMetrics } from './metrics.js'
14+
import { ServerStatusMetric, Metrics } from './metrics.js'
1515

1616
const log = logger('libp2p:tcp:listener')
1717

@@ -32,7 +32,7 @@ interface Context extends TCPCreateListenerOptions {
3232
socketInactivityTimeout?: number
3333
socketCloseTimeout?: number
3434
maxConnections?: number
35-
metrics: TcpMetrics | null
35+
metrics: Metrics | null
3636
}
3737

3838
type Status = {started: false} | {started: true, listeningAddr: Multiaddr, peerId: string | null }
@@ -41,7 +41,7 @@ export class TCPListener extends EventEmitter<ListenerEvents> implements Listene
4141
private readonly server: net.Server
4242
/** Keep track of open connections to destroy in case of timeout */
4343
private readonly connections = new Set<MultiaddrConnection>()
44-
private readonly metrics: TcpMetrics | null
44+
private readonly metrics: Metrics | null
4545

4646
private status: Status = { started: false }
4747

src/metrics.ts

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
1+
export enum ServerStatusMetric {
2+
stopped = 0,
3+
started = 1
4+
}
5+
6+
export function getMetrics (register: MetricsRegister) {
7+
return {
8+
serverStatus: register.gauge({
9+
name: 'libp2p_tcp_server_status',
10+
help: 'Current status of the TCP server'
11+
}),
12+
13+
connections: register.gauge({
14+
name: 'libp2p_tcp_connections_count',
15+
help: 'Current active connections in TCP listener'
16+
}),
17+
18+
listenerErrors: register.gauge<{ error: string }>({
19+
name: 'libp2p_tcp_listener_errors_total',
20+
help: 'Total count of TCP listener errors by error type',
21+
labelNames: ['error']
22+
}),
23+
24+
socketEvents: register.gauge<{ event: string }>({
25+
name: 'libp2p_tcp_socket_events',
26+
help: 'Total count of TCP socket events by event',
27+
labelNames: ['event']
28+
})
29+
}
30+
}
31+
32+
export type Metrics = ReturnType<typeof getMetrics>
33+
134
/* eslint-disable etc/prefer-interface, @typescript-eslint/method-signature-style */
235

36+
export interface MetricsRegister {
37+
gauge<T extends LabelsGeneric>(config: GaugeConfig<T>): Gauge<T>
38+
}
39+
40+
interface GaugeConfig<Labels extends LabelsGeneric> {
41+
name: string
42+
help: string
43+
labelNames?: keyof Labels extends string ? Array<keyof Labels> : undefined
44+
}
45+
346
type LabelsGeneric = Record<string, string | undefined>
447
type CollectFn<Labels extends LabelsGeneric> = (metric: Gauge<Labels>) => void
548

649
interface Gauge<Labels extends LabelsGeneric = never> {
7-
// Sorry for this mess, `prom-client` API choices are not great
8-
// If the function signature was `inc(value: number, labels?: Labels)`, this would be simpler
50+
// Follows `prom-client` API choices, to require less middleware on consumer
951
inc(value?: number): void
1052
inc(labels: Labels, value?: number): void
1153
inc(arg1?: Labels | number, arg2?: number): void
@@ -20,15 +62,3 @@ interface Gauge<Labels extends LabelsGeneric = never> {
2062

2163
addCollect(collectFn: CollectFn<Labels>): void
2264
}
23-
24-
export enum ServerStatusMetric {
25-
stopped = 0,
26-
started = 1
27-
}
28-
29-
export interface TcpMetrics {
30-
serverStatus: Gauge
31-
connections: Gauge
32-
listenerErrors: Gauge<{error: string}>
33-
socketEvents: Gauge<{event: string}>
34-
}

src/socket-to-conn.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import errCode from 'err-code'
99
import type { Socket } from 'net'
1010
import type { Multiaddr } from '@multiformats/multiaddr'
1111
import type { MultiaddrConnection } from '@libp2p/interface-connection'
12-
import type { TcpMetrics } from './metrics.js'
12+
import type { Metrics } from './metrics.js'
1313

1414
const log = logger('libp2p:tcp:socket')
1515

@@ -20,7 +20,7 @@ interface ToConnectionOptions {
2020
signal?: AbortSignal
2121
socketInactivityTimeout?: number
2222
socketCloseTimeout?: number
23-
metrics?: TcpMetrics | null
23+
metrics?: Metrics | null
2424
}
2525

2626
/**

0 commit comments

Comments
 (0)