Skip to content

Commit

Permalink
context class for onRequest callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghel committed Nov 12, 2024
1 parent 386b6f5 commit e3554cb
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/monitoring/src/interceptors/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './logging.interceptor.context';
export * from './request.cpu.time.interceptor.context';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExecutionContext } from "@nestjs/common";

export class LoggingInterceptorContext {
apiFunction: string = '';
durationMs: number = 0;
origin: string = '';
statusCode: number = 0;
context!: ExecutionContext;

constructor(init?: Partial<LoggingInterceptorContext>) {
Object.assign(this, init);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ExecutionContext } from '@nestjs/common';

export class RequestCpuTimeInterceptorContext {
apiFunction: string = '';
durationMs: number = 0;
context!: ExecutionContext;

constructor(init?: Partial<RequestCpuTimeInterceptorContext>) {
Object.assign(this, init);
}
}
1 change: 1 addition & 0 deletions packages/monitoring/src/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './entities';
export * from './log.requests.interceptor';
export * from './logging.interceptor';
export * from './request.cpu.time.interceptor';
19 changes: 16 additions & 3 deletions packages/monitoring/src/interceptors/logging.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Observable, throwError } from "rxjs";
import { catchError, tap } from "rxjs/operators";
import { MetricsService } from "../metrics";
import { PerformanceProfiler } from "../profilers/performance.profiler";
import { LoggingInterceptorContext } from "./entities/logging.interceptor.context";

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
constructor(
private readonly metricsService: MetricsService,
private readonly onRequest?: (apiFunction: string, durationMs: number, context: ExecutionContext) => void,
private readonly onRequest?: (context: LoggingInterceptorContext) => void,
) { }

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
Expand Down Expand Up @@ -41,7 +42,13 @@ export class LoggingInterceptor implements NestInterceptor {
this.metricsService.setApiCall(apiFunction, origin, res.statusCode, profiler.duration);

if (this.onRequest) {
this.onRequest(apiFunction, profiler.duration, context);
this.onRequest(new LoggingInterceptorContext({
apiFunction,
durationMs: profiler.duration,
origin,
statusCode: res.statusCode,
context,
}));
}
}),
catchError(err => {
Expand All @@ -51,7 +58,13 @@ export class LoggingInterceptor implements NestInterceptor {
this.metricsService.setApiCall(apiFunction, origin, statusCode, profiler.duration);

if (this.onRequest) {
this.onRequest(apiFunction, profiler.duration, context);
this.onRequest(new LoggingInterceptorContext({
apiFunction,
durationMs: profiler.duration,
origin,
statusCode,
context,
}));
}

return throwError(() => err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Observable, throwError } from "rxjs";
import { catchError, tap } from 'rxjs/operators';
import { MetricsService } from '../metrics';
import { CpuProfiler } from "../profilers/cpu.profiler";
import { RequestCpuTimeInterceptorContext } from "./entities";

@Injectable()
export class RequestCpuTimeInterceptor implements NestInterceptor {
constructor(
private readonly metricsService: MetricsService,
private readonly onRequest?: (apiFunction: string, durationMs: number, context: ExecutionContext) => void,
private readonly onRequest?: (context: RequestCpuTimeInterceptorContext) => void,
) { }

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
Expand All @@ -31,7 +32,11 @@ export class RequestCpuTimeInterceptor implements NestInterceptor {
this.metricsService.setApiCpuTime(apiFunction, duration);

if (this.onRequest) {
this.onRequest(apiFunction, duration, context);
this.onRequest(new RequestCpuTimeInterceptorContext({
apiFunction,
durationMs: duration,
context,
}));
}

if (!request.res.headersSent) {
Expand All @@ -43,7 +48,11 @@ export class RequestCpuTimeInterceptor implements NestInterceptor {
this.metricsService.setApiCpuTime(apiFunction, duration);

if (this.onRequest) {
this.onRequest(apiFunction, duration, context);
this.onRequest(new RequestCpuTimeInterceptorContext({
apiFunction,
durationMs: duration,
context,
}));
}

if (!request.res.headersSent) {
Expand Down

0 comments on commit e3554cb

Please sign in to comment.