Skip to content

Commit

Permalink
fix: fix metric group timers (#2789)
Browse files Browse the repository at this point in the history
We need to pass the metric label as the object key, otherwise trying
to stop a group timer throws.
  • Loading branch information
achingbrain authored Oct 28, 2024
1 parent 7383821 commit a4b2db1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/metrics-prometheus/src/metric-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class PrometheusMetricGroup implements MetricGroup, CalculatedMetric<Reco

timer (key: string): StopTimer {
return this.gauge.startTimer({
key: 0
[this.label]: key
})
}
}
20 changes: 20 additions & 0 deletions packages/metrics-prometheus/test/metric-groups.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,24 @@ describe('metric groups', () => {
expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey1}"} ${metricValue1}`, 'did not include updated metric')
expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey2}"} ${metricValue2}`, 'did not include updated metric')
})

it('should allow grouped timers', async () => {
const metricName = randomMetricName()
const metricLabel = randomMetricName('label_')
const metricKey = randomMetricName('key_')
const metrics = prometheusMetrics()({
logger: defaultLogger()
})
const metric1 = metrics.registerMetricGroup(metricName, {
label: metricLabel
})

const timer = metric1.timer(metricKey)

timer()

const reportedMetrics = await client.register.metrics()

expect(reportedMetrics).to.include(`${metricName}{${metricLabel}="${metricKey}"}`, 'did not include updated metric')
})
})

0 comments on commit a4b2db1

Please sign in to comment.