From 941f585164d69b32738ee18b9c1ab7ec728930c9 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Sat, 28 Mar 2015 18:51:41 +0000 Subject: [PATCH] Avoid +InfYs and similar, just display +Inf. --- rules/ast/quantile.go | 4 ++-- templates/templates.go | 7 +++++-- templates/templates_test.go | 7 +++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/rules/ast/quantile.go b/rules/ast/quantile.go index bf43c777d..38ddc7976 100644 --- a/rules/ast/quantile.go +++ b/rules/ast/quantile.go @@ -25,8 +25,8 @@ import ( // excludedLabels are the labels to exclude from signature calculation for // quantiles. var excludedLabels = map[clientmodel.LabelName]struct{}{ - clientmodel.MetricNameLabel: struct{}{}, - clientmodel.BucketLabel: struct{}{}, + clientmodel.MetricNameLabel: {}, + clientmodel.BucketLabel: {}, } type bucket struct { diff --git a/templates/templates.go b/templates/templates.go index ca7946d87..2bbb70f25 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -140,7 +140,7 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c return v }, "humanize": func(v float64) string { - if v == 0 { + if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) { return fmt.Sprintf("%.4g", v) } if math.Abs(v) >= 1 { @@ -165,7 +165,7 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c return fmt.Sprintf("%.4g%s", v, prefix) }, "humanize1024": func(v float64) string { - if math.Abs(v) <= 1 { + if math.Abs(v) <= 1 || math.IsNaN(v) || math.IsInf(v, 0) { return fmt.Sprintf("%.4g", v) } prefix := "" @@ -179,6 +179,9 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c return fmt.Sprintf("%.4g%s", v, prefix) }, "humanizeDuration": func(v float64) string { + if math.IsNaN(v) || math.IsInf(v, 0) { + return fmt.Sprintf("%.4g", v) + } if v == 0 { return fmt.Sprintf("%.4gs", v) } diff --git a/templates/templates_test.go b/templates/templates_test.go index 549211518..9caa14897 100644 --- a/templates/templates_test.go +++ b/templates/templates_test.go @@ -14,6 +14,7 @@ package templates import ( + "math" "testing" clientmodel "github.com/prometheus/client_golang/model" @@ -122,6 +123,12 @@ func TestTemplateExpansion(t *testing.T) { input: []float64{.1, .0001, .12345, 60.1, 60.5, 1.2345, 12.345}, output: "100ms:100us:123.5ms:1m 0s:1m 0s:1.234s:12.35s:", }, + { + // Humanize* Inf and NaN. + text: "{{ range . }}{{ humanize . }}:{{ humanize1024 . }}:{{ humanizeDuration . }}:{{ end }}", + input: []float64{math.Inf(1), math.Inf(-1), math.NaN()}, + output: "+Inf:+Inf:+Inf:-Inf:-Inf:-Inf:NaN:NaN:NaN:", + }, { // Title. text: "{{ \"aa bb CC\" | title }}",