Skip to content

Commit

Permalink
feat(interceptor): creates set up for http/ws/rpc logging
Browse files Browse the repository at this point in the history
Creates interfaces and services that should handle the logic for the HTTP,
 WS, and RPC logging. This is a broken build and it is expected for CI to f
ail.

re #7 #8 #9
  • Loading branch information
jmcdo29 committed Mar 13, 2020
1 parent a1401eb commit bef7442
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 273 deletions.
16 changes: 16 additions & 0 deletions src/decorators/skip.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { OGMA_INTERCEPTOR_SKIP } from '../ogma.constants';

export function OgmaSkip() {
return (
target: object,
key?: string | symbol,
descriptor?: TypedPropertyDescriptor<any>,
) => {
if (descriptor) {
Reflect.defineMetadata(OGMA_INTERCEPTOR_SKIP, true, descriptor.value);
return descriptor;
}
Reflect.defineMetadata(OGMA_INTERCEPTOR_SKIP, true, target);
return target;
};
}
34 changes: 26 additions & 8 deletions src/interceptor/delegator.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExecutionContext, Injectable } from '@nestjs/common';
import { OgmaInterceptorServiceOptions } from '../interfaces/ogma-options.interface';
import { HttpInterceptorService } from './http-interceptor.service';
import { LogObject } from './interfaces/log.interface';
import { RpcInterceptorService } from './rpc-interceptor.service';
import { WebsocketInterceptorService } from './websocket-interceptor.service';

Expand All @@ -17,60 +18,77 @@ export class DelegatorService {
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object {
): string | LogObject {
let logObject: string | LogObject;
switch (context.getType()) {
case 'http':
return this.httpParser.getSuccessContext(
logObject = this.httpParser.getSuccessContext(
data,
context,
startTime,
options,
);
break;
case 'ws':
return this.wsParser.getSuccessContext(
logObject = this.wsParser.getSuccessContext(
data,
context,
startTime,
options,
);
break;
case 'rpc':
return this.rpcParser.getSuccessContext(
logObject = this.rpcParser.getSuccessContext(
data,
context,
startTime,
options,
);
}
return this.getStringOrObject(logObject, { json: options.json });
}

getContextErrorString(
error: any,
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object {
): string | LogObject {
let logObject: string | LogObject;
switch (context.getType()) {
case 'http':
return this.httpParser.getErrorContext(
logObject = this.httpParser.getErrorContext(
error,
context,
startTime,
options,
);
break;
case 'ws':
return this.wsParser.getErrorContext(
logObject = this.wsParser.getErrorContext(
error,
context,
startTime,
options,
);
break;
case 'rpc':
return this.rpcParser.getErrorContext(
logObject = this.rpcParser.getErrorContext(
error,
context,
startTime,
options,
);
}
return this.getStringOrObject(logObject, { json: options.json });
}

private getStringOrObject(
data: LogObject,
options: { json: boolean },
): string | LogObject {
return options.json
? data
: `${data.callerAddress} - ${data.method} ${data.callPoint} ${data.protocol} ${data.status} ${data.responseTime}ms - ${data.contentLength}`;
}
}
83 changes: 22 additions & 61 deletions src/interceptor/http-interceptor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
} from '../interfaces/fastify-like.interface';
import { OgmaInterceptorServiceOptions } from '../interfaces/ogma-options.interface';
import { OgmaRequest, OgmaResponse } from '../interfaces/ogma-types.interface';
import { InterceptorService } from './interceptor-service.interface';
import { InterceptorService } from './interfaces/interceptor-service.interface';
import { LogObject } from './interfaces/log.interface';

@Injectable()
export class HttpInterceptorService implements InterceptorService {
Expand All @@ -17,19 +18,35 @@ export class HttpInterceptorService implements InterceptorService {
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object {
): LogObject {
this.options = options;
return 'success string';
return {
callerAddress: '127.0.0.1',
method: 'GET',
callPoint: '/',
status: '200',
responseTime: 83,
contentLength: 42,
protocol: 'HTTP/1.1',
};
}

getErrorContext(
error: Error | HttpException,
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object {
): LogObject {
this.options = options;
return 'error string';
return {
callerAddress: '127.0.0.1',
method: 'GET',
callPoint: '/',
status: '500',
responseTime: 83,
contentLength: 42,
protocol: 'HTTP/1.1',
};
}

private getRequest(context: ExecutionContext): OgmaRequest {
Expand All @@ -46,62 +63,6 @@ export class HttpInterceptorService implements InterceptorService {
return context.switchToHttp().getResponse();
}

private devContext(
req: OgmaRequest,
res: OgmaResponse,
data: Buffer,
startTime: number,
): string | object {
const requestTime = Date.now() - startTime + ' ms';
const contentLength = data.byteLength;
const status = this.getStatusCode(res);
const url = this.getUrl(req);
const method = this.getMethod(req);
if (this.options.json) {
return {
method,
url,
status,
'Content-Length': contentLength,
'Response-Time': requestTime,
};
}
return `${method} ${url} ${this.statusCodeColor(
status,
)} ${requestTime} - ${contentLength}`;
}

private prodContext(
req: OgmaRequest,
res: OgmaResponse,
data: Buffer,
startTime: number,
): string | object {
const requestTime = Date.now() - startTime + ' ms';
const contentLength = data.byteLength;
const address = req.ips ?? req.ip;
const status = this.getStatusCode(res);
const url = this.getUrl(req);
const method = this.getMethod(req);
const httpMajor = this.getHttpMajor(req);
const httpMinor = this.getHttpMinor(req);
const httpVersion = `${httpMajor}.${httpMinor}`;
if (this.options.json) {
return {
'Remote-Address': address,
method,
url,
status,
'Content-Length': contentLength,
'Response-Time': requestTime,
httpVersion,
};
}
return `${address} - ${method} ${url} HTTP/${httpVersion} ${this.statusCodeColor(
status,
)} ${requestTime} - ${contentLength}`;
}

private isFastifyRequest(req: OgmaRequest): req is FastifyLikeRequest {
return Object.keys(req).indexOf('raw') !== -1;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ExecutionContext, HttpException } from '@nestjs/common';
import { OgmaInterceptorServiceOptions } from 'src/interfaces/ogma-options.interface';
import { LogObject } from './log.interface';

export interface InterceptorService {
getSuccessContext(
data: any,
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object;
): LogObject;

getErrorContext(
error: Error | HttpException,
context: ExecutionContext,
startTime: number,
options: OgmaInterceptorServiceOptions,
): string | object;
): LogObject;
}
9 changes: 9 additions & 0 deletions src/interceptor/interfaces/log.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface LogObject {
callerAddress: string[] | string;
method: string;
callPoint: string;
protocol: string;
status: string;
responseTime: number;
contentLength: number;
}
Loading

0 comments on commit bef7442

Please sign in to comment.