Add validation of metric values

Make sure to validate that when adding metric samples that the value is
a number.

Fixes: https://gitlab.com/gitlab-org/gitlab-exporter/issues/60
parent 2d4a7c3f
Pipeline #133695 passed with stage
in 52 seconds
......@@ -35,7 +35,9 @@ module GitLab
result.each do |table_name, tuple_stats|
tuple_stats.each do |column_name, value|
@metrics.add("gitlab_database_stat_table_#{column_name}", value.to_s, table_name: table_name)
@metrics.add("gitlab_database_stat_table_#{column_name}",
value,
table_name: table_name) unless value.is_a?(Numeric)
end
end
......
......@@ -16,6 +16,8 @@ module GitLab
end
def add(name, value, quantile = false, **labels)
fail "value must be a number" unless value.is_a?(Numeric)
if quantile
@quantiles[{ name: name, labels: labels }] << value
else
......
......@@ -14,4 +14,13 @@ describe GitLab::Exporter::PrometheusMetrics do
/mymetric{mylabel="x",myotherlabel="y"} 1.3 \d*$/
)
end
it "fails to add a non-numeric metric value" do
expect {
subject.add("mymetric", "1.4", mylabel: "x", myotherlabel: "y").to_s
}.to raise_error(RuntimeError)
expect {
subject.add("mymetric", "invalid", mylabel: "x", myotherlabel: "y").to_s
}.to raise_error(RuntimeError)
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment