From abe7861691fdd1aa37c97e96215e339b9cb2f5f1 Mon Sep 17 00:00:00 2001 From: PierreDemailly <39910767+PierreDemailly@users.noreply.github.com> Date: Fri, 1 Dec 2023 20:30:24 +0100 Subject: [PATCH] feat(agent): add debug logs (#166) --- src/agent/README.md | 11 ++++-- src/agent/src/index.ts | 34 +++++++++++++------ .../src/notifiers/agentFailure.notifier.ts | 1 + .../src/notifiers/compositeRules.notifier.ts | 1 + src/agent/src/notifiers/rules.notifier.ts | 1 + src/agent/src/tasks/asyncTask.ts | 7 ++-- src/agent/src/utils/selfMonitoring.ts | 1 + src/agent/test/FT/helpers.ts | 3 ++ 8 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/agent/README.md b/src/agent/README.md index 841901d..de29f3a 100644 --- a/src/agent/README.md +++ b/src/agent/README.md @@ -70,12 +70,13 @@ await start(); ## 🌐 API -### `start(location?: string, logger?: Logger): ToadScheduler` +### `start(location?: string, options?: StartOptions): Promise` Run Sigyn agent. It will fetch logs depending your rules `polling` and send alerts when `count` threshold is reached. - `location: string` Optional, default to `process.cwd()`. The path to your SQLite database, it will create the file if it doesn't exists but the directory **must** exists. -- `logger: Logger` Optional, default to `pino`. You can use your own logger which must be an object with theses 2 methods: `info` & `error`. +- `options.logger: Logger` Optional, default to `pino`. You can use your own logger which must be an object with theses 3 methods: `debug`, `info` & `error`. +- `options.level` Optional, only works if no logger given. Set log level: `"info" | "debug" | "error"`. The returned scheduler instance allow you to put some extra logic if needed, see [API for scheduler](https://github.com/kibertoad/toad-scheduler/blob/main/README.md#api-for-scheduler). @@ -85,6 +86,12 @@ The returned scheduler instance allow you to put some extra logic if needed, see interface Logger { info: (message: string) => void; error: (message: string) => void; + debug: (message: string) => void; +} + +interface StartOptions { + logger?: Logger; + level?: "info" | "debug" | "error"; } ``` diff --git a/src/agent/src/index.ts b/src/agent/src/index.ts index 7c416f4..aaf1414 100644 --- a/src/agent/src/index.ts +++ b/src/agent/src/index.ts @@ -17,24 +17,36 @@ import * as utils from "./utils/index"; // CONSTANTS const kScheduler = new ToadScheduler(); -const kLogger = pino({ - level: "info", - transport: { - target: "pino-pretty" - } -}); export interface Logger { info: (message: string) => void; error: (message: string) => void; + debug: (message: string) => void; +} + +export interface StartOptions { + logger?: Logger; + level?: "info" | "debug" | "error"; +} + +function defaultLogger(level: StartOptions["level"]) { + return pino({ + level, + transport: { + target: "pino-pretty" + } + }); } export async function start( location = process.cwd(), - logger: Logger = kLogger + options: StartOptions = {} ) { - kLogger.info(`Starting sigyn agent at '${location}'`); - initDB(kLogger); + const { logger, level = "info" } = options; + const agentLogger = logger ?? defaultLogger(level); + + agentLogger.info(`Starting sigyn agent at '${location}'`); + initDB(agentLogger); const { rules, loki } = await initConfig( path.join(location, "/sigyn.config.json") @@ -49,10 +61,10 @@ export async function start( continue; } - const rule = new Rule(ruleConfig, { logger }); + const rule = new Rule(ruleConfig, { logger: agentLogger }); rule.init(); - const task = asyncTask(ruleConfig, { rule, logger, lokiApi }); + const task = asyncTask(ruleConfig, { rule, logger: agentLogger, lokiApi }); const rulePollings = utils.rules.getPollings(ruleConfig.polling); for (const [isCron, polling] of rulePollings) { diff --git a/src/agent/src/notifiers/agentFailure.notifier.ts b/src/agent/src/notifiers/agentFailure.notifier.ts index 08149b3..a11689b 100644 --- a/src/agent/src/notifiers/agentFailure.notifier.ts +++ b/src/agent/src/notifiers/agentFailure.notifier.ts @@ -51,6 +51,7 @@ export class AgentFailureNotifier extends Notifier { } catch (error) { this.logger.error(`[SELF-MONITORING](notify: error|notifier: ${alert.notifierConfig.notifier}|message: ${error.message})`); + this.logger.debug(error); } } diff --git a/src/agent/src/notifiers/compositeRules.notifier.ts b/src/agent/src/notifiers/compositeRules.notifier.ts index c5d4e33..abef495 100644 --- a/src/agent/src/notifiers/compositeRules.notifier.ts +++ b/src/agent/src/notifiers/compositeRules.notifier.ts @@ -47,6 +47,7 @@ export class CompositeRuleNotifier extends Notifier { } catch (error) { this.logger.error(`[${compositeRuleName}](notify: error|notifier: ${notifierConfig.notifier}|message: ${error.message})`); + this.logger.debug(error); } } diff --git a/src/agent/src/notifiers/rules.notifier.ts b/src/agent/src/notifiers/rules.notifier.ts index 41b6ea5..53073d4 100644 --- a/src/agent/src/notifiers/rules.notifier.ts +++ b/src/agent/src/notifiers/rules.notifier.ts @@ -91,6 +91,7 @@ export class RuleNotifier extends Notifier { .run("failed", alert.notif.alertId); this.logger.error(`[${rule.name}](notify: error|notifier: ${notifierConfig.notifier}|message: ${error.message})`); + this.logger.debug(error); } } diff --git a/src/agent/src/tasks/asyncTask.ts b/src/agent/src/tasks/asyncTask.ts index c208440..6d2248d 100644 --- a/src/agent/src/tasks/asyncTask.ts +++ b/src/agent/src/tasks/asyncTask.ts @@ -36,10 +36,11 @@ export function asyncTask(ruleConfig: SigynInitializedRule, options: AsyncTaskOp rule.clearLabels(); } } - catch (e) { - logger.error(`[${ruleConfig.name}](error: ${e.message})`); + catch (error) { + logger.error(`[${ruleConfig.name}](error: ${error.message})`); + logger.debug(error); - handleAgentFailure(e.message, rule, logger); + handleAgentFailure(error.message, rule, logger); } }); diff --git a/src/agent/src/utils/selfMonitoring.ts b/src/agent/src/utils/selfMonitoring.ts index c1eb152..6fac880 100644 --- a/src/agent/src/utils/selfMonitoring.ts +++ b/src/agent/src/utils/selfMonitoring.ts @@ -100,5 +100,6 @@ export function handleAgentFailure(errorMessage: string, rule: Rule, logger: Log } catch (error) { logger.error(`[SELF MONITORING](error: ${error.message})`); + logger.debug(error); } } diff --git a/src/agent/test/FT/helpers.ts b/src/agent/test/FT/helpers.ts index 41e92a9..bee53cf 100644 --- a/src/agent/test/FT/helpers.ts +++ b/src/agent/test/FT/helpers.ts @@ -58,4 +58,7 @@ export class MockLogger { error(message: string) { console.log("❗", message); } + debug(message: string) { + console.log("🐛", message); + } }