Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
646 lines (512 loc) · 17 KB
/
Copy pathindex.ts
File metadata and controls
646 lines (512 loc) · 17 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/**
* @packageDocumentation
*
* Stores metrics in memory and periodically invokes a configured callback to
* receive them.
*
* @example
*
* ```ts
* import { createLibp2p } from 'libp2p'
* import { simpleMetrics } from '@libp2p/simple-metrics'
*
* const node = await createLibp2p({
* // ... other options
* metrics: simpleMetrics({
* onMetrics: (metrics) => {
* // do something with metrics
* }
* }),
* intervalMs: 1000 // default 1s
* })
*
* ```
*/
import { serviceCapabilities } from '@libp2p/interface'
import { logger } from '@libp2p/logger'
import { TDigest } from 'tdigest'
import type { Startable, MultiaddrConnection, Stream, Metric, MetricGroup, StopTimer, Metrics, CalculatedMetricOptions, MetricOptions, Counter, CounterGroup, CalculateMetric, Histogram, HistogramOptions, HistogramGroup, Summary, SummaryOptions, SummaryGroup, CalculatedHistogramOptions, CalculatedSummaryOptions, ComponentLogger, Logger, MessageStream } from '@libp2p/interface'
const log = logger('libp2p:simple-metrics')
class SimpleMetric implements Metric {
private value: number = 0
private readonly calculate?: CalculateMetric
constructor (opts?: CalculatedMetricOptions) {
this.calculate = opts?.calculate
}
async collect (): Promise<number> {
if (this.calculate != null) {
return this.calculate()
}
return this.value
}
update (value: number): void {
this.value = value
}
increment (value: number = 1): void {
this.value += value
}
decrement (value: number = 1): void {
this.value -= value
}
reset (): void {
this.value = 0
}
timer (): StopTimer {
const start = Date.now()
return () => {
this.value = Date.now() - start
}
}
}
class SimpleGroupMetric implements MetricGroup {
private values: Record<string, number> = Object.create(null)
private readonly calculate?: CalculateMetric<Record<string, number>>
constructor (opts?: CalculatedMetricOptions<Record<string, number>>) {
this.calculate = opts?.calculate
}
async collect (): Promise<Record<string, number>> {
if (this.calculate != null) {
return this.calculate()
}
return this.values
}
update (values: Record<string, number>): void {
Object.entries(values).forEach(([key, value]) => {
this.values[key] = value
})
}
increment (values: Record<string, number | unknown>): void {
Object.entries(values).forEach(([key, value]) => {
this.values[key] = this.values[key] ?? 0
const inc = typeof value === 'number' ? value : 1
this.values[key] += Number(inc)
})
}
decrement (values: Record<string, number | unknown>): void {
Object.entries(values).forEach(([key, value]) => {
this.values[key] = this.values[key] ?? 0
const dec = typeof value === 'number' ? value : 1
this.values[key] -= Number(dec)
})
}
reset (): void {
this.values = Object.create(null)
}
timer (key: string): StopTimer {
const start = Date.now()
return () => {
this.values[key] = Date.now() - start
}
}
}
class SimpleHistogram implements Histogram {
private bucketValues = new Map<number, number>()
private countValue: number = 0
private sumValue: number = 0
private readonly calculate?: CalculateMetric
constructor (opts?: Partial<CalculatedHistogramOptions>) {
const buckets = [
...(opts?.buckets ?? [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]),
Infinity
]
for (const bucket of buckets) {
this.bucketValues.set(bucket, 0)
}
this.calculate = opts?.calculate
}
public async collect (): Promise<{ count: number, sum: number, buckets: Record<number, number> }> {
if (this.calculate != null) {
this.observe(await this.calculate())
}
return {
count: this.countValue,
sum: this.sumValue,
buckets: Object.fromEntries(this.bucketValues)
}
}
observe (value: number): void {
this.countValue++
this.sumValue += value
for (const [bucket, count] of this.bucketValues.entries()) {
if (value <= bucket) {
this.bucketValues.set(bucket, count + 1)
}
}
}
reset (): void {
this.countValue = 0
this.sumValue = 0
for (const bucket of this.bucketValues.keys()) {
this.bucketValues.set(bucket, 0)
}
}
timer (): StopTimer {
const start = Date.now()
return () => {
this.observe(Date.now() - start)
}
}
}
class SimpleHistogramGroup implements HistogramGroup {
public histograms: Record<string, SimpleHistogram> = Object.create(null)
private readonly opts: HistogramOptions
private readonly calculate?: CalculateMetric<Record<string, number>>
constructor (opts?: Partial<CalculatedHistogramOptions<Record<string, number>>>) {
this.histograms = Object.create(null)
this.opts = {
buckets: opts?.buckets
}
this.calculate = opts?.calculate
}
public async collect (): Promise<Record<string, { count: number, sum: number, buckets: Record<number, number> }>> {
if (this.calculate != null) {
this.observe(await this.calculate())
}
const output: Record<string, { count: number, sum: number, buckets: Record<number, number> }> = Object.create(null)
for (const [key, histogram] of Object.entries(this.histograms)) {
output[key] = await histogram.collect()
}
return output
}
observe (values: Partial<Record<string, number>>): void {
for (const [key, value] of Object.entries(values) as Array<[string, number]>) {
if (this.histograms[key] === undefined) {
this.histograms[key] = new SimpleHistogram(this.opts)
}
this.histograms[key].observe(value)
}
}
reset (): void {
for (const histogram of Object.values(this.histograms)) {
histogram.reset()
}
}
timer (key: string): StopTimer {
const start = Date.now()
return () => {
this.observe({ [key]: Date.now() - start })
}
}
}
class SimpleSummary implements Summary {
public sumValue: number = 0
public countValue: number = 0
public percentiles: number[]
public tdigest = new TDigest(0.01)
private readonly compressCount: number
private readonly calculate?: CalculateMetric
constructor (opts?: Partial<CalculatedSummaryOptions>) {
this.percentiles = opts?.percentiles ?? [0.01, 0.05, 0.5, 0.9, 0.95, 0.99, 0.999]
this.compressCount = opts?.compressCount ?? 1000
this.calculate = opts?.calculate
}
public async collect (): Promise<{ count: number, sum: number, percentiles: Record<string, number> }> {
if (this.calculate != null) {
this.observe(await this.calculate())
}
return {
count: this.countValue,
sum: this.sumValue,
percentiles: Object.fromEntries(this.percentiles.map(p => [p, this.tdigest.percentile(p)]))
}
}
observe (value: number): void {
this.sumValue += value
this.countValue++
this.tdigest.push(value)
if (this.tdigest.size() > this.compressCount) {
this.tdigest.compress()
}
}
reset (): void {
this.sumValue = 0
this.countValue = 0
this.tdigest.reset()
}
timer (): StopTimer {
const start = Date.now()
return () => {
this.observe(Date.now() - start)
}
}
}
class SimpleSummaryGroup implements SummaryGroup {
public summaries: Record<string, SimpleSummary> = Object.create(null)
private readonly opts: SummaryOptions
private readonly calculate?: CalculateMetric<Record<string, number>>
constructor (opts?: Partial<CalculatedSummaryOptions<Record<string, number>>>) {
this.summaries = Object.create(null)
this.opts = {
percentiles: opts?.percentiles,
compressCount: opts?.compressCount
}
this.calculate = opts?.calculate
}
public async collect (): Promise<Record<string, { count: number, sum: number, percentiles: Record<string, number> }>> {
if (this.calculate != null) {
this.observe(await this.calculate())
}
return {
...Object.fromEntries(Object.entries(this.summaries).map(([key, summary]) => {
return [key, {
count: summary.countValue,
sum: summary.sumValue,
percentiles: Object.fromEntries(summary.percentiles.map(p => [p, summary.tdigest.percentile(p)]))
}]
}))
}
}
observe (values: Record<string, number>): void {
for (const [key, value] of Object.entries(values)) {
if (this.summaries[key] === undefined) {
this.summaries[key] = new SimpleSummary(this.opts)
}
this.summaries[key].observe(value)
}
}
reset (): void {
for (const summary of Object.values(this.summaries)) {
summary.reset()
}
}
timer (key: string): StopTimer {
const start = Date.now()
return () => {
this.observe({ [key]: Date.now() - start })
}
}
}
export interface OnMetrics { (metrics: Record<string, any>): void }
export interface SimpleMetricsInit {
/**
* How often to invoke the onMetrics callback
*/
intervalMs?: number
/**
* A callback periodically invoked with collected metrics
*/
onMetrics: OnMetrics
}
export interface SimpleMetricsComponents {
logger: ComponentLogger
}
class SimpleMetrics implements Metrics, Startable {
public metrics = new Map<string, SimpleMetric | SimpleGroupMetric | SimpleHistogram | SimpleHistogramGroup | SimpleSummary | SimpleSummaryGroup>()
private readonly transferStats: Map<string, number>
private started: boolean
private interval?: ReturnType<typeof setInterval>
private readonly intervalMs: number
private readonly onMetrics: OnMetrics
private readonly log: Logger
constructor (components: SimpleMetricsComponents, init: SimpleMetricsInit) {
this.log = components.logger.forComponent('libp2p:simple-metrics')
this.started = false
this._emitMetrics = this._emitMetrics.bind(this)
this.intervalMs = init.intervalMs ?? 1000
this.onMetrics = init.onMetrics
// holds global and per-protocol sent/received stats
this.transferStats = new Map()
}
readonly [Symbol.toStringTag] = '@libp2p/metrics-simple'
readonly [serviceCapabilities]: string[] = [
'@libp2p/metrics'
]
isStarted (): boolean {
return this.started
}
start (): void {
this.started = true
this.interval = setInterval(this._emitMetrics, this.intervalMs)
}
stop (): void {
this.started = false
clearInterval(this.interval)
this.transferStats.clear()
}
private _emitMetrics (): void {
Promise.resolve().then(async () => {
const output: Record<string, any> = Object.create(null)
for (const [name, metric] of this.metrics.entries()) {
output[name] = await metric.collect()
}
this.onMetrics(structuredClone(output))
})
.catch(err => {
log.error('could not invoke onMetrics callback - %e', err)
})
}
/**
* Increment the transfer stat for the passed key, making sure
* it exists first
*/
_incrementValue (key: string, value: number): void {
const existing = this.transferStats.get(key) ?? 0
this.transferStats.set(key, existing + value)
}
/**
* Override the sink/source of the stream to count the bytes
* in and out
*/
_track (stream: MessageStream, name: string): void {
// AbstractMessageStream dispatches buffered inbound data only when there is
// at least one `message` listener. Adding a metrics listener too early can
// consume the read buffer before protocol handlers attach their own readers
// (e.g. identify), causing lost first frames.
//
// Instead, wrap dispatchEvent to observe already-dispatched message events
// without increasing listenerCount('message').
const dispatchEvent = stream.dispatchEvent.bind(stream)
stream.dispatchEvent = (evt: Event): boolean => {
if (evt.type === 'message') {
this._incrementValue(`${name} received`, (evt as MessageEvent<{ byteLength: number }>).data.byteLength)
}
return dispatchEvent(evt)
}
const send = stream.send.bind(stream)
stream.send = (buf) => {
this._incrementValue(`${name} sent`, buf.byteLength)
return send(buf)
}
}
trackMultiaddrConnection (maConn: MultiaddrConnection): void {
this._track(maConn, 'global')
}
trackProtocolStream (stream: Stream): void {
if (stream.protocol == null) {
// protocol not negotiated yet, should not happen as the upgrader
// calls this handler after protocol negotiation
return
}
this._track(stream, stream.protocol)
}
registerMetric (name: string, opts: CalculatedMetricOptions): void
registerMetric (name: string, opts?: MetricOptions): Metric
registerMetric (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleMetric(opts)
this.metrics.set(name, metric)
return metric
}
registerMetricGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
registerMetricGroup (name: string, opts?: MetricOptions): MetricGroup
registerMetricGroup (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleGroupMetric(opts)
this.metrics.set(name, metric)
return metric
}
registerCounter (name: string, opts: CalculatedMetricOptions): void
registerCounter (name: string, opts?: MetricOptions): Counter
registerCounter (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleMetric(opts)
this.metrics.set(name, metric)
return metric
}
registerCounterGroup (name: string, opts: CalculatedMetricOptions<Record<string, number>>): void
registerCounterGroup (name: string, opts?: MetricOptions): CounterGroup
registerCounterGroup (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleGroupMetric(opts)
this.metrics.set(name, metric)
return metric
}
registerHistogram (name: string, opts: CalculatedHistogramOptions): void
registerHistogram (name: string, opts?: HistogramOptions): Histogram
registerHistogram (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleHistogram(opts)
this.metrics.set(name, metric)
return metric
}
registerHistogramGroup (name: string, opts: CalculatedHistogramOptions<Record<string, number>>): void
registerHistogramGroup (name: string, opts?: HistogramOptions): HistogramGroup
registerHistogramGroup (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleHistogramGroup(opts)
this.metrics.set(name, metric)
return metric
}
registerSummary (name: string, opts: CalculatedSummaryOptions): void
registerSummary (name: string, opts?: SummaryOptions): Summary
registerSummary (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleSummary(opts)
this.metrics.set(name, metric)
return metric
}
registerSummaryGroup (name: string, opts: CalculatedSummaryOptions<Record<string, number>>): void
registerSummaryGroup (name: string, opts?: SummaryOptions): SummaryGroup
registerSummaryGroup (name: string, opts: any = {}): any {
if (name == null || name.trim() === '') {
throw new Error('Metric name is required')
}
let metric = this.metrics.get(name)
if (metric != null) {
this.log('reuse existing metric', name)
return metric
}
metric = new SimpleSummaryGroup(opts)
this.metrics.set(name, metric)
return metric
}
createTrace (): any {
// no-op
}
traceFunction <T extends (...args: any[]) => any> (name: string, fn: T): T {
// no-op
return fn
}
}
export function simpleMetrics (init: SimpleMetricsInit): (components: SimpleMetricsComponents) => Metrics {
return (components) => new SimpleMetrics(components, init)
}