prometheus

This commit is contained in:
CN-JS-HuiBai
2026-04-04 23:08:30 +08:00
parent f3f49f2c8e
commit a1703e72be
4 changed files with 78 additions and 14 deletions

View File

@@ -12,9 +12,9 @@ const httpsAgent = new https.Agent({ keepAlive: true, rejectUnauthorized: false
const serverIdMap = new Map(); // token -> { instance, job, source }
const SECRET = crypto.randomBytes(16).toString('hex');
function getServerToken(instance) {
function getServerToken(instance, job, source) {
const hash = crypto.createHmac('sha256', SECRET)
.update(instance)
.update(`${instance}:${job}:${source}`)
.digest('hex')
.substring(0, 16);
return hash;
@@ -220,11 +220,11 @@ async function getOverviewMetrics(url, sourceName) {
const instances = new Map();
const getOrCreate = (metric) => {
const originalInstance = metric.instance;
const token = getServerToken(originalInstance, sourceName);
const job = metric.job || 'Unknown';
const token = getServerToken(originalInstance, job, sourceName);
// Store mapping for detail queries
serverIdMap.set(token, { instance: originalInstance, source: sourceName, job: metric.job });
serverIdMap.set(token, { instance: originalInstance, source: sourceName, job });
if (!instances.has(token)) {
instances.set(token, {
@@ -252,14 +252,13 @@ async function getOverviewMetrics(url, sourceName) {
};
// Initialize instances from targets first (to ensure we have all servers even if they have no metrics)
const nodeJobRegex = /node|exporter|host/i;
for (const target of targetsResult) {
const labels = target.labels || {};
const instance = labels.instance;
const job = labels.job;
const job = labels.job || '';
// Only include targets that look like node-exporters
if (instance && (nodeJobRegex.test(job) || nodeJobRegex.test(target.scrapePool))) {
// Include every target from the activeTargets list
if (instance) {
const inst = getOrCreate(labels);
inst.up = target.health === 'up';
}
@@ -371,8 +370,7 @@ async function getOverviewMetrics(url, sourceName) {
total: totalTraffic24hRx + totalTraffic24hTx
},
servers: allInstancesList.map(s => {
const { originalInstance, ...rest } = s;
return rest;
return s; // Include all fields including originalInstance
})
};
}