Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: emit warn when labels and values are out of sync #57

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ const MetricsGuard = class MetricsGuard extends stream.Transform {
if (labels) {
return Array.from(labels.keys()).map((label) => {
const [name, value] = label.split(SEPARATOR);
if (!name) {
this.emit("warn", "metrics", `is null ${name} ${value}`);
}
return { name, value };
});
}
Expand Down Expand Up @@ -131,18 +134,31 @@ const MetricsGuard = class MetricsGuard extends stream.Transform {
this.registry.set(metric.name, entry);
}

let invalidLabels = [];
// push all label permutations on the metric into the
// Set for the metric in the registry. the size of
// this Set is compared with the threshold
metric.labels.forEach((label) => {
entry.add(`${label.name}${SEPARATOR}${label.value}`);
Array.from(Object.keys(label)).forEach((key) => {
if (label && (typeof key === "undefined" || typeof label[key] === "undefined")) {
invalidLabels.push(`${metric.name} label name="${key}", value=${label[key]}`);
} else {
entry.add(`${label.name}${SEPARATOR}${label.value}`);
}
});
});

if (metric.source === this.id) {
this.emit("drop", metric);
next(null);
return;
}
if (invalidLabels.length > 0) {
// Omit a warning when having invalid label data
this.emit("warn", "labels", invalidLabels.toString());
next(null);
return;
}

next(null, metric);
}
Expand Down
47 changes: 47 additions & 0 deletions test/guard.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,50 @@ tap.test(
});
},
);

tap.test("Guard() - do not add metric when a label value is undefined and emit warning", (t) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An undefined value should be allowed in my opinion, and not emit a warning.

const guard = new Guard();
const metric = new Metric({
name: "foo",
description: "foo",
labels: [{ name: "someLabel", value: undefined }],
});
const metrics = [metric];

const src = srcObjectStream(metrics);
const dest = destObjectStream((arr) => {
t.equal(arr.length, 0);
t.end();
});

guard.on("warn", (type, message) => {
t.equal(type, "labels");
t.match(message, /undefined/);
});
src.pipe(guard).pipe(dest);

setImmediate(() => {
dest.end();
});
});

tap.test("Guard() - do not add metric when label name / values are undefined and emit warning", (t) => {
const guard = new Guard();
const metrics = [new Metric({ name: "foo", description: "foo", labels: [{ name: undefined, value: null }] })];

const src = srcObjectStream(metrics);
const dest = destObjectStream((arr) => {
t.equal(arr.length, 0);
t.end();
});

guard.on("warn", (type, message) => {
t.equal(type, "labels");
t.match(message, /undefined/);
});
src.pipe(guard).pipe(dest);

setImmediate(() => {
dest.end();
});
});