|
| 1 | +import { Request, Response, Router } from 'express' |
| 2 | +import { Gauge, Registry } from 'prom-client' |
| 3 | + |
| 4 | +import { Logger } from '@crowd/logging' |
| 5 | + |
| 6 | +const KAFKA_CONNECT_URL = 'http://localhost:8083' |
| 7 | + |
| 8 | +// All possible Kafka Connect states |
| 9 | +const CONNECTOR_STATES = ['RUNNING', 'FAILED', 'PAUSED', 'UNASSIGNED'] as const |
| 10 | +const TASK_STATES = ['RUNNING', 'FAILED', 'PAUSED', 'UNASSIGNED'] as const |
| 11 | + |
| 12 | +interface ConnectorTask { |
| 13 | + id: number |
| 14 | + state: string |
| 15 | + worker_id: string |
| 16 | +} |
| 17 | + |
| 18 | +interface ConnectorStatus { |
| 19 | + name: string |
| 20 | + connector: { |
| 21 | + state: string |
| 22 | + worker_id: string |
| 23 | + } |
| 24 | + tasks: ConnectorTask[] |
| 25 | + type: string |
| 26 | +} |
| 27 | + |
| 28 | +interface ConnectorsResponse { |
| 29 | + [connectorName: string]: { |
| 30 | + status: ConnectorStatus |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +export function installConnectorHealthRoutes(app: Router, log: Logger): void { |
| 35 | + app.get('/connector-health', async (req: Request, res: Response) => { |
| 36 | + try { |
| 37 | + // Create a new registry for this request |
| 38 | + const register = new Registry() |
| 39 | + |
| 40 | + // Fetch connector statuses from Kafka Connect |
| 41 | + const connectorsUrl = `${KAFKA_CONNECT_URL}/connectors?expand=status` |
| 42 | + const response = await fetch(connectorsUrl) |
| 43 | + |
| 44 | + if (!response.ok) { |
| 45 | + log.error( |
| 46 | + { status: response.status, statusText: response.statusText }, |
| 47 | + 'Failed to fetch connector status from Kafka Connect', |
| 48 | + ) |
| 49 | + res.status(500).json({ |
| 50 | + error: 'Failed to fetch connector status from Kafka Connect', |
| 51 | + status: response.status, |
| 52 | + }) |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + const data = (await response.json()) as ConnectorsResponse |
| 57 | + |
| 58 | + // Create gauges for connector status (one-hot encoding per state) |
| 59 | + const connectorStatusGauge = new Gauge({ |
| 60 | + name: 'connector_status', |
| 61 | + help: 'Connector status (one-hot: 1 for active state, 0 otherwise)', |
| 62 | + labelNames: ['connector', 'worker_id', 'state'], |
| 63 | + registers: [register], |
| 64 | + }) |
| 65 | + |
| 66 | + // Create gauges for task status (one-hot encoding per state) |
| 67 | + const taskStatusGauge = new Gauge({ |
| 68 | + name: 'task_status', |
| 69 | + help: 'Task status (one-hot: 1 for active state, 0 otherwise)', |
| 70 | + labelNames: ['connector', 'task_id', 'worker_id', 'state'], |
| 71 | + registers: [register], |
| 72 | + }) |
| 73 | + |
| 74 | + // Process each connector |
| 75 | + for (const connectorData of Object.values(data)) { |
| 76 | + const status = connectorData.status |
| 77 | + |
| 78 | + // Set connector status metric (one-hot: 1 for current state, 0 for all others) |
| 79 | + for (const state of CONNECTOR_STATES) { |
| 80 | + connectorStatusGauge.set( |
| 81 | + { |
| 82 | + connector: status.name, |
| 83 | + worker_id: status.connector.worker_id, |
| 84 | + state, |
| 85 | + }, |
| 86 | + status.connector.state === state ? 1 : 0, |
| 87 | + ) |
| 88 | + } |
| 89 | + |
| 90 | + // Set task status metrics (one-hot: 1 for current state, 0 for all others) |
| 91 | + for (const task of status.tasks) { |
| 92 | + for (const state of TASK_STATES) { |
| 93 | + taskStatusGauge.set( |
| 94 | + { |
| 95 | + connector: status.name, |
| 96 | + task_id: task.id.toString(), |
| 97 | + worker_id: task.worker_id, |
| 98 | + state, |
| 99 | + }, |
| 100 | + task.state === state ? 1 : 0, |
| 101 | + ) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Return metrics in Prometheus format |
| 107 | + res.set('Content-Type', register.contentType) |
| 108 | + res.send(await register.metrics()) |
| 109 | + } catch (err) { |
| 110 | + log.error(err, 'Error fetching connector health') |
| 111 | + res.status(500).json({ error: 'Internal server error' }) |
| 112 | + } |
| 113 | + }) |
| 114 | +} |
0 commit comments