Skip to content
Open
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
16 changes: 8 additions & 8 deletions packages/metrics-simple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SimpleMetric implements Metric {
}

class SimpleGroupMetric implements MetricGroup {
private values: Record<string, number> = {}
private values: Record<string, number> = Object.create(null)
private readonly calculate?: CalculateMetric<Record<string, number>>

constructor (opts?: CalculatedMetricOptions<Record<string, number>>) {
Expand Down Expand Up @@ -112,7 +112,7 @@ class SimpleGroupMetric implements MetricGroup {
}

reset (): void {
this.values = {}
this.values = Object.create(null)
}

timer (key: string): StopTimer {
Expand Down Expand Up @@ -182,12 +182,12 @@ class SimpleHistogram implements Histogram {
}

class SimpleHistogramGroup implements HistogramGroup {
public histograms: Record<string, SimpleHistogram> = {}
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 = {}
this.histograms = Object.create(null)
this.opts = {
buckets: opts?.buckets
}
Expand All @@ -199,7 +199,7 @@ class SimpleHistogramGroup implements HistogramGroup {
this.observe(await this.calculate())
}

const output: Record<string, { count: number, sum: number, buckets: Record<number, number> }> = {}
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()
Expand Down Expand Up @@ -286,12 +286,12 @@ class SimpleSummary implements Summary {
}

class SimpleSummaryGroup implements SummaryGroup {
public summaries: Record<string, SimpleSummary> = {}
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 = {}
this.summaries = Object.create(null)
this.opts = {
percentiles: opts?.percentiles,
compressCount: opts?.compressCount
Expand Down Expand Up @@ -405,7 +405,7 @@ class SimpleMetrics implements Metrics, Startable {

private _emitMetrics (): void {
Promise.resolve().then(async () => {
const output: Record<string, any> = {}
const output: Record<string, any> = Object.create(null)

for (const [name, metric] of this.metrics.entries()) {
output[name] = await metric.collect()
Expand Down
27 changes: 27 additions & 0 deletions packages/metrics-simple/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ import type { Metrics } from '@libp2p/interface'
describe('simple-metrics', () => {
let s: Metrics

it('should support metric names and labels that match object prototype properties', async () => {
const deferred = pDefer<Record<string, any>>()
s = simpleMetrics({ onMetrics: deferred.resolve, intervalMs: 10 })({
logger: defaultLogger()
})
s.registerCounter('__proto__').increment(7)
const group = s.registerMetricGroup('group')
const counters = s.registerCounterGroup('counters')
const histograms = s.registerHistogramGroup('histograms')
const summaries = s.registerSummaryGroup('summaries')
for (const label of ['constructor', 'toString', '__proto__']) {
group.update({ [label]: 2 })
counters.increment({ [label]: 3 })
histograms.observe({ [label]: 4 })
summaries.observe({ [label]: 5 })
}
await start(s)
const metrics = await deferred.promise
expect(metrics).to.have.own.property('__proto__', 7)
for (const label of ['constructor', 'toString', '__proto__']) {
expect(metrics.group[label]).to.equal(2)
expect(metrics.counters[label]).to.equal(3)
expect(metrics.histograms[label].count).to.equal(1)
expect(metrics.summaries[label].count).to.equal(1)
}
})

afterEach(async () => {
if (s != null) {
await stop(s)
Expand Down