Skip to content

Commit

Permalink
fix: allow creating counter groups (#2550)
Browse files Browse the repository at this point in the history
Fixes a small bug in the creation of in-memory metrics.
  • Loading branch information
achingbrain authored May 17, 2024
1 parent 1eb5b27 commit 8214dcf
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/metrics-simple/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ class SimpleMetrics implements Metrics, Startable {
return
}

const metric = new DefaultMetric()
const metric = new DefaultGroupMetric()
this.metrics.set(name, metric)

return metric
Expand All @@ -264,7 +264,7 @@ class SimpleMetrics implements Metrics, Startable {
return
}

const metric = new DefaultGroupMetric()
const metric = new DefaultMetric()
this.metrics.set(name, metric)

return metric
Expand Down
88 changes: 88 additions & 0 deletions packages/metrics-simple/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,92 @@ describe('simple-metrics', () => {

expect(list).to.not.have.nested.property('[1].foo.baz')
})

it('should create a metric', async () => {
const deferred = pDefer<Record<string, any>>()

s = simpleMetrics({
onMetrics: (metrics) => {
deferred.resolve(metrics)
},
intervalMs: 10
})({})

await start(s)

const m = s.registerMetric('test_metric')
m.update(10)

const metrics = await deferred.promise
expect(metrics).to.have.property('test_metric', 10)
})

it('should create a counter metric', async () => {
const deferred = pDefer<Record<string, any>>()

s = simpleMetrics({
onMetrics: (metrics) => {
deferred.resolve(metrics)
},
intervalMs: 10
})({})

await start(s)

const m = s.registerCounter('test_metric')
m.increment()

const metrics = await deferred.promise
expect(metrics).to.have.property('test_metric', 1)
})

it('should create a metric group', async () => {
const deferred = pDefer<Record<string, any>>()

s = simpleMetrics({
onMetrics: (metrics) => {
deferred.resolve(metrics)
},
intervalMs: 10
})({})

await start(s)

const m = s.registerMetricGroup('test_metric')
m.update({
foo: 10,
bar: 20
})

const metrics = await deferred.promise
expect(metrics).to.have.deep.property('test_metric', {
foo: 10,
bar: 20
})
})

it('should create a metric counter group', async () => {
const deferred = pDefer<Record<string, any>>()

s = simpleMetrics({
onMetrics: (metrics) => {
deferred.resolve(metrics)
},
intervalMs: 10
})({})

await start(s)

const m = s.registerCounterGroup('test_metric')
m.increment({
foo: 10,
bar: 20
})

const metrics = await deferred.promise
expect(metrics).to.have.deep.property('test_metric', {
foo: 10,
bar: 20
})
})
})

0 comments on commit 8214dcf

Please sign in to comment.