Merge pull request #616 from brian-brazil/yotta-infinity

Avoid +InfYs and similar, just display +Inf.
This commit is contained in:
Julius Volz 2015-03-28 20:27:56 +01:00
commit 52d89b2173
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 // excludedLabels are the labels to exclude from signature calculation for
// quantiles. // quantiles.
var excludedLabels = map[clientmodel.LabelName]struct{}{ var excludedLabels = map[clientmodel.LabelName]struct{}{
clientmodel.MetricNameLabel: struct{}{}, clientmodel.MetricNameLabel: {},
clientmodel.BucketLabel: struct{}{}, clientmodel.BucketLabel: {},
} }
type bucket struct { type bucket struct {

View file

@ -140,7 +140,7 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c
return v return v
}, },
"humanize": func(v float64) string { "humanize": func(v float64) string {
if v == 0 { if v == 0 || math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v) return fmt.Sprintf("%.4g", v)
} }
if math.Abs(v) >= 1 { 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) return fmt.Sprintf("%.4g%s", v, prefix)
}, },
"humanize1024": func(v float64) string { "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) return fmt.Sprintf("%.4g", v)
} }
prefix := "" prefix := ""
@ -179,6 +179,9 @@ func NewTemplateExpander(text string, name string, data interface{}, timestamp c
return fmt.Sprintf("%.4g%s", v, prefix) return fmt.Sprintf("%.4g%s", v, prefix)
}, },
"humanizeDuration": func(v float64) string { "humanizeDuration": func(v float64) string {
if math.IsNaN(v) || math.IsInf(v, 0) {
return fmt.Sprintf("%.4g", v)
}
if v == 0 { if v == 0 {
return fmt.Sprintf("%.4gs", v) return fmt.Sprintf("%.4gs", v)
} }

View file

@ -14,6 +14,7 @@
package templates package templates
import ( import (
"math"
"testing" "testing"
clientmodel "github.com/prometheus/client_golang/model" 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}, 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:", 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. // Title.
text: "{{ \"aa bb CC\" | title }}", text: "{{ \"aa bb CC\" | title }}",