tsweb: add Float expvar support in varz

We make assertions about stringification of 0.5. IEEE floating point and
all reasonable proprietary floating point can exactly represent 0.5.
We don't make assertions about other floating point values, too brittle
in tests.

Signed-off-by: Denton Gentry <dgentry@tailscale.com>
pull/5040/head
Denton Gentry 2022-07-11 20:36:52 -07:00 committed by Denton Gentry
parent cca25f6107
commit 755396d6fe
2 changed files with 34 additions and 0 deletions

View File

@ -466,6 +466,12 @@ func writePromExpVar(w io.Writer, prefix string, kv expvar.KeyValue) {
}
fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value())
return
case *expvar.Float:
if typ == "" {
typ = "gauge"
}
fmt.Fprintf(w, "# TYPE %s %s\n%s %v\n", name, typ, name, v.Value())
return
case *metrics.Set:
v.Do(func(kv expvar.KeyValue) {
writePromExpVar(w, name+"_", kv)

View File

@ -334,6 +334,9 @@ func TestVarzHandler(t *testing.T) {
t.Logf("Got: %s", rec.Body.Bytes())
})
half := new(expvar.Float)
half.Set(0.5)
tests := []struct {
name string
k string // key name
@ -358,6 +361,31 @@ func TestVarzHandler(t *testing.T) {
new(expvar.Int),
"# TYPE foo gauge\nfoo 0\n",
},
{
// For a float = 0.0, Prometheus client_golang outputs "0"
"float_zero",
"foo",
new(expvar.Float),
"# TYPE foo gauge\nfoo 0\n",
},
{
"float_point_5",
"foo",
half,
"# TYPE foo gauge\nfoo 0.5\n",
},
{
"float_with_type_counter",
"counter_foo",
half,
"# TYPE foo counter\nfoo 0.5\n",
},
{
"float_with_type_gauge",
"gauge_foo",
half,
"# TYPE foo gauge\nfoo 0.5\n",
},
{
"metrics_set",
"s",