Skip to content

Commit

Permalink
ADD: fetchCsmMetrics (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
M4r71nW authored Feb 21, 2025
1 parent 248c8fc commit 7d0dba3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
76 changes: 76 additions & 0 deletions launcher/src/backend/Monitoring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3618,6 +3618,82 @@ export class Monitoring {
}
}

/**
* Fetches metrics from Prometheus and returns their values.
* If a metric is missing, it will be included in the result with a value of null.
* @returns {Object[]} Array of metric values e.g. [{ name: "lcoms_stealing_penalty_stolenAmount", value: 150.75 },
* { name: "lcoms_node_operator_status", value: 1 },
* { name: "if a metric is missing", value: null }]
*/
async fetchCsmMetrics() {
try {
// Retrieve service information to ensure the service is available
const serviceInfos = await this.getServiceInfos("LCOMService");
if (serviceInfos.length < 1) {
return [];
}

const queries = {
lcoms_stealing_penalty_stolenAmount: "lcoms_stealing_penalty_stolenAmount",
lcoms_node_operator_status: "lcoms_node_operator_status",
lcoms_current_bond: "lcoms_current_bond",
lcoms_required_bond: "lcoms_required_bond",
lcoms_current_bond_shares: "lcoms_current_bond_shares",
lcoms_required_bond_shares: "lcoms_required_bond_shares",
lcoms_unbonded_keys: "lcoms_unbonded_keys",
lcoms_required_bond_st_eth: "lcoms_required_bond_st_eth",
lcoms_total_added_keys: "lcoms_total_added_keys",
lcoms_total_withdrawn_keys: "lcoms_total_withdrawn_keys",
lcoms_total_deposited_keys: "lcoms_total_deposited_keys",
lcoms_total_vetted_keys: "lcoms_total_vetted_keys",
lcoms_stuck_validators_count: "lcoms_stuck_validators_count",
lcoms_depositable_validators_count: "lcoms_depositable_validators_count",
lcoms_target_limit: "lcoms_target_limit",
lcoms_target_limit_mode: "lcoms_target_limit_mode",
lcoms_total_exited_keys: "lcoms_total_exited_keys",
lcoms_enqueued_count: "lcoms_enqueued_count",
lcoms_target_validators_count: "lcoms_target_validators_count",
lcoms_refunded_validators_count: "lcoms_refunded_validators_count",
lcoms_stuck_penalty_end_timestamp: "lcoms_stuck_penalty_end_timestamp",
lcoms_total_exited_validators: "lcoms_total_exited_validators",
lcoms_total_deposited_validators: "lcoms_total_deposited_validators",
lcoms_fee_to_distribute: "lcoms_fee_to_distribute",
lcoms_fee_distributed: "lcoms_fee_distributed",
lcoms_fee_distributed_value: "lcoms_fee_distributed_value",
lcoms_exit_request_timestamp: "lcoms_exit_request_timestamp",
};

// Create promises to fetch all metric data from Prometheus
const queryPromises = Object.entries(queries).map(([key, query]) => {
return this.queryPrometheus(encodeURIComponent(query)).then((result) => ({ key, result }));
});

// Wait for all Prometheus queries to resolve
const results = await Promise.all(queryPromises);

// Process results and extract metric values, ensuring missing metrics return null
const metrics = Object.keys(queries).map((key) => {
const metric = results.find((m) => m.key === key);
if (!metric || metric.result.status !== "success" || !metric.result.data.result || metric.result.data.result.length === 0) {
return { name: key, value: null };
}

const metricData = metric.result.data.result[0];
if (!metricData || !metricData.value || metricData.value.length < 2) {
return { name: key, value: null };
}

// Parse the metric value from Prometheus response
return { name: key, value: parseFloat(metricData.value[1]) };
});

return metrics;
} catch (error) {
log.error("Fetching CSM Metrics Failed:\n" + error);
return [];
}
}

/**
* Will gather metrics from Prometheus and evaluate.
* If thresholds are exceeded, an alert will be generated and added to the retuned array.
Expand Down
4 changes: 4 additions & 0 deletions launcher/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,10 @@ ipcMain.handle("getSubnetSubs", async () => {
return await monitoring.getSubnetSubs();
});

ipcMain.handle("fetchCsmMetrics", async () => {
return await monitoring.fetchCsmMetrics();
});

ipcMain.handle("fetchCsmAlerts", async () => {
return await monitoring.fetchCsmAlerts();
});
Expand Down
4 changes: 4 additions & 0 deletions launcher/src/store/ControlService.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,10 @@ class ControlService extends EventEmitter {
return this.promiseIpc.send("fetchObolCharonAlerts");
}

async fetchCsmMetrics() {
return this.promiseIpc.send("fetchCsmMetrics");
}

async fetchCsmAlerts() {
return this.promiseIpc.send("fetchCsmAlerts");
}
Expand Down

0 comments on commit 7d0dba3

Please sign in to comment.