Browse Source

Merge pull request #155 from sutcalag/main

add spent time when accessing milvus
ryjiang 4 years ago
parent
commit
b69cfd03a7

+ 9 - 9
server/src/app.module.ts

@@ -1,4 +1,4 @@
-import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
+import { Module } from '@nestjs/common';
 import { APP_INTERCEPTOR } from '@nestjs/core';
 import { ServeStaticModule } from '@nestjs/serve-static';
 import { AppController } from './app.controller';
@@ -12,7 +12,8 @@ import { AuthModule } from './auth/auth.module';
 import { join } from 'path';
 import { PartitionsModule } from './partitions/partitions.module';
 import { SchemaModule } from './schema/schema.module';
-import { LoggerMiddleware } from './middlewares/logger';
+import { EventsModule } from './events/events.module';
+import { LoggingInterceptor } from './interceptors/index';
 
 @Module({
   imports: [
@@ -26,6 +27,7 @@ import { LoggerMiddleware } from './middlewares/logger';
     AuthModule,
     PartitionsModule,
     SchemaModule,
+    EventsModule,
   ],
   controllers: [AppController],
   providers: [
@@ -39,13 +41,11 @@ import { LoggerMiddleware } from './middlewares/logger';
       provide: APP_INTERCEPTOR,
       useClass: TransformResInterceptor,
     },
+    {
+      provide: APP_INTERCEPTOR,
+      useClass: LoggingInterceptor,
+    },
     UsersService,
   ],
 })
-export class AppModule {
-  configure(consumer: MiddlewareConsumer) {
-    consumer
-      .apply(LoggerMiddleware)
-      .forRoutes({ path: '*', method: RequestMethod.ALL });
-  }
-}
+export class AppModule { }

+ 1 - 1
server/src/collections/collections.controller.ts

@@ -25,7 +25,7 @@ import {
 } from './dto';
 import { cacheKeys } from '../cache/config';
 
-//Including 2 kind of cache check getCollections and getStatistics for detail
+//Including 2 kind of cache contorl, check getCollections and getStatistics for detail
 @ApiTags('collections')
 @Controller('collections')
 export class CollectionsController {

+ 20 - 1
server/src/interceptors/index.ts

@@ -7,7 +7,7 @@ import {
   HttpStatus,
   BadRequestException,
 } from '@nestjs/common';
-import { map, catchError } from 'rxjs/operators';
+import { map, catchError, tap } from 'rxjs/operators';
 import { Observable, throwError } from 'rxjs';
 
 export interface Response<T> {
@@ -50,3 +50,22 @@ export class ErrorInterceptor implements NestInterceptor {
     );
   }
 }
+
+/**
+ * add spent time looger when accessing milvus.
+ */
+@Injectable()
+export class LoggingInterceptor implements NestInterceptor {
+  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
+    const [req = {}, res = {}] = context.getArgs();
+    const { ip = '', method = '', originalUrl = '', headers = {} } = req;
+    const { statusCode = '' } = res;
+    const ua = headers['user-agent'] || '';
+    const now = Date.now();
+    return next
+      .handle()
+      .pipe(
+        tap(() => Logger.log(`${method} ${originalUrl} takes ${Date.now() - now}ms ip:${ip} ua:${ua} status:${statusCode}`)),
+      );
+  }
+}

+ 1 - 1
server/src/partitions/partitions.controller.ts

@@ -20,7 +20,7 @@ import { PartitionsService } from './partitions.service';
 @ApiTags('partitions')
 @Controller('partitions')
 export class PartitionsController {
-  constructor(private partitionsService: PartitionsService) {}
+  constructor(private partitionsService: PartitionsService) { }
 
   @Get()
   @UsePipes(new ValidationPipe())

+ 5 - 6
server/src/schema/schema.controller.ts

@@ -16,11 +16,10 @@ import {
   GetIndexState,
 } from './dto';
 import { SchemaService } from './schema.service';
-
 @ApiTags('schema')
 @Controller('schema')
 export class SchemaController {
-  constructor(private schemaService: SchemaService) {}
+  constructor(private schemaService: SchemaService) { }
 
   @Post('index')
   @UsePipes(new ValidationPipe())
@@ -28,10 +27,10 @@ export class SchemaController {
     const { type, collection_name, extra_params, field_name } = body;
     return type === ManageType.CREATE
       ? await this.schemaService.createIndex({
-          collection_name,
-          extra_params,
-          field_name,
-        })
+        collection_name,
+        extra_params,
+        field_name,
+      })
       : await this.schemaService.dropIndex({ collection_name, field_name });
   }