Skip to content

Commit

Permalink
Merge branch 'master' into redis
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffminsungkim committed May 25, 2020
2 parents b649830 + 3decf04 commit ec844ea
Show file tree
Hide file tree
Showing 61 changed files with 1,906 additions and 753 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ module.exports = {
WeakSet: 'readonly',
Promise: 'readonly',
Reflect: 'readonly',
Symbol: 'readonly',
},
};
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Start Docker Compose
run: docker-compose up -d
- name: install
run: yarn setup
- name: lint
Expand All @@ -34,3 +36,5 @@ jobs:
run: yarn test:cov
- name: integration test
run: yarn test:int
- name: Stop Docker Compose
run: docker-compose down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ coverage/
*.tgz

*.txt
*.gql
*.gql
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
![CI](https://github.com/jmcdo29/ogma/workflows/CI/badge.svg) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![Coffee](https://badgen.net/badge/Buy%20Me/A%20Coffee/purple?icon=kofi)](https://www.buymeacoffee.com/jmcdo29)

</div>
# @ogma

# @Ogma

Ogma is a no-nonsense logger developed to make logging simple, and easy to read in development, while also having a powerful JSON form when it comes to production level logs, to make it easier to parse and consume by external services. This monorepo has all of the code for the base logger, the binary to rehydrate the JSON logs, and the [NestJS Module](https://nestjs.com) along with supported plugins for the module's interceptor.

Expand Down
10 changes: 5 additions & 5 deletions benchmarks/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ Benchmarks were made by testing the logging capabilities of several loggers agai

| Logger | Simple | Long | JSON | Deep |
| --- | --- | --- | --- | --- |
| Bunyan | 358.503331ms | 5150.968539ms | 401.276122ms | 885.27512ms |
| Ogma | 249.590941ms | 1966.489754ms | 284.218876ms | 382.467005ms |
| Pino | 153.721066ms | 5568.779201ms | 172.492747ms | 245.40139ms |
| Winston | 468.264545ms | 5491.972299ms | 354.748487ms | 451.454986ms |
| Bunyan | 375.159229ms | 4928.94154ms | 420.335668ms | 458.811827ms |
| Ogma | 229.933075ms | 2245.650824ms | 352.558238ms | 391.385758ms |
| Pino | 160.423314ms | 6529.866729ms | 163.253851ms | 443.295177ms |
| Winston | 470.131286ms | 5411.124633ms | 986.761038ms | 501.628974ms |

## Information

Benchmarks generated on Linux/linux x64 5.0.0-32-generic ~Intel(R) Core(TM) i3-8130U CPU @ 2.20GHz (cores/threads): 4
Benchmarks generated on Darwin/darwin x64 18.7.0 ~Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz (cores/threads): 4
44 changes: 44 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
version: "3.3"
services:
mqtt:
image: eclipse-mosquitto
container_name: mqtt
ports:
- "1883:1883"
rabbitmq:
image: rabbitmq
container_name: rabbit
ports:
- "5672:5672"
# zookeeper:
# container_name: kafka-zookeeper
# hostname: zookeeper
# image: confluentinc/cp-zookeeper:5.3.2
# ports:
# - "2181:2181"
# environment:
# ZOOKEEPER_CLIENT_PORT: 2181
# ZOOKEEPER_TICK_TIME: 2000
# kafka:
# container_name: kafka
# hostname: kafka
# image: confluentinc/cp-kafka:5.3.2
# depends_on:
# - zookeeper
# ports:
# - "29092:29092"
# - "9092:9092"
# environment:
# KAFKA_BROKER_ID: 1
# KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
# KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
# KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
# KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
# KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
# grpc:
nats:
image: nats
container_name: nats
ports:
- "4222:4222"
# redis:
10 changes: 10 additions & 0 deletions integration/src/grpc/client/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArgumentsHost, Catch, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';

@Catch()
export class ExceptionFilter extends BaseExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const res = host.switchToHttp().getResponse();
res.send(exception);
}
}
36 changes: 36 additions & 0 deletions integration/src/grpc/client/grpc-client.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
Controller,
Get,
Inject,
OnModuleInit,
UseFilters,
} from '@nestjs/common';
import { ClientGrpc } from '@nestjs/microservices';
import { ExceptionFilter } from './exception.filter';
import { HelloService } from './hello-service.interface';

