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

fix: pass numbers to prom-client #1

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
},
"dependencies": {
"@libp2p/interface-connection": "^3.0.2",
"@libp2p/interface-metrics": "^4.0.0",
"@libp2p/interface-metrics": "^4.0.2",
"@libp2p/logger": "^2.0.2",
"it-foreach": "^1.0.0",
"it-stream-types": "^1.0.4"
Expand Down
14 changes: 5 additions & 9 deletions src/counter-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ export class PrometheusCounterGroup implements CounterGroup {
private readonly counter: PromCounter
private readonly label: string

constructor (name: string, opts: CalculatedMetricOptions<Record<string, number | bigint>>) {
constructor (name: string, opts: CalculatedMetricOptions<Record<string, number>>) {
name = normaliseString(name)
const help = normaliseString(opts.help ?? name)
const label = this.label = normaliseString(opts.label ?? name)
let collect: CollectFunction<PromCounter<any>> | undefined

// calculated metric
if (opts?.calculate != null) {
const calculate: CalculateMetric<Record<string, number | bigint>> = opts.calculate
const calculate: CalculateMetric<Record<string, number>> = opts.calculate

collect = async function () {
const values = await calculate()

Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.inc({ [label]: key }, Number(value))
this.inc({ [label]: key }, value)
})
}
}
Expand All @@ -35,11 +33,9 @@ export class PrometheusCounterGroup implements CounterGroup {
})
}

increment (values: Record<string, number | bigint | unknown>): void {
increment (values: Record<string, number | unknown>): void {
Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
const inc = typeof value === 'number' || typeof value === 'bigint' ? Number(value) : 1
const inc = typeof value === 'number' ? value : 1

this.counter.inc({ [this.label]: key }, inc)
})
Expand Down
8 changes: 3 additions & 5 deletions src/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PrometheusCounter implements Counter {
collect = async function () {
const value = await calculate()

this.inc(Number(value))
this.inc(value)
}
}

Expand All @@ -30,10 +30,8 @@ export class PrometheusCounter implements Counter {
})
}

increment (value: number | bigint = 1): void {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.counter.inc(Number(value))
increment (value: number = 1): void {
this.counter.inc(value)
}

reset (): void {
Expand Down
24 changes: 8 additions & 16 deletions src/metric-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ export class PrometheusMetricGroup implements MetricGroup {
private readonly gauge: Gauge
private readonly label: string

constructor (name: string, opts: CalculatedMetricOptions<Record<string, number | bigint>>) {
constructor (name: string, opts: CalculatedMetricOptions<Record<string, number>>) {
name = normaliseString(name)
const help = normaliseString(opts.help ?? name)
const label = this.label = normaliseString(opts.label ?? name)
let collect: CollectFunction<Gauge<any>> | undefined

// calculated metric
if (opts?.calculate != null) {
const calculate: CalculateMetric<Record<string, number | bigint>> = opts.calculate
const calculate: CalculateMetric<Record<string, number>> = opts.calculate

collect = async function () {
const values = await calculate()

Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.set({ [label]: key }, Number(value))
this.set({ [label]: key }, value)
})
}
}
Expand All @@ -35,29 +33,23 @@ export class PrometheusMetricGroup implements MetricGroup {
})
}

update (values: Record<string, number | bigint>): void {
update (values: Record<string, number>): void {
Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.gauge.set({ [this.label]: key }, Number(value))
this.gauge.set({ [this.label]: key }, value)
})
}

increment (values: Record<string, number | bigint | unknown>): void {
increment (values: Record<string, number | unknown>): void {
Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
const inc = typeof value === 'number' || typeof value === 'bigint' ? Number(value) : 1
const inc = typeof value === 'number' ? value : 1

this.gauge.inc({ [this.label]: key }, inc)
})
}

decrement (values: Record<string, number | unknown>): void {
Object.entries(values).forEach(([key, value]) => {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
const dec = typeof value === 'number' || typeof value === 'bigint' ? Number(value) : 1
const dec = typeof value === 'number' ? value : 1

this.gauge.dec({ [this.label]: key }, dec)
})
Expand Down
22 changes: 7 additions & 15 deletions src/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ export class PrometheusMetric implements Metric {
collect = async function () {
const value = await calculate()

// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.set(Number(value))
this.set(value)
}
}

Expand All @@ -32,22 +30,16 @@ export class PrometheusMetric implements Metric {
})
}

update (value: number | bigint): void {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.gauge.set(Number(value))
update (value: number): void {
this.gauge.set(value)
}

increment (value: number | bigint = 1): void {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.gauge.inc(Number(value))
increment (value: number = 1): void {
this.gauge.inc(value)
}

decrement (value: number | bigint = 1): void {
// prom-client does not support bigints for values
// https://github.com/siimon/prom-client/issues/259
this.gauge.dec(Number(value))
decrement (value: number = 1): void {
this.gauge.dec(value)
}

reset (): void {
Expand Down