0

I have implemented an interceptor in my NestJS application to log http request/responses using axios interceptors

https://github.com/axios/axios#interceptors

Is there something similar that I could use to log incoming/outgoing kafka events in my application ?

mh377
  • 1,656
  • 5
  • 22
  • 41

1 Answers1

0

I have managed to log the incoming kafka events but I havent figured out a way to intercept the outgoing events yet.

Below is a sample of my code:

// kafka-logging.interceptor.ts

import { Injectable, ExecutionContext, CallHandler, NestInterceptor, Logger } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class KafkaLoggingInterceptor implements NestInterceptor {
  private logger = new Logger(KafkaLoggingInterceptor.name);

  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const request = context.switchToHttp().getRequest();

    this.logger.verbose('-------------------------------------  KAFKA EVENT ---------------------------------------');
    this.logger.verbose({ request });
    this.logger.verbose('------------------------------------------------------------------------------------------');

    return next.handle();
  }
}

Then I can add the interceptor directly to my consumer so that it doesn't log all incoming REST requests as well.

// my-kafka.consumer.ts

import { KafkaLoggingInterceptor } from './kafka-logging.interceptor';

@Controller()
@UseInterceptors(new KafkaLoggingInterceptor())
export class MyKafkaConsumer {}

If I ever figure out how to log the outgoing events then I will post an update.

mh377
  • 1,656
  • 5
  • 22
  • 41