|
| 1 | +import { type DebugLogger, createDebugLogger } from '@aztec/aztec.js'; |
| 2 | +import { fileURLToPath } from '@aztec/foundation/url'; |
| 3 | + |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as yaml from 'js-yaml'; |
| 6 | +import { dirname, join } from 'path'; |
| 7 | + |
| 8 | +const GRAFANA_ENDPOINT = 'http://localhost:3000/api/datasources/proxy/uid/prometheus/api/v1/query'; |
| 9 | +interface AlertConfig { |
| 10 | + alert: string; |
| 11 | + expr: string; |
| 12 | + for: string; |
| 13 | + labels: Record<string, string>; |
| 14 | + annotations: Record<string, string>; |
| 15 | +} |
| 16 | +// Define __dirname for ES modules |
| 17 | +const __filename = fileURLToPath(import.meta.url); |
| 18 | +const __dirname = dirname(__filename); |
| 19 | + |
| 20 | +// Load YAML configuration |
| 21 | +function loadAlertsConfig(filePath: string): AlertConfig[] { |
| 22 | + const fileContents = fs.readFileSync(join(__dirname, filePath), 'utf8'); |
| 23 | + const data = yaml.load(fileContents) as { alerts: AlertConfig[] }; |
| 24 | + return data.alerts; |
| 25 | +} |
| 26 | + |
| 27 | +// Function to query Grafana based on an expression |
| 28 | +async function queryGrafana(expr: string): Promise<number> { |
| 29 | + // Create base64 encoded credentials for basic auth |
| 30 | + const credentials = Buffer.from('admin:admin').toString('base64'); |
| 31 | + |
| 32 | + const response = await fetch(`${GRAFANA_ENDPOINT}?query=${encodeURIComponent(expr)}`, { |
| 33 | + headers: { |
| 34 | + Authorization: `Basic ${credentials}`, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + if (!response.ok) { |
| 39 | + throw new Error(`Failed to fetch data from Grafana: ${response.statusText}`); |
| 40 | + } |
| 41 | + |
| 42 | + const data = await response.json(); |
| 43 | + const result = data.data.result; |
| 44 | + return result.length > 0 ? parseFloat(result[0].value[1]) : 0; |
| 45 | +} |
| 46 | + |
| 47 | +// Function to check alerts based on expressions |
| 48 | +async function checkAlerts(alerts: AlertConfig[], logger: DebugLogger) { |
| 49 | + let alertTriggered = false; |
| 50 | + |
| 51 | + for (const alert of alerts) { |
| 52 | + logger.info(`Checking alert: ${JSON.stringify(alert)}`); |
| 53 | + |
| 54 | + const metricValue = await queryGrafana(alert.expr); |
| 55 | + logger.info(`Metric value: ${metricValue}`); |
| 56 | + if (metricValue > 0) { |
| 57 | + logger.error(`Alert ${alert.alert} triggered! Value: ${metricValue}`); |
| 58 | + alertTriggered = true; |
| 59 | + } else { |
| 60 | + logger.info(`Alert ${alert.alert} passed.`); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // If any alerts have been triggered we fail the test |
| 65 | + if (alertTriggered) { |
| 66 | + throw new Error('Test failed due to triggered alert'); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// Main function to run tests |
| 71 | +async function runAlertChecker(logger: DebugLogger) { |
| 72 | + const alerts = loadAlertsConfig('alerts.yaml'); |
| 73 | + try { |
| 74 | + await checkAlerts(alerts, logger); |
| 75 | + logger.info('All alerts passed.'); |
| 76 | + } catch (error) { |
| 77 | + logger.error(error instanceof Error ? error.message : String(error)); |
| 78 | + process.exit(1); // Exit with error code |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// Running as a jest test to use existing end to end test framework |
| 83 | +describe('Alert Checker', () => { |
| 84 | + const logger = createDebugLogger('aztec:alert-checker'); |
| 85 | + it('should check alerts', async () => { |
| 86 | + await runAlertChecker(logger); |
| 87 | + }); |
| 88 | +}); |
0 commit comments