@Controller()
export class GrpcClientController implements OnModuleInit {
private helloService: HelloService;

constructor(@Inject('GRPC_SERVICE') private readonly grpc: ClientGrpc) {}

async onModuleInit() {
this.helloService = this.grpc.getService<HelloService>('HelloService');
}
@Get()
sayHello() {
return this.helloService.sayHello({ ip: '127.0.0.1' });
}

@Get('error')
@UseFilters(ExceptionFilter)
sayError() {
return this.helloService.sayError({ ip: '127.0.0.1' });
}

@Get('skip')
saySkip() {
return this.helloService.saySkip({ ip: '127.0.0.1' });
}
}
21 changes: 21 additions & 0 deletions integration/src/grpc/client/grpc-client.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Module } from '@nestjs/common';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { join } from 'path';
import { GrpcClientController } from './grpc-client.controller';

@Module({
imports: [
ClientsModule.register([
{
transport: Transport.GRPC,
name: 'GRPC_SERVICE',
options: {
package: 'hello',
protoPath: join(__dirname, '..', 'hello/hello.proto'),
},
},
]),
],
controllers: [GrpcClientController],
})
export class GrpcClientModule {}
5 changes: 5 additions & 0 deletions integration/src/grpc/client/hello-service.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface HelloService {
sayHello(data: { ip?: string }): { hello: string };
sayError(data: { ip?: string }): { hello: string };
saySkip(data: { ip?: string }): { hello: string };
}
19 changes: 19 additions & 0 deletions integration/src/grpc/hello/hello.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
syntax="proto3";

package hello;

service HelloService {
rpc SayHello (Ip) returns (Greeting) {}

rpc SayError (Ip) returns (Greeting) {}

rpc SaySkip (Ip) returns (Greeting) {}
}

message Greeting {
string hello = 1;
}

message Ip {
optional string ip = 1;
}
10 changes: 10 additions & 0 deletions integration/src/grpc/server/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Catch, HttpException } from '@nestjs/common';
import { BaseRpcExceptionFilter } from '@nestjs/microservices';
import { Observable, throwError } from 'rxjs';

@Catch()
export class ExceptionFilter extends BaseRpcExceptionFilter {
catch(exception: HttpException): Observable<any> {
return throwError(exception.message);
}
}
27 changes: 27 additions & 0 deletions integration/src/grpc/server/grpc-server.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BadRequestException, Controller, UseFilters } from '@nestjs/common';
import { GrpcMethod } from '@nestjs/microservices';
import { OgmaSkip } from '@ogma/nestjs-module';
import { AppService } from '../../app.service';
import { ExceptionFilter } from './exception.filter';

@Controller()
export class GrpcServerController {
constructor(private readonly service: AppService) {}

@GrpcMethod('HelloService', 'SayHello')
sayHello() {
return this.service.getHello();
}

@GrpcMethod('HelloService', 'SayError')
@UseFilters(ExceptionFilter)
sayError() {
throw new BadRequestException('Borked');
}

@OgmaSkip()
@GrpcMethod('HelloService', 'SaySkip')
saySkip() {
return this.service.getHello();
}
}
9 changes: 9 additions & 0 deletions integration/src/grpc/server/grpc-server.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AppService } from '../../app.service';
import { GrpcServerController } from './grpc-server.controller';

@Module({
controllers: [GrpcServerController],
providers: [AppService],
})
export class GrpcServerModule {}
10 changes: 10 additions & 0 deletions integration/src/kafka/client/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ArgumentsHost, Catch, HttpException } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';

