util/clientmetric: add gauge_ name prefix when uploading names

The prefix is a signal to tsweb to treat this as a gauge metric when
generating the Prometheus version.

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
pull/4670/head
Mihai Parparita 2 years ago committed by Mihai Parparita
parent 86069874c9
commit dd5548771e

@ -241,7 +241,7 @@ func EncodeLogTailMetricsDelta() string {
m.wireID = numWireID
}
if m.lastNamed.IsZero() || now.Sub(m.lastNamed) > metricLogNameFrequency {
enc.writeName(m.Name())
enc.writeName(m.Name(), m.Type())
m.lastNamed = now
enc.writeValue(m.wireID, val)
} else {
@ -271,9 +271,16 @@ type deltaEncBuf struct {
// writeName writes a "name" (N) record to the buffer, which notes
// that the immediately following record's wireID has the provided
// name.
func (b *deltaEncBuf) writeName(name string) {
func (b *deltaEncBuf) writeName(name string, typ Type) {
var namePrefix string
if typ == TypeGauge {
// Add the gauge_ prefix so that tsweb knows that this is a gauge metric
// when generating the Prometheus version.
namePrefix = "gauge_"
}
b.buf.WriteByte('N')
b.writeHexVarint(int64(len(name)))
b.writeHexVarint(int64(len(namePrefix) + len(name)))
b.buf.WriteString(namePrefix)
b.buf.WriteString(name)
}

@ -11,9 +11,9 @@ import (
func TestDeltaEncBuf(t *testing.T) {
var enc deltaEncBuf
enc.writeName("one_one")
enc.writeName("one_one", TypeCounter)
enc.writeValue(1, 1)
enc.writeName("two_zero")
enc.writeName("two_zero", TypeGauge)
enc.writeValue(2, 0)
enc.writeDelta(1, 63)
@ -22,7 +22,7 @@ func TestDeltaEncBuf(t *testing.T) {
enc.writeDelta(2, -64)
got := enc.buf.String()
const want = "N0eone_oneS0202N10two_zeroS0400I027eI048001I028101I047f"
const want = "N0eone_oneS0202N1cgauge_two_zeroS0400I027eI048001I028101I047f"
if got != want {
t.Errorf("error\n got %q\nwant %q\n", got, want)
}
@ -49,7 +49,7 @@ func TestEncodeLogTailMetricsDelta(t *testing.T) {
clearMetrics()
c1 := NewCounter("foo")
c2 := NewCounter("bar")
c2 := NewGauge("bar")
c1.Add(123)
if got, want := EncodeLogTailMetricsDelta(), "N06fooS02f601"; got != want {
t.Errorf("first = %q; want %q", got, want)
@ -57,7 +57,7 @@ func TestEncodeLogTailMetricsDelta(t *testing.T) {
c2.Add(456)
advanceTime()
if got, want := EncodeLogTailMetricsDelta(), "N06barS049007"; got != want {
if got, want := EncodeLogTailMetricsDelta(), "N12gauge_barS049007"; got != want {
t.Errorf("second = %q; want %q", got, want)
}

Loading…
Cancel
Save