浏览代码

add spent time when accessing milvus

sutcalag 4 年之前
父节点
当前提交
bed5b17800

+ 2 - 0
server/src/app.module.ts

@@ -12,6 +12,7 @@ import { AuthModule } from './auth/auth.module';
 import { join } from 'path';
 import { join } from 'path';
 import { PartitionsModule } from './partitions/partitions.module';
 import { PartitionsModule } from './partitions/partitions.module';
 import { SchemaModule } from './schema/schema.module';
 import { SchemaModule } from './schema/schema.module';
+import { EventsModule } from './events/events.module';
 import { LoggerMiddleware } from './middlewares/logger';
 import { LoggerMiddleware } from './middlewares/logger';
 
 
 @Module({
 @Module({
@@ -26,6 +27,7 @@ import { LoggerMiddleware } from './middlewares/logger';
     AuthModule,
     AuthModule,
     PartitionsModule,
     PartitionsModule,
     SchemaModule,
     SchemaModule,
+    EventsModule,
   ],
   ],
   controllers: [AppController],
   controllers: [AppController],
   providers: [
   providers: [

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

@@ -24,10 +24,12 @@ import {
   VectorSearch,
   VectorSearch,
 } from './dto';
 } from './dto';
 import { cacheKeys } from '../cache/config';
 import { cacheKeys } from '../cache/config';
+import { LoggingInterceptor } from '../interceptors/index';
 
 
-//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')
 @ApiTags('collections')
 @Controller('collections')
 @Controller('collections')
+@UseInterceptors(LoggingInterceptor)
 export class CollectionsController {
 export class CollectionsController {
   constructor(private collectionsService: CollectionsService, @Inject(CACHE_MANAGER) private cacheManager: Cache) { }
   constructor(private collectionsService: CollectionsService, @Inject(CACHE_MANAGER) private cacheManager: Cache) { }
 
 

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

@@ -7,7 +7,7 @@ import {
   HttpStatus,
   HttpStatus,
   BadRequestException,
   BadRequestException,
 } from '@nestjs/common';
 } from '@nestjs/common';
-import { map, catchError } from 'rxjs/operators';
+import { map, catchError, tap } from 'rxjs/operators';
 import { Observable, throwError } from 'rxjs';
 import { Observable, throwError } from 'rxjs';
 
 
 export interface Response<T> {
 export interface Response<T> {
@@ -50,3 +50,18 @@ 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 now = Date.now();
+    return next
+      .handle()
+      .pipe(
+        tap(() => Logger.log(`request to ${context.getArgs()[0]['url']} takes ${Date.now() - now}ms`)),
+      );
+  }
+}

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

@@ -7,6 +7,7 @@ import {
   Query,
   Query,
   UsePipes,
   UsePipes,
   ValidationPipe,
   ValidationPipe,
+  UseInterceptors,
 } from '@nestjs/common';
 } from '@nestjs/common';
 import { ApiTags } from '@nestjs/swagger';
 import { ApiTags } from '@nestjs/swagger';
 import {
 import {
@@ -16,11 +17,13 @@ import {
   ManageType,
   ManageType,
 } from './dto';
 } from './dto';
 import { PartitionsService } from './partitions.service';
 import { PartitionsService } from './partitions.service';
+import { LoggingInterceptor } from '../interceptors/index';
 
 
 @ApiTags('partitions')
 @ApiTags('partitions')
 @Controller('partitions')
 @Controller('partitions')
+@UseInterceptors(LoggingInterceptor)
 export class PartitionsController {
 export class PartitionsController {
-  constructor(private partitionsService: PartitionsService) {}
+  constructor(private partitionsService: PartitionsService) { }
 
 
   @Get()
   @Get()
   @UsePipes(new ValidationPipe())
   @UsePipes(new ValidationPipe())

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

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