Skip to content

Commit

Permalink
Provide to option upload metrics to multiple metrics DBs (aptos-labs#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
manudhundi authored Nov 7, 2024
1 parent 2204802 commit 8706cc9
Showing 1 changed file with 58 additions and 28 deletions.
86 changes: 58 additions & 28 deletions common.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,78 @@
import axios from 'axios';

const METRICS_URL = process.env.METRICS_URL;
const METRICS_AUTH_TOKEN = process.env.METRICS_AUTH_TOKEN;
const METRICS_TAG = process.env.METRICS_TAG;

// Metrics helper functions
function getMetricPayload(name, labels, value) {
const labelsStrs = [];

for (const [k, v] of Object.entries(labels)) {
if (!v) {
continue;
}
labelsStrs.push(`${k}="${v}"`);
}

const allLabelsStr = labelsStrs.join(",");
return `${name}{${allLabelsStr}} ${value}`;
}

async function pushMetrics(payloads) {
if (!METRICS_URL) {
console.log("No METRICS_URL specified! Skipping dump...");
}

function getMetricsConfig() {
const metricsConfigs = [];

// URL and Token
const firstUrl = process.env.METRICS_URL;
const firstToken = process.env.METRICS_AUTH_TOKEN;

if (firstUrl && firstToken) {
metricsConfigs.push({ url: firstUrl, token: firstToken });
}

// Additional URLs and Tokens with index (_2, _3, etc.)
let index = 2;

while (true) {
const url = process.env[`METRICS_URL_${index}`];
const token = process.env[`METRICS_AUTH_TOKEN_${index}`];

if (!url || !token) {
break; // Stop if either the URL or token is missing for the current index
}

metricsConfigs.push({ url, token });
index++;
}

return metricsConfigs;
}

async function pushMetrics(payloads) {
const metricsConfigs = getMetricsConfig();

if (metricsConfigs.length === 0) {
console.log("No valid METRICS_URL and METRICS_AUTH_TOKEN pairs! Skipping dump...");
return;
}
const headers = {
"Authorization": `Bearer ${METRICS_AUTH_TOKEN}`,
};

const url = `${METRICS_URL}/api/v1/import/prometheus/metrics/job/e2elatency-${METRICS_TAG}/`;
const data = payloads + '\n';

try {
const response = await axios.post(url, data, { headers });
return response.status;
} catch (error) {
console.error("Error pushing metrics ", error.message);
return 500; // Return a default status code in case of an error

for (const { url, token } of metricsConfigs) {
const headers = {
"Authorization": `Bearer ${token}`,
"apikey": `${token}`
};

const fullUrl = `${url}/api/v1/import/prometheus/metrics/job/e2elatency-${METRICS_TAG}/`;
const data = payloads + '\n';
try {
await axios.post(fullUrl, data, { headers });
} catch (error) {
console.error(`Error pushing metrics to ${fullUrl}: `, error.message);
}
}
}

async function sleepAsync(milliseconds) {
return 200;
}

async function sleepAsync(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds));
}
}

export { getMetricPayload, pushMetrics, sleepAsync }
export { getMetricPayload, pushMetrics, sleepAsync };

0 comments on commit 8706cc9

Please sign in to comment.