This repository was archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.ts
187 lines (162 loc) · 5.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface-connection'
/**
* Create tracked metrics with these options. Loosely based on the
* interfaces exposed by the prom-client module
*/
export interface MetricOptions {
/**
* Optional label for the metric
*/
label?: string
/**
* Optional help for the metric
*/
help?: string
}
/**
* A function that returns a tracked metric which may be expensive
* to calculate so it is only invoked when metrics are being scraped
*/
export type CalculateMetric<T = number> = (() => T) | (() => Promise<T>)
/**
* Create tracked metrics that are expensive to calculate by passing
* a function that is only invoked when metrics are being scraped
*/
export interface CalculatedMetricOptions<T = number> extends MetricOptions {
/**
* An optional function invoked to calculate the component metric instead of
* using `.update`, `.increment`, and `.decrement`
*/
calculate: CalculateMetric<T>
}
/**
* Call this function to stop the timer returned from the `.timer` method
* on the metric
*/
export interface StopTimer { (): void }
/**
* A tracked metric loosely based on the interfaces exposed by the
* prom-client module
*/
export interface Metric {
/**
* Update the stored metric to the passed value
*/
update: (value: number) => void
/**
* Increment the metric by the passed value or 1
*/
increment: (value?: number) => void
/**
* Decrement the metric by the passed value or 1
*/
decrement: (value?: number) => void
/**
* Reset this metric to its default value
*/
reset: () => void
/**
* Start a timed metric, call the returned function to
* stop the timer
*/
timer: () => StopTimer
}
/**
* A group of related metrics loosely based on the interfaces exposed by the
* prom-client module
*/
export interface MetricGroup {
/**
* Update the stored metric group to the passed value
*/
update: (values: Record<string, number>) => void
/**
* Increment the metric group keys by the passed number or
* any non-numeric value to increment by 1
*/
increment: (values: Record<string, number | unknown>) => void
/**
* Decrement the metric group keys by the passed number or
* any non-numeric value to decrement by 1
*/
decrement: (values: Record<string, number | unknown>) => void
/**
* Reset the passed key in this metric group to its default value
* or all keys if no key is passed
*/
reset: () => void
/**
* Start a timed metric for the named key in the group, call
* the returned function to stop the timer
*/
timer: (key: string) => StopTimer
}
/**
* A tracked counter loosely based on the Counter interface exposed
* by the prom-client module - counters are metrics that only go up
*/
export interface Counter {
/**
* Increment the metric by the passed value or 1
*/
increment: (value?: number) => void
/**
* Reset this metric to its default value
*/
reset: () => void
}
/**
* A group of tracked counters loosely based on the Counter interface
* exposed by the prom-client module - counters are metrics that only
* go up
*/
export interface CounterGroup {
/**
* Increment the metric group keys by the passed number or
* any non-numeric value to increment by 1
*/
increment: (values: Record<string, number | unknown>) => void
/**
* Reset the passed key in this metric group to its default value
* or all keys if no key is passed
*/
reset: () => void
}
/**
* The libp2p metrics tracking object. This interface is only concerned
* with the collection of metrics, please see the individual implementations
* for how to extract metrics for viewing.
*/
export interface Metrics {
/**
* Track a newly opened multiaddr connection
*/
trackMultiaddrConnection: (maConn: MultiaddrConnection) => void
/**
* Track a newly opened protocol stream
*/
trackProtocolStream: (stream: Stream, connection: Connection) => void
/**
* Register an arbitrary metric. Call this to set help/labels for metrics
* and update/increment/decrement/etc them by calling methods on the returned
* metric object
*/
registerMetric: ((name: string, options?: MetricOptions) => Metric) & ((name: string, options: CalculatedMetricOptions) => void)
/**
* Register a a group of related metrics. Call this to set help/labels for
* groups of related metrics that will be updated with by calling `.update`,
* `.increment` and/or `.decrement` methods on the returned metric group object
*/
registerMetricGroup: ((name: string, options?: MetricOptions) => MetricGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
/**
* Register an arbitrary counter. Call this to set help/labels for counters
* and increment them by calling methods on the returned counter object
*/
registerCounter: ((name: string, options?: MetricOptions) => Counter) & ((name: string, options: CalculatedMetricOptions) => void)
/**
* Register a a group of related counters. Call this to set help/labels for
* groups of related counters that will be updated with by calling the `.increment`
* method on the returned counter group object
*/
registerCounterGroup: ((name: string, options?: MetricOptions) => CounterGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
}