Avoid +InfYs and similar, just display +Inf.

This commit is contained in:
Brian Brazil 2015-03-28 18:51:41 +00:00
parent b841ae41cd
commit 941f585164
3 changed files with 14 additions and 4 deletions

View file

@ -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 {

View file

@ -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)
}

View file

@ -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 }}",