Skip to content
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
8 changes: 8 additions & 0 deletions .changeset/polite-grapes-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@hyperdx/common-utils": patch
"@hyperdx/api": patch
"@hyperdx/app": patch
"@hyperdx/cli": patch
---

feat: Add additional alert threshold types
6 changes: 5 additions & 1 deletion packages/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@
"type": "string",
"enum": [
"above",
"below"
"below",
"above_exclusive",
"below_or_equal",
"equal",
"not_equal"
],
"description": "Threshold comparison direction."
},
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
validateRawSqlForAlert,
} from '@hyperdx/common-utils/dist/core/utils';
import { isRawSqlSavedChartConfig } from '@hyperdx/common-utils/dist/guards';
import { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
import { sign, verify } from 'jsonwebtoken';
import { groupBy } from 'lodash';
import ms from 'ms';
Expand All @@ -13,7 +14,6 @@ import Alert, {
AlertChannel,
AlertInterval,
AlertSource,
AlertThresholdType,
IAlert,
} from '@/models/alert';
import Dashboard, { IDashboard } from '@/models/dashboard';
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createNativeClient } from '@hyperdx/common-utils/dist/clickhouse/node';
import {
AlertThresholdType,
BuilderSavedChartConfig,
DisplayType,
RawSqlSavedChartConfig,
Expand All @@ -15,7 +16,7 @@ import { AlertInput } from '@/controllers/alerts';
import { getTeam } from '@/controllers/team';
import { findUserByEmail } from '@/controllers/user';
import { mongooseConnection } from '@/models';
import { AlertInterval, AlertSource, AlertThresholdType } from '@/models/alert';
import { AlertInterval, AlertSource } from '@/models/alert';
import Server from '@/server';
import logger from '@/utils/logger';
import { MetricModel } from '@/utils/logParser';
Expand Down
11 changes: 5 additions & 6 deletions packages/api/src/models/alert.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { ALERT_INTERVAL_TO_MINUTES } from '@hyperdx/common-utils/dist/types';
import {
ALERT_INTERVAL_TO_MINUTES,
AlertThresholdType,
} from '@hyperdx/common-utils/dist/types';
export { AlertThresholdType } from '@hyperdx/common-utils/dist/types';
import mongoose, { Schema } from 'mongoose';

import type { ObjectId } from '.';
import Team from './team';

export enum AlertThresholdType {
ABOVE = 'above',
BELOW = 'below',
}

export enum AlertState {
ALERT = 'ALERT',
DISABLED = 'DISABLED',
Expand Down
11 changes: 5 additions & 6 deletions packages/api/src/routers/api/__tests__/alerts.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { DisplayType } from '@hyperdx/common-utils/dist/types';
import {
AlertThresholdType,
DisplayType,
} from '@hyperdx/common-utils/dist/types';

import {
getLoggedInAgent,
Expand All @@ -11,11 +14,7 @@ import {
randomMongoId,
RAW_SQL_ALERT_TEMPLATE,
} from '@/fixtures';
import Alert, {
AlertSource,
AlertState,
AlertThresholdType,
} from '@/models/alert';
import Alert, { AlertSource, AlertState } from '@/models/alert';
import AlertHistory from '@/models/alertHistory';
import Webhook, { WebhookDocument, WebhookService } from '@/models/webhook';

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/routers/external-api/v2/alerts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { alertSchema, objectIdSchema } from '@/utils/zod';
* description: Evaluation interval.
* AlertThresholdType:
* type: string
* enum: [above, below]
* enum: [above, below, above_exclusive, below_or_equal, equal, not_equal]
* description: Threshold comparison direction.
* AlertSource:
* type: string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state above threshold=5 alertValue=10 1`] = `"🚨 Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state above_exclusive threshold=5 alertValue=10 1`] = `"🚨 Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state below threshold=5 alertValue=2 1`] = `"🚨 Alert for "My Search" - 2 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state below_or_equal threshold=5 alertValue=3 1`] = `"🚨 Alert for "My Search" - 3 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state equal threshold=5 alertValue=5 1`] = `"🚨 Alert for "My Search" - 5 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts ALERT state not_equal threshold=5 alertValue=10 1`] = `"🚨 Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) above threshold=5 okValue=3 1`] = `"✅ Alert for "My Search" - 3 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) above_exclusive threshold=5 okValue=3 1`] = `"✅ Alert for "My Search" - 3 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) below threshold=5 okValue=10 1`] = `"✅ Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) below_or_equal threshold=5 okValue=10 1`] = `"✅ Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) equal threshold=5 okValue=10 1`] = `"✅ Alert for "My Search" - 10 lines found"`;

exports[`buildAlertMessageTemplateTitle saved search alerts OK state (resolved) not_equal threshold=5 okValue=5 1`] = `"✅ Alert for "My Search" - 5 lines found"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state above threshold=5 alertValue=10 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 10 meets or exceeds 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state above_exclusive threshold=5 alertValue=10 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 10 exceeds 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state below threshold=5 alertValue=2 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 2 falls below 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state below_or_equal threshold=5 alertValue=3 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 3 falls to or below 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state decimal threshold 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 10.1 meets or exceeds 1.5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state equal threshold=5 alertValue=5 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 5 equals 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state integer threshold rounds value 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 11 meets or exceeds 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts ALERT state not_equal threshold=5 alertValue=10 1`] = `"🚨 Alert for "Test Chart" in "My Dashboard" - 10 does not equal 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) above threshold=5 okValue=3 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 3 falls below 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) above_exclusive threshold=5 okValue=3 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 3 falls to or below 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) below threshold=5 okValue=10 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 10 meets or exceeds 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) below_or_equal threshold=5 okValue=10 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 10 exceeds 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) equal threshold=5 okValue=10 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 10 does not equal 5"`;

exports[`buildAlertMessageTemplateTitle tile alerts OK state (resolved) not_equal threshold=5 okValue=5 1`] = `"✅ Alert for "Test Chart" in "My Dashboard" - 5 equals 5"`;

exports[`renderAlertTemplate saved search alerts ALERT state above threshold=5 alertValue=10 1`] = `
"
10 lines found, which meets or exceeds the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state above_exclusive threshold=5 alertValue=10 1`] = `
"
10 lines found, which exceeds the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state below threshold=5 alertValue=2 1`] = `
"
2 lines found, which falls below the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state below_or_equal threshold=5 alertValue=3 1`] = `
"
3 lines found, which falls to or below the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state equal threshold=5 alertValue=5 1`] = `
"
5 lines found, which equals the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state not_equal threshold=5 alertValue=10 1`] = `
"
10 lines found, which does not equal the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts ALERT state with group 1`] = `
"Group: "http"
10 lines found, which meets or exceeds the threshold of 5 lines
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)

