Files
CurrenciCombo/orchestrator/src/services/alerting.ts
defiQUG 14dfd3c9bf docs: Enhance development setup documentation and update environment variable validation
- Added a new section in CURRENT_STATUS.md detailing prerequisites and quick start instructions for development setup.
- Updated environment variable validation to include defaults for missing variables in env.ts.
- Improved error handling in errorHandler.ts for better validation feedback.
- Made various code adjustments across services to ensure robustness and clarity.
2025-11-05 19:00:46 -08:00

111 lines
2.6 KiB
TypeScript

import { logger } from "../logging/logger";
/**
* Alerting service
* Integrates with PagerDuty, Opsgenie, etc.
*/
export interface Alert {
severity: "critical" | "warning" | "info";
title: string;
message: string;
metadata?: any;
timestamp?: string;
}
export class AlertingService {
private pagerDutyKey?: string;
private opsgenieKey?: string;
private alertHistory: Alert[] = [];
constructor() {
this.pagerDutyKey = process.env.PAGERDUTY_INTEGRATION_KEY;
this.opsgenieKey = process.env.OPSGENIE_API_KEY;
}
/**
* Send alert
*/
async sendAlert(alert: Alert): Promise<void> {
// Prevent alert fatigue
if (this.shouldThrottle(alert)) {
logger.warn({ alert }, "Alert throttled");
return;
}
this.alertHistory.push({
...alert,
timestamp: new Date().toISOString(),
} as any);
// Send to PagerDuty
if (alert.severity === "critical" && this.pagerDutyKey) {
await this.sendToPagerDuty(alert);
}
// Send to Opsgenie
if (this.opsgenieKey) {
await this.sendToOpsgenie(alert);
}
// Log alert
logger[alert.severity === "critical" ? "error" : "warn"]({ alert }, alert.message);
}
/**
* Send to PagerDuty
*/
private async sendToPagerDuty(alert: Alert): Promise<void> {
// Mock implementation
// In production: POST to PagerDuty Events API
// await fetch("https://events.pagerduty.com/v2/enqueue", {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// routing_key: this.pagerDutyKey,
// event_action: "trigger",
// payload: {
// summary: alert.title,
// severity: alert.severity,
// source: "orchestrator",
// custom_details: alert.metadata,
// },
// }),
// });
logger.info({ alert }, "[PagerDuty] Alert sent");
}
/**
* Send to Opsgenie
*/
private async sendToOpsgenie(alert: Alert): Promise<void> {
// Mock implementation
logger.info({ alert }, "[Opsgenie] Alert sent");
}
/**
* Check if alert should be throttled (alert fatigue prevention)
*/
private shouldThrottle(alert: Alert): boolean {
const recentAlerts = this.alertHistory.filter(
(a) => a.timestamp && Date.now() - new Date(a.timestamp).getTime() < 5 * 60 * 1000 // 5 minutes
);
// Throttle if more than 10 alerts in 5 minutes
return recentAlerts.length > 10;
}
/**
* Set alert thresholds
*/
setThreshold(metric: string, threshold: number, severity: "critical" | "warning") {
// Configure alert thresholds
}
}
export const alerting = new AlertingService();