Skip to content

Commit 120a651

Browse files
committed
feat: add support for custom counter increment value
1 parent 67bf1b8 commit 120a651

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ export class UserService {
133133
user_type: 'standard'
134134
} );
135135

136+
// Increment counter with custom value
137+
ReporterService.counter( 'batch_users_created_total', {
138+
source: 'batch',
139+
user_type: 'standard'
140+
}, 5 );
141+
136142
// Update active user gauge
137143
ReporterService.gauge( 'active_users', 42, {
138144
region: 'us-east-1'
@@ -152,7 +158,7 @@ The global static service for reporting metrics:
152158

153159
| Method | Description | Parameters |
154160
|-----------------|-----------------------------|-------------------------------------------------------------|
155-
| `counter()` | Increment a counter metric | `key: string, labels?: Record<string, string \| number>`
161+
| `counter()` | Increment a counter metric | `key: string, labels?: Record<string, string \| number>, value?: number = 1`
156162
| `gauge()` | Set a gauge value | `key: string, value: number, labels?: Record<string, string \| number>`
157163
| `histogram()` | Record a histogram value | `key: string, value: number, labels?: Record<string, string \| number>, buckets?: number[]`
158164
| `summary()` | Record a summary value | `key: string, value: number, labels?: Record<string, string \| number>, percentiles?: number[]`

src/metrics/metrics.service.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ describe( 'MetricsService', () => {
126126
service.incCounter( 'test_counter', { '': 'invalid' } );
127127
} ).toThrow();
128128
} );
129+
130+
it( 'should increment counter with custom value', async () => {
131+
service.incCounter( 'test_counter', {}, 5 );
132+
const metrics = await registry.metrics();
133+
expect( metrics ).toContain( 'test_counter 5' );
134+
} );
135+
136+
it( 'should increment counter with custom value and labels', async () => {
137+
service.incCounter( 'test_counter', { method: 'GET' }, 3 );
138+
const metrics = await registry.metrics();
139+
expect( metrics ).toContain( 'test_counter{method="GET"} 3' );
140+
} );
141+
142+
it( 'should accumulate custom values correctly', async () => {
143+
service.incCounter( 'test_counter', { method: 'GET' }, 3 );
144+
service.incCounter( 'test_counter', { method: 'GET' }, 2 );
145+
const metrics = await registry.metrics();
146+
expect( metrics ).toContain( 'test_counter{method="GET"} 5' );
147+
} );
129148
} );
130149

131150
describe( 'setGauge', () => {

src/metrics/metrics.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class MetricsService {
2525
}
2626
}
2727

28-
public incCounter( key: string, labels?: Record<string, string | number> ): void {
28+
public incCounter( key: string, labels?: Record<string, string | number>, value: number = 1 ): void {
2929
if ( ! this.counter[ key ] ) {
3030
this.counter[ key ] = new Counter( {
3131
name: key,
@@ -34,7 +34,7 @@ export class MetricsService {
3434
registers: [ this.registry ],
3535
} );
3636
}
37-
this.counter[ key ].inc( labels || {} );
37+
this.counter[ key ].inc( labels || {}, value );
3838
}
3939

4040
public setGauge( key: string, value: number, labels?: Record<string, string | number> ): void {

src/reporter/reporter.service.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ describe( 'ReporterService', () => {
7070
const metrics = await registry.metrics();
7171
expect( metrics ).toContain( 'test_counter 1' );
7272
} );
73+
74+
it( 'should increment counter with custom value', async () => {
75+
ReporterService.counter( 'test_counter', undefined, 5 );
76+
const metrics = await registry.metrics();
77+
expect( metrics ).toContain( 'test_counter 5' );
78+
} );
79+
80+
it( 'should increment counter with custom value and labels', async () => {
81+
ReporterService.counter( 'test_counter', { method: 'POST' }, 3 );
82+
const metrics = await registry.metrics();
83+
expect( metrics ).toContain( 'test_counter{method="POST"} 3' );
84+
} );
85+
86+
it( 'should accumulate custom values correctly', async () => {
87+
ReporterService.counter( 'test_counter', { method: 'POST' }, 3 );
88+
ReporterService.counter( 'test_counter', { method: 'POST' }, 2 );
89+
const metrics = await registry.metrics();
90+
expect( metrics ).toContain( 'test_counter{method="POST"} 5' );
91+
} );
7392
} );
7493

7594
describe( 'gauge', () => {

src/reporter/reporter.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export class ReporterService implements OnApplicationBootstrap {
1212
ReporterService.metricsService = this.metrics;
1313
}
1414

15-
static counter( key: string, labels?: Record<string, string | number> ): void {
15+
static counter( key: string, labels?: Record<string, string | number>, value: number = 1 ): void {
1616
try {
17-
ReporterService.metricsService.incCounter( key, labels );
17+
ReporterService.metricsService.incCounter( key, labels, value );
1818
} catch ( error ) {
1919
this.logError( 'increment counter', key, labels, error );
2020
}

0 commit comments

Comments
 (0)