Skip to content

Commit 0076c1f

Browse files
authored
fix: metrics only need numbers (libp2p#312)
We don't use bigints for metrics anywhere and some scrapers like `prom-client` don't support them so leave them out of the interface for now.
1 parent 78039b3 commit 0076c1f

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

packages/interface-metrics/src/index.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export interface MetricOptions {
2020
* A function that returns a tracked metric which may be expensive
2121
* to calculate so it is only invoked when metrics are being scraped
2222
*/
23-
export type CalculateMetric<T = number | bigint> = (() => T) | (() => Promise<T>)
23+
export type CalculateMetric<T = number> = (() => T) | (() => Promise<T>)
2424

2525
/**
2626
* Create tracked metrics that are expensive to calculate by passing
2727
* a function that is only invoked when metrics are being scraped
2828
*/
29-
export interface CalculatedMetricOptions<T = number | bigint> extends MetricOptions {
29+
export interface CalculatedMetricOptions<T = number> extends MetricOptions {
3030
/**
3131
* An optional function invoked to calculate the component metric instead of
3232
* using `.update`, `.increment`, and `.decrement`
@@ -48,17 +48,17 @@ export interface Metric {
4848
/**
4949
* Update the stored metric to the passed value
5050
*/
51-
update: (value: number | bigint) => void
51+
update: (value: number) => void
5252

5353
/**
5454
* Increment the metric by the passed value or 1
5555
*/
56-
increment: (value?: number | bigint) => void
56+
increment: (value?: number) => void
5757

5858
/**
5959
* Decrement the metric by the passed value or 1
6060
*/
61-
decrement: (value?: number | bigint) => void
61+
decrement: (value?: number) => void
6262

6363
/**
6464
* Reset this metric to its default value
@@ -80,19 +80,19 @@ export interface MetricGroup {
8080
/**
8181
* Update the stored metric group to the passed value
8282
*/
83-
update: (values: Record<string, number | bigint>) => void
83+
update: (values: Record<string, number>) => void
8484

8585
/**
8686
* Increment the metric group keys by the passed number or
8787
* any non-numeric value to increment by 1
8888
*/
89-
increment: (values: Record<string, number | bigint | unknown>) => void
89+
increment: (values: Record<string, number | unknown>) => void
9090

9191
/**
9292
* Decrement the metric group keys by the passed number or
9393
* any non-numeric value to decrement by 1
9494
*/
95-
decrement: (values: Record<string, number | bigint | unknown>) => void
95+
decrement: (values: Record<string, number | unknown>) => void
9696

9797
/**
9898
* Reset the passed key in this metric group to its default value
@@ -115,7 +115,7 @@ export interface Counter {
115115
/**
116116
* Increment the metric by the passed value or 1
117117
*/
118-
increment: (value?: number | bigint) => void
118+
increment: (value?: number) => void
119119

120120
/**
121121
* Reset this metric to its default value
@@ -133,7 +133,7 @@ export interface CounterGroup {
133133
* Increment the metric group keys by the passed number or
134134
* any non-numeric value to increment by 1
135135
*/
136-
increment: (values: Record<string, number | bigint | unknown>) => void
136+
increment: (values: Record<string, number | unknown>) => void
137137

138138
/**
139139
* Reset the passed key in this metric group to its default value
@@ -170,7 +170,7 @@ export interface Metrics {
170170
* groups of related metrics that will be updated with by calling `.update`,
171171
* `.increment` and/or `.decrement` methods on the returned metric group object
172172
*/
173-
registerMetricGroup: ((name: string, options?: MetricOptions) => MetricGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number | bigint>>) => void)
173+
registerMetricGroup: ((name: string, options?: MetricOptions) => MetricGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
174174

175175
/**
176176
* Register an arbitrary counter. Call this to set help/labels for counters
@@ -183,5 +183,5 @@ export interface Metrics {
183183
* groups of related counters that will be updated with by calling the `.increment`
184184
* method on the returned counter group object
185185
*/
186-
registerCounterGroup: ((name: string, options?: MetricOptions) => CounterGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number | bigint>>) => void)
186+
registerCounterGroup: ((name: string, options?: MetricOptions) => CounterGroup) & ((name: string, options: CalculatedMetricOptions<Record<string, number>>) => void)
187187
}

packages/interface-mocks/src/metrics.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface-
44
class DefaultMetric implements Metric {
55
public value: number = 0
66

7-
update (value: number | bigint): void {
8-
this.value = Number(value)
7+
update (value: number): void {
8+
this.value = value
99
}
1010

11-
increment (value: number | bigint = 1): void {
12-
this.value += Number(value)
11+
increment (value: number = 1): void {
12+
this.value += value
1313
}
1414

15-
decrement (value: number | bigint = 1): void {
16-
this.value -= Number(value)
15+
decrement (value: number = 1): void {
16+
this.value -= value
1717
}
1818

1919
reset (): void {
@@ -32,25 +32,25 @@ class DefaultMetric implements Metric {
3232
class DefaultGroupMetric implements MetricGroup {
3333
public values: Record<string, number> = {}
3434

35-
update (values: Record<string, number | bigint>): void {
35+
update (values: Record<string, number>): void {
3636
Object.entries(values).forEach(([key, value]) => {
37-
this.values[key] = Number(value)
37+
this.values[key] = value
3838
})
3939
}
4040

41-
increment (values: Record<string, number | bigint | unknown>): void {
41+
increment (values: Record<string, number | unknown>): void {
4242
Object.entries(values).forEach(([key, value]) => {
4343
this.values[key] = this.values[key] ?? 0
44-
const inc = typeof value === 'number' || typeof value === 'bigint' ? value : 1
44+
const inc = typeof value === 'number' ? value : 1
4545

4646
this.values[key] += Number(inc)
4747
})
4848
}
4949

50-
decrement (values: Record<string, number | bigint | unknown>): void {
50+
decrement (values: Record<string, number | unknown>): void {
5151
Object.entries(values).forEach(([key, value]) => {
5252
this.values[key] = this.values[key] ?? 0
53-
const dec = typeof value === 'number' || typeof value === 'bigint' ? value : 1
53+
const dec = typeof value === 'number' ? value : 1
5454

5555
this.values[key] -= Number(dec)
5656
})

0 commit comments

Comments
 (0)