Skip to content

feat: Implement logger #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"plugin:unicorn/recommended"
],
"rules": {
"no-console": "error",
"@typescript-eslint/no-unused-vars": 0
},
"overrides": [
Expand Down
248 changes: 244 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@apache-arrow/esnext-esm": "^12.0.1",
"@cloudquery/plugin-pb-javascript": "^0.0.6",
"boolean": "^3.2.0",
"winston": "^3.10.0",
"yargs": "^17.7.2"
}
}
8 changes: 7 additions & 1 deletion src/grpc/discovery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import winston from 'winston';
import grpc = require('@grpc/grpc-js');
import { discovery1 } from '@cloudquery/plugin-pb-javascript';

const SUPPORTED_VERSIONS = [3];

export class DiscoveryServer extends discovery1.cloudquery.discovery.v1.UnimplementedDiscoveryService {
GetVersions(
call: grpc.ServerUnaryCall<
Expand All @@ -9,6 +12,9 @@ export class DiscoveryServer extends discovery1.cloudquery.discovery.v1.Unimplem
>,
callback: grpc.sendUnaryData<discovery1.cloudquery.discovery.v1.GetVersions.Response>,
): void {
return callback(null, new discovery1.cloudquery.discovery.v1.GetVersions.Response({ versions: [3] }));
return callback(
null,
new discovery1.cloudquery.discovery.v1.GetVersions.Response({ versions: SUPPORTED_VERSIONS }),
);
}
}
1 change: 1 addition & 0 deletions src/grpc/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import winston from 'winston';
import grpc = require('@grpc/grpc-js');
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';

Expand Down
9 changes: 7 additions & 2 deletions src/grpc/server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import winston from 'winston';
import grpc = require('@grpc/grpc-js');
import { pluginV3 } from '@cloudquery/plugin-pb-javascript';
import { discovery1 } from '@cloudquery/plugin-pb-javascript';
Expand All @@ -12,10 +13,14 @@ export const getServer = () => {
return server;
};

export const startServer = (address: string) => {
export const startServer = (logger: winston.Logger, address: string) => {
const server = getServer();
server.bindAsync(address, grpc.ServerCredentials.createInsecure(), (error, port) => {
if (error) {
logger.error(error);
return;
}
server.start();
console.log('server running on port', port);
logger.info('server running on port', port);
});
};
26 changes: 26 additions & 0 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import winston from 'winston';

export enum LogLevel {
TRACE = 'trace',
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
}

export enum LogFormat {
JSON = 'json',
TEXT = 'text',
}

export const createLogger = (level: LogLevel, format: LogFormat) => {
// Winston doesn't have a TRACE level, so we need to normalize it to DEBUG.
const normalizedLevel = level === LogLevel.TRACE ? LogLevel.DEBUG : level;
const logger = winston.createLogger({
level: normalizedLevel,
format: format == LogFormat.JSON ? winston.format.json() : winston.format.simple(),
transports: [new winston.transports.Console()],
});

return logger;
};
17 changes: 9 additions & 8 deletions src/serve/serve.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { startServer } from '../grpc/server.js';
import { LogFormat, LogLevel, createLogger } from '../logger/logger.js';

const NETWORK_CHOICES = ['tcp', 'tcp4', 'tcp6', 'unix', 'unixpacket'] as const;
const LOG_LEVEL_CHOICES = ['trace', 'debug', 'info', 'warn', 'error'] as const;
const LOG_FORMAT_CHOICES = ['json', 'text'] as const;

const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const;

export type ServeArguments = {
address: string;
network: (typeof NETWORK_CHOICES)[number];
logLevel: (typeof LOG_LEVEL_CHOICES)[number];
logFormat: (typeof LOG_FORMAT_CHOICES)[number];
logLevel: LogLevel;
logFormat: LogFormat;
sentry: boolean;
otelEndpoint: string;
otelEndpointInsecure: boolean;
Expand All @@ -24,8 +24,9 @@ export const serve = yargs(hideBin(process.argv))
'start plugin gRPC server',
() => {},
({ address, network, logLevel, logFormat, sentry: sentry, otelEndpoint, telemetryLevel }: ServeArguments) => {
console.log({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel });
startServer(address);
const logger = createLogger(logLevel, logFormat);
logger.info(JSON.stringify({ address, network, logLevel, logFormat, sentry, otelEndpoint, telemetryLevel }));
startServer(logger, address);
},
)
.options({
Expand All @@ -45,14 +46,14 @@ export const serve = yargs(hideBin(process.argv))
'log-level': {
alias: 'l',
type: 'string',
choices: LOG_LEVEL_CHOICES,
choices: Object.values(LogLevel),
description: 'log level',
default: 'info',
},
'log-format': {
alias: 'f',
type: 'string',
choices: LOG_FORMAT_CHOICES,
choices: Object.values(LogFormat),
description: 'log format',
default: 'text',
},
Expand Down