Skip to content
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

fix(agent): self monitoring inaccurate throttle #160

Merged
merged 1 commit into from
Nov 23, 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
16 changes: 8 additions & 8 deletions src/agent/src/notifiers/agentFailure.notifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ export class AgentFailureNotifier extends Notifier<AgentFailureAlert> {
}

async sendNotification(alert: AgentFailureAlert) {
const { notifierConfig } = alert;
const notifierOptions = {
...notifierConfig,
data: await this.#agentFailureAlertData(alert),
template: this.config.selfMonitoring!.template
};

try {
const { notifierConfig } = alert;
const notifierOptions = {
...notifierConfig,
data: await this.#agentFailureAlertData(alert),
template: this.config.selfMonitoring!.template
};

await this.execute(notifierOptions);

this.logger.info(`[SELF-MONITORING](notify: success|notifier: ${notifierConfig.notifier})`);
}
catch (error) {
this.logger.error(`[SELF-MONITORING](notify: error|notifier: ${notifierConfig.notifier}|message: ${error.message})`);
this.logger.error(`[SELF-MONITORING](notify: error|notifier: ${alert.notifierConfig.notifier}|message: ${error.message})`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/agent/src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class Rule {
}

const alerts = getDB().prepare(
"SELECT alertId, key, value FROM alertLabels WHERE key IN (?)"
`SELECT alertId, key, value FROM alertLabels WHERE key IN (${labelScope.map(() => "?").join(",")})`
).all(labelScope) as { alertId: number; key: string; value: string }[];

const alertIds = alerts.flatMap((alert) => {
Expand All @@ -264,7 +264,7 @@ export class Rule {
return 0;
}

return (getDB().prepare("SELECT COUNT(id) as count FROM alerts WHERE id IN (?) AND createdAt >= ?").get(
return (getDB().prepare(`SELECT COUNT(id) as count FROM alerts WHERE id IN (${alertIds.map(() => "?").join(",")}) AND createdAt >= ?`).get(
alertIds,
interval
) as { count: number }).count;
Expand Down
12 changes: 10 additions & 2 deletions src/agent/src/utils/selfMonitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { AgentFailureAlert } from "../notifiers/agentFailure.notifier";
export function getAgentFailureRules(alert: AgentFailureAlert): string {
const ruleIds = new Set(alert.failures.map(({ ruleId }) => ruleId));
const failures = getDB()
.prepare("SELECT name FROM rules WHERE id IN (?)")
.prepare(`SELECT name FROM rules WHERE id IN (${[...ruleIds].map(() => "?").join(",")})`)
.all([...ruleIds]) as { name: string }[];

return failures.map(({ name }) => name).join(", ");
Expand All @@ -31,10 +31,18 @@ function hasAgentFailureThrottle(throttle: SigynInitializedSelfMonitoring["throt
.get(intervalDate) as { count: number });
const agentFailuresAlertCount = agentFailuresAlert?.count ?? 0;

if (agentFailuresAlertCount <= activationThreshold) {
if (activationThreshold > 0 && agentFailuresAlertCount <= activationThreshold) {
return false;
}

if (count > 0 && agentFailuresAlertCount - activationThreshold > count) {
return false;
}

if (activationThreshold > 0 && agentFailuresAlertCount > activationThreshold) {
return true;
}

return agentFailuresAlertCount === 1 ? false : agentFailuresAlertCount - activationThreshold <= count;
}

Expand Down