\`\`\`
"2023-03-17 22:14:01","error","Failed to connect to database"
"2023-03-17 22:13:45","error","Connection timeout after 30s"
"2023-03-17 22:12:30","error","Retry limit exceeded"
\`\`\`"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) above threshold=5 okValue=3 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) above_exclusive threshold=5 okValue=3 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) below threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) below_or_equal threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) equal threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) not_equal threshold=5 okValue=5 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate saved search alerts OK state (resolved) with group 1`] = `
"Group: "http" - The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state above threshold=5 alertValue=10 1`] = `
"
10 meets or exceeds 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state above_exclusive threshold=5 alertValue=10 1`] = `
"
10 exceeds 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state below threshold=5 alertValue=2 1`] = `
"
2 falls below 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state below_or_equal threshold=5 alertValue=3 1`] = `
"
3 falls to or below 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state decimal threshold 1`] = `
"
10.1 meets or exceeds 1.5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state equal threshold=5 alertValue=5 1`] = `
"
5 equals 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state integer threshold rounds value 1`] = `
"
11 meets or exceeds 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state not_equal threshold=5 alertValue=10 1`] = `
"
10 does not equal 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts ALERT state with group 1`] = `
"Group: "us-east-1"
10 meets or exceeds 5
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) above threshold=5 okValue=3 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) above_exclusive threshold=5 okValue=3 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) below threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) below_or_equal threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) equal threshold=5 okValue=10 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) not_equal threshold=5 okValue=5 1`] = `
"The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;

exports[`renderAlertTemplate tile alerts OK state (resolved) with group 1`] = `
"Group: "us-east-1" - The alert has been resolved.
Time Range (UTC): [Mar 17 10:10:00 PM - Mar 17 10:15:00 PM)
"
`;
Loading
Loading