Skip to content

Instantly share code, notes, and snippets.

@xcooper
Created May 4, 2023 06:48
Show Gist options
  • Save xcooper/3452fe33633efd8e731aa9ad0a17ebad to your computer and use it in GitHub Desktop.
Save xcooper/3452fe33633efd8e731aa9ad0a17ebad to your computer and use it in GitHub Desktop.
JS for converting Prometheus format to JSON
<!DOCTYPE html>
<html>
<head>
<title>Exposition to JSON Converter</title>
</head>
<body>
<h1>Exposition to JSON Converter</h1>
<form id="expositionForm">
<label for="expositionInput">Exposition String:</label>
<br>
<textarea id="expositionInput" name="exposition" rows="10" cols="50"></textarea>
<br>
<button type="submit">Convert to JSON</button>
<br>
<textarea id="output" name="exposition" rows="30" cols="50"></textarea>
</form>
<script>
function convertExpositionToJson(expositionString) {
// Split the string into individual lines
const lines = expositionString.trim().split("\n");
// Loop through the lines and parse the metric data
const metrics = {};
let currentMetric = null;
let help = null;
let type = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith("# HELP")) {
help = line.substr(7).trim();
} else if (line.startsWith("# TYPE")) {
type = line.substr(7).trim().split(' ')[1];
} else if (line.startsWith("#")) {
// Ignore comment lines
continue;
} else if (line.startsWith(" ")) {
// This line is a continuation of the previous label
const [key, value] = line.trim().split(" ", 2);
const currentValue = currentMetric.labels[key];
currentMetric.labels[key] = currentValue + value;
} else {
// This line is the start of a new metric
currentMetric = {};
currentMetric.labels = [];
currentMetric.help = help;
currentMetric.type = type;
const [nameAndLabels, value] = line.split(" ");
currentMetric.value = parseFloat(value);
const [name, labelsString] = nameAndLabels.split("{");
currentMetric.name = name;
if (labelsString) {
const labelsArray = labelsString.replace("}", "").split(",");
for (let j = 0; j < labelsArray.length; j++) {
const [key, value] = labelsArray[j].split("=");
const label = {};
label['name'] = key;
label['value'] = value;
currentMetric.labels.push(label);
}
}
const metricAttribute = {};
metricAttribute[currentMetric.type] = currentMetric.value;
delete currentMetric.value;
metricAttribute.label = currentMetric.labels;
delete currentMetric.labels;
metrics[name] = { ...currentMetric, metric: [metricAttribute] };
}
}
// Convert the metrics array to a JSON format string and return it
return JSON.stringify(metrics, null, 2);
}
// Get references to the HTML elements
const form = document.getElementById("expositionForm");
const input = document.getElementById("expositionInput");
const output = document.getElementById("output");
form.addEventListener("submit", (event) => {
event.preventDefault();
// Get the exposition string from the input field
const expositionString = input.value.trim();
// Convert the exposition string to JSON format
const json = convertExpositionToJson(expositionString);
output.value = json;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment