Skip to content

Commit df33069

Browse files
authored
fix: add optional generics to metric groups (#2665)
To give a bit more type safety to metric groups, add optional generics that allow you to specify which grouped metrics you can update/increment.
1 parent 1bc7c01 commit df33069

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

packages/interface/src/metrics/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ export interface Metric {
7676
* A group of related metrics loosely based on the interfaces exposed by the
7777
* prom-client module
7878
*/
79-
export interface MetricGroup {
79+
export interface MetricGroup<T extends string = any> {
8080
/**
8181
* Update the stored metric group to the passed value
8282
*/
83-
update(values: Record<string, number>): void
83+
update(values: Partial<Record<T, number>>): void
8484

8585
/**
8686
* Increment the metric group keys by the passed number or
87-
* any non-numeric value to increment by 1
87+
* `true` to increment by 1
8888
*/
89-
increment(values: Record<string, number | unknown>): void
89+
increment(values: Partial<Record<T, number | true>>): void
9090

9191
/**
9292
* Decrement the metric group keys by the passed number or
93-
* any non-numeric value to decrement by 1
93+
* `true` to decrement by 1
9494
*/
95-
decrement(values: Record<string, number | unknown>): void
95+
decrement(values: Partial<Record<T, number | true>>): void
9696

9797
/**
9898
* Reset the passed key in this metric group to its default value
@@ -128,12 +128,12 @@ export interface Counter {
128128
* exposed by the prom-client module - counters are metrics that only
129129
* go up
130130
*/
131-
export interface CounterGroup {
131+
export interface CounterGroup<T extends string = any> {
132132
/**
133133
* Increment the metric group keys by the passed number or
134134
* any non-numeric value to increment by 1
135135
*/
136-
increment(values: Record<string, number | unknown>): void
136+
increment(values: Partial<Record<T, number | true>>): void
137137

138138
/**
139139
* Reset the passed key in this metric group to its default value

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('metric groups', () => {
3434
label: metricLabel
3535
})
3636
metric.increment({
37-
[metricKey]: false
37+
[metricKey]: true
3838
})
3939

4040
await expect(client.register.metrics()).to.eventually.include(`${metricName}{${metricLabel}="${metricKey}"} 1`, 'did not include updated metric')
@@ -69,7 +69,7 @@ describe('metric groups', () => {
6969
label: metricLabel
7070
})
7171
metric.decrement({
72-
[metricKey]: false
72+
[metricKey]: true
7373
})
7474

7575
await expect(client.register.metrics()).to.eventually.include(`${metricName}{${metricLabel}="${metricKey}"} -1`, 'did not include updated metric')

packages/transport-tcp/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export interface TCPComponents {
121121
}
122122

123123
export interface TCPMetrics {
124-
dialerEvents: CounterGroup
124+
dialerEvents: CounterGroup<'error' | 'timeout' | 'connect' | 'abort'>
125125
}
126126

127127
export function tcp (init: TCPOptions = {}): (components: TCPComponents) => Transport {

0 commit comments

Comments
 (0)