Skip to content

Commit

Permalink
✨ added reaction support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohland committed Nov 8, 2023
1 parent 0c14be7 commit 1d121be
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "barky",
"version": "1.0.50",
"version": "1.0.51",
"description": "A simple cloud services watchdog with digest notification support & no external dependencies",
"homepage": "https://github.com/Rohland/barky#readme",
"main": "dist/cli.js",
Expand Down
38 changes: 27 additions & 11 deletions src/digest/alerter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe("alerter", () => {
await executeAlerts(config, context);

// assert
expect(console.log).toHaveBeenCalledWith(expect.stringMatching("🔕 Alerts muted at"));
expect(console.log).toHaveBeenCalledWith(expect.stringMatching("🔕 Outage muted at"));
});
});
});
Expand Down Expand Up @@ -336,9 +336,9 @@ describe("alerter", () => {
const alerts = await getAlerts();
expect(alerts.length).toEqual(0);
});
describe("but if some muted muted", () => {
describe("if none left", () => {
it("should send muted notification", async () => {
describe("but if some muted", () => {
describe("if no active alerts left", () => {
it("should send resolved notification (as it resolved and was previously tracked)", async () => {
// arrange
const config = new DigestConfiguration({
"mute-windows": [
Expand Down Expand Up @@ -367,27 +367,43 @@ describe("alerter", () => {
await executeAlerts(config, context);

// assert
expect(console.log).toHaveBeenCalledWith(expect.stringContaining("🔕 Alerts muted at "));
expect(console.log).toHaveBeenCalledWith(expect.stringContaining("Outage ended at "));
const alerts = await getAlerts();
expect(alerts.length).toEqual(0);
});
});
describe("if some left", () => {
it("should send notification", async () => {
describe("if some muted and some active and resolved", () => {
it("should send resolved notification", async () => {
// arrange
const config = new DigestConfiguration({});
const config = new DigestConfiguration({
"mute-windows": [
{
match: "web",
time: "00:00-24:00",
}
]
});
// @ts-ignore
const previousSnapshot = new Snapshot({
const previousWebSnapshot = new Snapshot({
type: "web",
label: "health",
identifier: "www.codeo.co.za",
});
const context = new DigestContext([previousSnapshot], []);
// @ts-ignore
const previousMysqlSnapshot = new Snapshot({
type: "mysql",
label: "health",
identifier: "query",
});
const context = new DigestContext([previousWebSnapshot, previousMysqlSnapshot], []);
const alert = new AlertState({
channel: "console",
start_date: new Date(new Date().getTime() - 1000 * 60 * 2),
last_alert_date: new Date(new Date().getTime() - 1000 * 60 * 2),
affected: JSON.stringify([["web|health|www.codeo.co.za", previousSnapshot]])
affected: JSON.stringify([
["web|health|www.codeo.co.za", previousWebSnapshot],
["mysql|health|query", previousMysqlSnapshot]
])
});
await persistAlerts([alert]);

Expand Down
4 changes: 0 additions & 4 deletions src/digest/alerter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,8 @@ async function sendResolvedAlerts(
alerts: AlertState[],
config: DigestConfiguration) {
await Promise.all(alerts.map(async alert => {
alert.removeMuted(config.muteWindows);
alert.resolve();
if (alert.size === 0) {
if (alert.isMuted) {
await sendMutedAlert(config, alert);
}
return;
}
const channel = config.getChannelConfig(alert.channel);
Expand Down
2 changes: 1 addition & 1 deletion src/evaluators/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function evaluateResult(
if (status != expectedStatus) {
return {
success: false,
msg: `Expected status:${expectedStatus},received ${status}`
msg: `Expected status:${expectedStatus}, got ${status}`
};
}

Expand Down
8 changes: 5 additions & 3 deletions src/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sleepMs } from "./lib/sleep";
import { startClock, stopClock } from "./lib/profiler";
import { canLockProcessFor } from "./lib/process-lock";


export const LoopMs = 30000;

export async function loop(
Expand All @@ -15,7 +16,7 @@ export async function loop(
return -1;
}
while(true) {
const result = await doLoop(runner);
const result = await doLoop(args, runner);
if (result < 0) {
return result;
}
Expand All @@ -30,14 +31,15 @@ function validateNoOtherInstancesRunningFor(args) {
return canLockProcessFor(key);
}

async function doLoop(runner: () => Promise<number>): Promise<number> {
async function doLoop(args, runner: () => Promise<number>): Promise<number> {
const clock = startClock();
const result = await runner();
const ms = stopClock(clock);
if (result < 0) {
return result;
}
const timeToSleep = LoopMs - ms;
const interval = args.debug ? 5000: LoopMs;
const timeToSleep = interval - ms;
await sleepMs(timeToSleep < 0 ? 0 : timeToSleep);
return result;
}
2 changes: 1 addition & 1 deletion src/models/channels/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class ConsoleChannelConfig extends ChannelConfig {
}

sendMutedAlert(alert: AlertState): Promise<void> {
const message = `${ this.prefix } 🔕 Alerts muted at ${ alert.endTime }. ${ this.postfix }`.trim();
const message = `${ this.prefix } 🔕 Outage muted at ${ alert.endTime }. ${ this.postfix }`.trim();
console.log(message);
return Promise.resolve();
}
Expand Down
6 changes: 3 additions & 3 deletions src/models/channels/slack.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("slack", () => {
label: "health",
identifier: "www.codeo.co.za",
success: false,
last_result: "Expected 200, received 500",
last_result: "Expected 200, got 500",
alert_config: {
links: [
{
Expand Down Expand Up @@ -58,7 +58,7 @@ describe("slack", () => {
label: "health",
identifier: "www.codeo.co.za",
success: false,
last_result: "Expected 200, received 500",
last_result: "Expected 200, got 500",
alert_config: {
links: [
{
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("slack", () => {
label: "health",
identifier: "www.codeo.co.za",
success: false,
last_result: "Expected 200, received 500",
last_result: "Expected 200, got 500",
alert_config: {
links: [
{
Expand Down
34 changes: 32 additions & 2 deletions src/models/channels/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ export class SlackChannelConfig extends ChannelConfig {
`✅ <!channel> Previous outage resolved at ${ alert.endTime }. Duration was ${ alert.durationHuman }.\n_See above for more details about affected services._`,
alert.state,
true
)
),
this.reactToSlackMessage(alert.state, "white_check_mark")
]);
}

Expand All @@ -161,7 +162,8 @@ export class SlackChannelConfig extends ChannelConfig {
`🔕 <!channel> Affected alerts were muted at ${ alert.endTime }.\n_See above for more details about affected services._`,
alert.state,
true
)
),
this.reactToSlackMessage(alert.state, "no_bell")
]);
}

Expand Down Expand Up @@ -210,4 +212,32 @@ export class SlackChannelConfig extends ChannelConfig {
throw new Error(msg);
}
}

private async reactToSlackMessage(state: any, reaction: string) {
if (!state) {
return;
}
const body = {
name: reaction,
channel: state?.channel ?? this.channel,
timestamp: state.ts
};
const config = {
method: 'post',
url: "https://slack.com/api/reactions.add",
headers: {
'Authorization': `Bearer ${ this.token }`,
'Content-type': 'application/json;charset=utf-8',
'Accept': '*/*',
},
data: JSON.stringify(body)
};
try {
const result = await axios.request(config);
console.log(result);
} catch (err) {
const msg = `Error posting to Slack: ${ err.message }`;
log(msg, err);
}
}
}

0 comments on commit 1d121be

Please sign in to comment.