Skip to content

Commit

Permalink
feat: alert message template - pt1 (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
wrn14897 committed Feb 22, 2024
1 parent 9bb5c80 commit 0667af1
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 256 deletions.
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"scripts": {
"start": "node ./build/index.js",
"dev": "nodemon --signal SIGTERM -e ts,json --exec 'ts-node' --transpile-only -r tsconfig-paths/register -r '@hyperdx/node-opentelemetry/build/src/tracing' ./src/index.ts",
"dev:task": "ts-node -r tsconfig-paths/register -r '@hyperdx/node-opentelemetry/build/src/tracing' ./src/tasks/index.ts",
"dev:task": "ts-node -r tsconfig-paths/register ./src/tasks/index.ts",
"build": "rimraf ./build && tsc && tsc-alias",
"lint": "eslint --quiet . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
Expand Down
14 changes: 14 additions & 0 deletions packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export interface IAlert {
timezone: string;
type: AlertType;

// Message template
templateTitle?: string;
templateBody?: string;

// Log alerts
groupBy?: string;
logView?: ObjectId;
Expand Down Expand Up @@ -91,6 +95,16 @@ const AlertSchema = new Schema<IAlert>(
ref: 'Team',
},

// Message template
templateTitle: {
type: String,
required: false,
},
templateBody: {
type: String,
required: false,
},

// Log alerts
logView: {
type: mongoose.Schema.Types.ObjectId,
Expand Down
52 changes: 2 additions & 50 deletions packages/api/src/routers/external-api/v1/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import {
validateGroupByProperty,
} from '@/controllers/alerts';
import { getTeam } from '@/controllers/team';
import { AlertDocument } from '@/models/alert';
import {
alertSchema,
externalAlertSchema,
externalAlertSchemaWithId,
objectIdSchema,
translateAlertDocumentToExternalAlert,
translateExternalAlertToInternalAlert,
} from '@/utils/zod';

const router = express.Router();
Expand Down Expand Up @@ -54,53 +53,6 @@ const validateGroupBy = async (
next();
};

const translateExternalAlertToInternalAlert = (
alertInput: z.infer<typeof externalAlertSchema>,
): z.infer<typeof alertSchema> => {
return {
interval: alertInput.interval,
threshold: alertInput.threshold,
type: alertInput.threshold_type === 'above' ? 'presence' : 'absence',
channel: {
...alertInput.channel,
type: 'webhook',
},
...(alertInput.source === 'search' && alertInput.savedSearchId
? { source: 'LOG', logViewId: alertInput.savedSearchId }
: alertInput.source === 'chart' && alertInput.dashboardId
? {
source: 'CHART',
dashboardId: alertInput.dashboardId,
chartId: alertInput.chartId,
}
: ({} as never)),
};
};

const translateAlertDocumentToExternalAlert = (
alertDoc: AlertDocument,
): z.infer<typeof externalAlertSchemaWithId> => {
return {
id: alertDoc._id.toString(),
interval: alertDoc.interval,
threshold: alertDoc.threshold,
threshold_type: alertDoc.type === 'absence' ? 'below' : 'above',
channel: {
...alertDoc.channel,
type: 'slack_webhook',
},
...(alertDoc.source === 'LOG' && alertDoc.logView
? { source: 'search', savedSearchId: alertDoc.logView.toString() }
: alertDoc.source === 'CHART' && alertDoc.dashboardId
? {
source: 'chart',
dashboardId: alertDoc.dashboardId.toString(),
chartId: alertDoc.chartId as string,
}
: ({} as never)),
};
};

router.get(
'/:id',
validateRequest({
Expand Down
49 changes: 9 additions & 40 deletions packages/api/src/tasks/__tests__/checkAlerts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,42 +79,10 @@ describe('checkAlerts', () => {
});

it('doesExceedThreshold', () => {
expect(
doesExceedThreshold(
{
type: 'presence',
threshold: 10,
} as any,
11,
),
).toBe(true);
expect(
doesExceedThreshold(
{
type: 'presence',
threshold: 10,
} as any,
10,
),
).toBe(true);
expect(
doesExceedThreshold(
{
type: 'absence',
threshold: 10,
} as any,
9,
),
).toBe(true);
expect(
doesExceedThreshold(
{
type: 'absence',
threshold: 10,
} as any,
10,
),
).toBe(false);
expect(doesExceedThreshold(true, 10, 11)).toBe(true);
expect(doesExceedThreshold(true, 10, 10)).toBe(true);
expect(doesExceedThreshold(false, 10, 9)).toBe(true);
expect(doesExceedThreshold(false, 10, 10)).toBe(false);
});

describe('processAlert', () => {
Expand Down Expand Up @@ -239,17 +207,18 @@ describe('checkAlerts', () => {
1,
'https://hooks.slack.com/services/123',
{
text: 'Alert for My Log View - 11 lines found',
blocks: [
{
text: {
text: [
`*<http://localhost:9090/search/${logView._id}?from=1700172600000&to=1700172900000&q=level%3Aerror+span_name%3A%22HyperDX%22 | Alert for My Log View>*`,
`*<http://localhost:9090/search/${logView._id}?from=1700172600000&to=1700172900000&q=level%3Aerror+span_name%3A%22HyperDX%22 | Alert for "My Log View">*`,
'Group: "HyperDX"',
'11 lines found, expected less than 10 lines',
'',
'```',
'Nov 16 22:10:00Z [error] Oh no! Something went wrong!',
'```',
'',
].join('\n'),
type: 'mrkdwn',
},
Expand Down Expand Up @@ -404,14 +373,14 @@ describe('checkAlerts', () => {
1,
'https://hooks.slack.com/services/123',
{
text: 'Alert for "Max Duration" in "My Dashboard" - 102 exceeds 10',
blocks: [
{
text: {
text: [
`*<http://localhost:9090/dashboards/${dashboard._id}?from=1700170200000&granularity=5+minute&to=1700174700000 | Alert for "Max Duration" in "My Dashboard">*`,
'Group: "HyperDX"',
'102 exceeds 10',
'',
].join('\n'),
type: 'mrkdwn',
},
Expand Down Expand Up @@ -640,14 +609,14 @@ describe('checkAlerts', () => {
1,
'https://hooks.slack.com/services/123',
{
text: 'Alert for "Redis Memory" in "My Dashboard" - 395.3421052631579 exceeds 10',
blocks: [
{
text: {
text: [
`*<http://localhost:9090/dashboards/${dashboard._id}?from=1700170200000&granularity=5+minute&to=1700174700000 | Alert for "Redis Memory" in "My Dashboard">*`,
'Group: "HyperDX"',
'395.3421052631579 exceeds 10',
'',
].join('\n'),
type: 'mrkdwn',
},
Expand Down
Loading

0 comments on commit 0667af1

Please sign in to comment.