@Catch()
export class ExceptionFilter extends BaseExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const res = host.switchToHttp().getResponse();
res.send(exception);
}
}
41 changes: 41 additions & 0 deletions integration/src/kafka/client/kafka-client.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Controller,
Get,
Inject,
OnModuleDestroy,
OnModuleInit,
UseFilters,
} from '@nestjs/common';
import { ClientKafka } from '@nestjs/microservices';
import { ExceptionFilter } from './exception.filter';

@Controller()
export class KafkaClientController implements OnModuleInit, OnModuleDestroy {
constructor(@Inject('KAFKA_SERVICE') private readonly kafka: ClientKafka) {}

async onModuleInit() {
['hello', 'error', 'skip'].forEach((key) =>
this.kafka.subscribeToResponseOf(`say.${key}`),
);
}

onModuleDestroy() {
this.kafka.close();
}

@Get()
sayHello() {
return this.kafka.send('say.hello', { ip: '127.0.0.1' });
}

@Get('error')
@UseFilters(ExceptionFilter)
sayError() {
return this.kafka.send('say.error', { ip: '127.0.0.1' });
}

@Get('skip')
saySkip() {
return this.kafka.send('say.skip', { ip: '127.0.0.1' });
}
}
25 changes: 25 additions & 0 deletions integration/src/kafka/client/kafka-client.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Module } from '@nestjs/common';
// import { ClientsModule, Transport } from '@nestjs/microservices';
import { KafkaClientController } from './kafka-client.controller';

@Module({
imports: [
/* ClientsModule.register([
{
name: 'KAFKA_SERVICE',
transport: Transport.KAFKA,
options: {
client: {
clientId: 'client',
brokers: ['localhost:9092'],
},
consumer: {
groupId: 'client-consumer',
},
},
},
]), */
],
controllers: [KafkaClientController],
})
export class KafkaClientModule {}
10 changes: 10 additions & 0 deletions integration/src/kafka/server/exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Catch, HttpException } from '@nestjs/common';
import { BaseRpcExceptionFilter } from '@nestjs/microservices';
import { Observable, throwError } from 'rxjs';

@Catch()
export class ExceptionFilter extends BaseRpcExceptionFilter {
catch(exception: HttpException): Observable<any> {
return throwError(exception.message);
}
}
27 changes: 27 additions & 0 deletions integration/src/kafka/server/kafka-server.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { BadRequestException, Controller, UseFilters } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { OgmaSkip } from '@ogma/nestjs-module';
import { AppService } from '../../app.service';
import { ExceptionFilter } from './exception.filter';

@Controller()
export class KafkaServerController {
constructor(private readonly service: AppService) {}

@MessagePattern('say.hello')
sayHello() {
return this.service.getHello();
}

@UseFilters(ExceptionFilter)
@MessagePattern('say.error')
sayError() {
throw new BadRequestException('Borked');
}

@OgmaSkip()
@MessagePattern('say.skip')
saySkip() {
return this.service.getHello();
}
}
9 changes: 9 additions & 0 deletions integration/src/kafka/server/kafka-server.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { AppService } from '../../app.service';
import { KafkaServerController } from './kafka-server.controller';

@Module({
controllers: [KafkaServerController],
providers: [AppService],
})
export class KafkaServerModule {}
6 changes: 3 additions & 3 deletions integration/src/rpc/client/rpc-client.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ export class RpcClientController

@Get()
getMessage() {
return this.micro.send({ cmd: 'message' }, {});
return this.micro.send({ cmd: 'message' }, { ip: '127.0.0.1' });
}

@Get('error')
@UseFilters(ExceptionFilter)
getError() {
return this.micro.send({ cmd: 'error' }, {});
return this.micro.send({ cmd: 'error' }, { ip: '127.0.0.1' });
}

@Get('skip')
getSkip() {
return this.micro.send({ cmd: 'skip' }, {});
return this.micro.send({ cmd: 'skip' }, { ip: '127.0.0.1' });
}
}
Loading

0 comments on commit ec844ea

Please sign in to comment.