Skip to content

Commit 78569b0

Browse files
committed
feat: support app interceptors
1 parent 309b358 commit 78569b0

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ import { ReporterModule } from 'nestjs-metrics-client';
9393
app: 'my-app',
9494
environment: 'production',
9595
},
96+
97+
// Optional: Configure interceptors for custom metrics
98+
interceptors: [ SomeInterceptor ],
99+
96100
// Optional: Configure Pushgateway for batch job metrics
97101
pushgatewayUrl: 'http://pushgateway:9091',
98102
pushgatewayOptions: {
@@ -164,6 +168,7 @@ The global static service for reporting metrics:
164168
| `defaultLabels` | `Record<string, string>` | `{}` | Labels automatically added to all metrics |
165169
| `pushgatewayUrl` | `string` | `undefined` | URL of the Pushgateway server |
166170
| `pushgatewayOptions` | `PushgatewayOptions` | `{}` | Additional options for Pushgateway requests |
171+
| `interceptors` | `Type<any>[]` | `[]` | Interceptors for custom metrics reporting |
167172

168173
#### `ReporterModule.forRootAsync(options)`
169174

src/interfaces.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import { Type } from '@nestjs/common';
2+
13
export interface MetricsConfig {
24
defaultLabels?: Record<string, string>;
35
defaultMetricsEnabled?: boolean;
6+
interceptors?: Type<any>[]
47
pushgatewayUrl?: string;
58
pushgatewayOptions?: {
69
timeout?: number;

src/reporter/reporter.module.ts

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,76 @@
1-
import { DynamicModule, Global, Module } from '@nestjs/common';
1+
import { DynamicModule, Global, Module, Provider, Type } from '@nestjs/common';
22
import { ReporterService } from './reporter.service';
33
import { collectDefaultMetrics, Registry } from 'prom-client';
44
import { MetricsConfig, ReporterAsyncOptions } from '../interfaces';
55
import { MetricsService } from '../metrics/metrics.service';
66
import { MetricsController } from '../metrics/metrics.controller';
77
import { CONFIG_OPTIONS } from '../constants';
8+
import { APP_INTERCEPTOR } from '@nestjs/core';
89

910
@Global()
1011
@Module( {} )
1112
export class ReporterModule {
1213
static forRoot( config: MetricsConfig = {} ): DynamicModule {
1314
const registry: Registry = this.configureRegistry( config );
14-
15+
const providers: Provider[] = [
16+
{
17+
provide: Registry,
18+
useValue: registry
19+
},
20+
{
21+
provide: CONFIG_OPTIONS,
22+
useValue: config
23+
},
24+
MetricsService,
25+
ReporterService
26+
];
27+
28+
if ( config.interceptors ) {
29+
providers.push( ...config.interceptors.map( interceptor => ( {
30+
provide: APP_INTERCEPTOR,
31+
useClass: interceptor as Type<any>,
32+
} ) ) );
33+
}
1534
return {
1635
module: ReporterModule,
17-
providers: [
18-
{
19-
provide: Registry,
20-
useValue: registry
21-
},
22-
{
23-
provide: CONFIG_OPTIONS,
24-
useValue: config
25-
},
26-
MetricsService,
27-
ReporterService
28-
],
36+
providers,
2937
controllers: [ MetricsController ],
3038
exports: [ ReporterService ]
3139
};
3240
}
3341

34-
static forRootAsync( options: ReporterAsyncOptions ): DynamicModule {
42+
static async forRootAsync( options: ReporterAsyncOptions ): Promise<DynamicModule> {
43+
const providers: Provider[] = [
44+
{
45+
provide: CONFIG_OPTIONS,
46+
useFactory: options.useFactory,
47+
inject: options.inject,
48+
},
49+
{
50+
provide: Registry,
51+
useFactory: async ( config: MetricsConfig ) => {
52+
return ReporterModule.configureRegistry( config );
53+
},
54+
inject: [CONFIG_OPTIONS],
55+
},
56+
MetricsService,
57+
ReporterService
58+
];
59+
60+
const asyncConfig = await options.useFactory( ...( options.inject || [] ) );
61+
if ( asyncConfig.interceptors ) {
62+
providers.push( ...asyncConfig.interceptors.map( interceptor => ( {
63+
provide: APP_INTERCEPTOR,
64+
useClass: interceptor as Type<any>,
65+
} ) ) );
66+
}
67+
3568
return {
3669
module: ReporterModule,
3770
imports: options.imports,
38-
providers: [
39-
{
40-
provide: CONFIG_OPTIONS,
41-
useFactory: options.useFactory,
42-
inject: options.inject,
43-
},
44-
{
45-
provide: Registry,
46-
useFactory: async ( config: MetricsConfig ) => {
47-
return ReporterModule.configureRegistry( config );
48-
},
49-
inject: [ CONFIG_OPTIONS ],
50-
},
51-
MetricsService,
52-
ReporterService
53-
],
54-
controllers: [ MetricsController ],
55-
exports: [ ReporterService ]
71+
providers,
72+
controllers: [MetricsController],
73+
exports: [ReporterService]
5674
};
5775
}
5876

0 commit comments

Comments
 (0)