Skip to content

Commit a4b2db1

Browse files
authored
fix: fix metric group timers (#2789)
We need to pass the metric label as the object key, otherwise trying to stop a group timer throws.
1 parent 7383821 commit a4b2db1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/metrics-prometheus/src/metric-group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class PrometheusMetricGroup implements MetricGroup, CalculatedMetric<Reco
7272

7373
timer (key: string): StopTimer {
7474
return this.gauge.startTimer({
75-
key: 0
75+
[this.label]: key
7676
})
7777
}
7878
}

packages/metrics-prometheus/test/metric-groups.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,24 @@ describe('metric groups', () => {
183183
expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey1}"} ${metricValue1}`, 'did not include updated metric')
184184
expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey2}"} ${metricValue2}`, 'did not include updated metric')
185185
})
186+
187+
it('should allow grouped timers', async () => {
188+
const metricName = randomMetricName()
189+
const metricLabel = randomMetricName('label_')
190+
const metricKey = randomMetricName('key_')
191+
const metrics = prometheusMetrics()({
192+
logger: defaultLogger()
193+
})
194+
const metric1 = metrics.registerMetricGroup(metricName, {
195+
label: metricLabel
196+
})
197+
198+
const timer = metric1.timer(metricKey)
199+
200+
timer()
201+
202+
const reportedMetrics = await client.register.metrics()
203+
204+
expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey}"}`, 'did not include updated metric')
205+
})
186206
})

0 commit comments

Comments
 (0)