Skip to content

Instantly share code, notes, and snippets.

@wperron
Created May 11, 2023 13:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/d04a751f3581692ee3021cd2d7489491 to your computer and use it in GitHub Desktop.
Save wperron/d04a751f3581692ee3021cd2d7489491 to your computer and use it in GitHub Desktop.
Compare two different sets of prometheus metrics based on the output of the `/metrics` endpoint
#!/usr/bin/env deno run -A
import { readStringDelim } from "https://deno.land/std@0.186.0/io/read_string_delim.ts";
import * as path from "https://deno.land/std@0.186.0/path/mod.ts";
async function extractMetricNames(f) {
const filename = path.join(Deno.cwd(), f);
let fileReader = await Deno.open(filename);
let metrics = new Set();
for await (let line of readStringDelim(fileReader, "\n")) {
if (!line.startsWith("#") && line.length !== 0) {
let end = line.indexOf("{");
if (end === -1) {
end = line.indexOf(" ");
}
if (end === -1) {
end = line.indexOf("\n");
}
const metricName = line.slice(0, end);
metrics.add(metricName);
}
}
metrics = [...metrics];
metrics.sort();
return metrics;
}
let [left, right] = await Promise.all([
extractMetricNames(Deno.args[0]),
extractMetricNames(Deno.args[1]),
]);
for (const l of left) {
if (!right.includes(l)) {
console.log(`%c-${l}`, "color: red");
}
}
for (const r of right) {
if (!left.includes(r)) {
console.log(`%c+${r}`, "color: green");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment