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

feat: Acknowledge and Silence Alerts #339

Merged
merged 1 commit into from
Mar 17, 2024
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 docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ services:
CLICKHOUSE_LOG_LEVEL: ${HYPERDX_LOG_LEVEL}
CLICKHOUSE_PASSWORD: worker
CLICKHOUSE_USER: worker
EXPRESS_SESSION_SECRET: 'hyperdx is cool 👋'
FRONTEND_URL: 'http://localhost:8080' # need to be localhost (CORS)
HDX_NODE_ADVANCED_NETWORK_CAPTURE: 1
HDX_NODE_BETA_MODE: 0
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ services:
CLICKHOUSE_LOG_LEVEL: ${HYPERDX_LOG_LEVEL}
CLICKHOUSE_PASSWORD: worker
CLICKHOUSE_USER: worker
EXPRESS_SESSION_SECRET: 'hyperdx is cool 👋'
FRONTEND_URL: ${HYPERDX_APP_URL}:${HYPERDX_APP_PORT} # need to be localhost (CORS)
HDX_NODE_ADVANCED_NETWORK_CAPTURE: 1
HDX_NODE_BETA_MODE: 0
Expand Down
84 changes: 81 additions & 3 deletions packages/api/src/controllers/alerts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getHours, getMinutes } from 'date-fns';
import { sign, verify } from 'jsonwebtoken';
import ms from 'ms';
import { z } from 'zod';

Expand All @@ -14,6 +15,8 @@ import Alert, {
} from '@/models/alert';
import Dashboard, { IDashboard } from '@/models/dashboard';
import LogView, { ILogView } from '@/models/logView';
import { IUser } from '@/models/user';
import logger from '@/utils/logger';
import { alertSchema } from '@/utils/zod';

export type AlertInput = {
Expand All @@ -34,6 +37,13 @@ export type AlertInput = {
// Chart alerts
dashboardId?: string;
chartId?: string;

// Silenced
silenced?: {
by?: ObjectId;
at: Date;
until: Date;
};
};

const getCron = (interval: AlertInterval) => {
Expand Down Expand Up @@ -131,7 +141,7 @@ export const createAlert = async (
}).save();
};

const dashboardLogViewIds = async (teamId: ObjectId) => {
const dashboardLogViewIds = async (teamId: ObjectId | string) => {
const [dashboards, logViews] = await Promise.all([
Dashboard.find({ team: teamId }, { _id: 1 }),
LogView.find({ team: teamId }, { _id: 1 }),
Expand Down Expand Up @@ -194,7 +204,10 @@ export const getAlerts = async (teamId: ObjectId) => {
});
};

export const getAlertById = async (alertId: string, teamId: ObjectId) => {
export const getAlertById = async (
alertId: ObjectId | string,
teamId: ObjectId | string,
) => {
const [logViewIds, dashboardIds] = await dashboardLogViewIds(teamId);

return Alert.findOne({
Expand Down Expand Up @@ -233,7 +246,10 @@ export const getAlertsWithLogViewAndDashboard = async (teamId: ObjectId) => {
}).populate<{
logView: ILogView;
dashboardId: IDashboard;
}>(['logView', 'dashboardId']);
silenced?: IAlert['silenced'] & {
by: IUser;
};
}>(['logView', 'dashboardId', 'silenced.by']);
};

export const deleteAlert = async (id: string, teamId: ObjectId) => {
Expand All @@ -255,3 +271,65 @@ export const deleteAlert = async (id: string, teamId: ObjectId) => {
],
});
};

export const generateAlertSilenceToken = async (
alertId: ObjectId | string,
teamId: ObjectId | string,
) => {
const secret = process.env.EXPRESS_SESSION_SECRET;

if (!secret) {
logger.error(
'EXPRESS_SESSION_SECRET is not set for signing token, skipping alert silence JWT generation',
);
return '';
}

const alert = await getAlertById(alertId, teamId);
if (alert == null) {
throw new Error('Alert not found');
}

const token = sign(
{ alertId: alert._id.toString(), teamId: teamId.toString() },
secret,
{ expiresIn: '1h' },
);

// Slack does not accept ids longer than 255 characters
if (token.length > 255) {
logger.error(
'Alert silence JWT length is greater than 255 characters, this may cause issues with some clients.',
);
}

return token;
};

export const silenceAlertByToken = async (token: string) => {
const secret = process.env.EXPRESS_SESSION_SECRET;

if (!secret) {
throw new Error('EXPRESS_SESSION_SECRET is not set for verifying token');
}

const decoded = verify(token, secret, {
algorithms: ['HS256'],
}) as { alertId: string; teamId: string };

if (!decoded?.alertId || !decoded?.teamId) {
throw new Error('Invalid token');
}

const alert = await getAlertById(decoded.alertId, decoded.teamId);
if (alert == null) {
throw new Error('Alert not found');
}

alert.silenced = {
at: new Date(),
until: new Date(Date.now() + ms('30m')),
};

return alert.save();
};
26 changes: 26 additions & 0 deletions packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ export interface IAlert {
// Chart alerts
dashboardId?: ObjectId;
chartId?: string;

// Silenced
silenced?: {
by?: ObjectId;
at: Date;
until: Date;
};
}

export type AlertDocument = mongoose.HydratedDocument<IAlert>;
Expand Down Expand Up @@ -125,6 +132,25 @@ const AlertSchema = new Schema<IAlert>(
type: String,
required: false,
},
silenced: {
required: false,
type: {
by: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: false,
},
at: {
type: Date,
required: true,
},
until: {
type: Date,
required: true,
},
required: false,
},
},
},
{
timestamps: true,
Expand Down
71 changes: 71 additions & 0 deletions packages/api/src/routers/api/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validateRequest } from 'zod-express-middleware';
import {
createAlert,
deleteAlert,
getAlertById,
getAlertsWithLogViewAndDashboard,
updateAlert,
validateGroupByProperty,
Expand Down Expand Up @@ -73,6 +74,13 @@ router.get('/', async (req, res, next) => {

return {
history,
silenced: alert.silenced
? {
by: alert.silenced.by?.email,
at: alert.silenced.at,
until: alert.silenced.until,
}
: undefined,
channel: _.pick(alert.channel, ['type']),
...(alert.dashboardId && {
dashboard: {
Expand Down Expand Up @@ -164,6 +172,69 @@ router.put(
},
);

router.post(
'/:id/silenced',
validateRequest({
body: z.object({
mutedUntil: z.string().datetime(),
}),
params: z.object({
id: objectIdSchema,
}),
}),
async (req, res, next) => {
try {
const teamId = req.user?.team;
if (teamId == null || req.user == null) {
return res.sendStatus(403);
}

const alert = await getAlertById(req.params.id, teamId);
if (!alert) {
throw new Error('Alert not found');
}
alert.silenced = {
by: req.user._id,
at: new Date(),
until: new Date(req.body.mutedUntil),
};
await alert.save();

res.sendStatus(200);
} catch (e) {
next(e);
}
},
);

router.delete(
'/:id/silenced',
validateRequest({
params: z.object({
id: objectIdSchema,
}),
}),
async (req, res, next) => {
try {
const teamId = req.user?.team;
if (teamId == null) {
return res.sendStatus(403);
}

const alert = await getAlertById(req.params.id, teamId);
if (!alert) {
throw new Error('Alert not found');
}
alert.silenced = undefined;
await alert.save();

res.sendStatus(200);
} catch (e) {
next(e);
}
},
);

router.delete(
'/:id',
validateRequest({
Expand Down
38 changes: 38 additions & 0 deletions packages/api/src/routers/api/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { z } from 'zod';
import { validateRequest } from 'zod-express-middleware';

import * as config from '@/config';
import {
generateAlertSilenceToken,
silenceAlertByToken,
} from '@/controllers/alerts';
import { createTeam, isTeamExisting } from '@/controllers/team';
import { handleAuthError, redirectToDashboard } from '@/middleware/auth';
import TeamInvite from '@/models/teamInvite';
Expand Down Expand Up @@ -167,4 +171,38 @@ router.post('/team/setup/:token', async (req, res, next) => {
}
});

router.get('/ext/silence-alert/:token', async (req, res) => {
let isError = false;

try {
const token = req.params.token;
await silenceAlertByToken(token);
} catch (e) {
isError = true;
logger.error(e);
}

// TODO: Create a template for utility pages
return res.send(`
<html>
<head>
<title>HyperDX</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css" />
</head>
<body>
<header>
<img src="https://www.hyperdx.io/Icon32.png" />
</header>
<main>
${
isError
? '<p><strong>Link is invalid or expired.</strong> Please try again.</p>'
: '<p><strong>Alert silenced.</strong> You can close this window now.</p>'
}
<a href="${config.FRONTEND_URL}">Back to HyperDX</a>
</main>
</body>
</html>`);
});

export default router;
9 changes: 9 additions & 0 deletions packages/api/src/tasks/checkAlerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ const fireChannelEvent = async ({
throw new Error('Team not found');
}

if ((alert.silenced?.until?.getTime() ?? 0) > Date.now()) {
logger.info({
message: 'Skipped firing alert due to silence',
alert,
silenced: alert.silenced,
});
return;
}

const attributesNested = expandToNestedObject(attributes);
const templateView: AlertMessageTemplateDefaultView = {
alert: {
Expand Down
Loading
Loading