forked from aptos-labs/e2e-latency-bench
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide to option upload metrics to multiple metrics DBs (aptos-labs#6)
- Loading branch information
1 parent
2204802
commit 8706cc9
Showing
1 changed file
with
58 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |