Skip to content

Commit

Permalink
feat(agent): add debug logs (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreDemailly authored Dec 1, 2023
1 parent 06f2b11 commit abe7861
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 16 deletions.
11 changes: 9 additions & 2 deletions src/agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ await start();

## 🌐 API

### `start(location?: string, logger?: Logger): ToadScheduler`
### `start(location?: string, options?: StartOptions): Promise<ToadScheduler>`

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).

Expand All @@ -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";
}
```

Expand Down
34 changes: 23 additions & 11 deletions src/agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/agent/src/notifiers/agentFailure.notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class AgentFailureNotifier extends Notifier<AgentFailureAlert> {
}
catch (error) {
this.logger.error(`[SELF-MONITORING](notify: error|notifier: ${alert.notifierConfig.notifier}|message: ${error.message})`);
this.logger.debug(error);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/agent/src/notifiers/compositeRules.notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class CompositeRuleNotifier extends Notifier<CompositeRuleAlert> {
}
catch (error) {
this.logger.error(`[${compositeRuleName}](notify: error|notifier: ${notifierConfig.notifier}|message: ${error.message})`);
this.logger.debug(error);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/agent/src/notifiers/rules.notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class RuleNotifier extends Notifier<RuleNotifierAlert> {
.run("failed", alert.notif.alertId);

this.logger.error(`[${rule.name}](notify: error|notifier: ${notifierConfig.notifier}|message: ${error.message})`);
this.logger.debug(error);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/agent/src/tasks/asyncTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down
1 change: 1 addition & 0 deletions src/agent/src/utils/selfMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions src/agent/test/FT/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,7 @@ export class MockLogger {
error(message: string) {
console.log("❗", message);
}
debug(message: string) {
console.log("🐛", message);
}
}

0 comments on commit abe7861

Please sign in to comment.