Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Dec 19, 2023
1 parent 846fc65 commit 4fc879e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
8 changes: 4 additions & 4 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,22 @@ func isCounterName(name string) bool {
return strings.HasSuffix(name, "_total")
}

// WriteGaugeUint64 writes gauge metric with the given name and value to w.
// WriteGaugeUint64 writes gauge metric with the given name and value to w in Prometheus text exposition format.
func WriteGaugeUint64(w io.Writer, name string, value uint64) {
writeMetricUint64(w, name, "gauge", value)
}

// WriteGaugeFloat64 writes gauge metric with the given name and value to w.
// WriteGaugeFloat64 writes gauge metric with the given name and value to w in Prometheus text exposition format.
func WriteGaugeFloat64(w io.Writer, name string, value float64) {
writeMetricFloat64(w, name, "gauge", value)
}

// WriteCounterUint64 writes counter metric with the given name and value to w.
// WriteCounterUint64 writes counter metric with the given name and value to w in Prometheus text exposition format.
func WriteCounterUint64(w io.Writer, name string, value uint64) {
writeMetricUint64(w, name, "counter", value)
}

// WriteCounterFloat64 writes counter metric with the given name and value to w.
// WriteCounterFloat64 writes counter metric with the given name and value to w in Prometheus text exposition format.
func WriteCounterFloat64(w io.Writer, name string, value float64) {
writeMetricFloat64(w, name, "counter", value)
}
Expand Down
75 changes: 75 additions & 0 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,81 @@ import (
"time"
)

func TestWriteMetrics(t *testing.T) {
t.Run("gauge_uint64", func(t *testing.T) {
var bb bytes.Buffer

WriteGaugeUint64(&bb, "foo", 123)
sExpected := "foo 123\n"
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}

ExposeMetadata(true)
bb.Reset()
WriteGaugeUint64(&bb, "foo", 123)
sExpected = "# HELP foo\n# TYPE foo gauge\nfoo 123\n"
ExposeMetadata(false)
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}
})
t.Run("gauge_float64", func(t *testing.T) {
var bb bytes.Buffer

WriteGaugeFloat64(&bb, "foo", 1.23)
sExpected := "foo 1.23\n"
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}

ExposeMetadata(true)
bb.Reset()
WriteGaugeFloat64(&bb, "foo", 1.23)
sExpected = "# HELP foo\n# TYPE foo gauge\nfoo 1.23\n"
ExposeMetadata(false)
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}
})
t.Run("counter_uint64", func(t *testing.T) {
var bb bytes.Buffer

WriteCounterUint64(&bb, "foo_total", 123)
sExpected := "foo_total 123\n"
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}

ExposeMetadata(true)
bb.Reset()
WriteCounterUint64(&bb, "foo_total", 123)
sExpected = "# HELP foo_total\n# TYPE foo_total counter\nfoo_total 123\n"
ExposeMetadata(false)
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}
})
t.Run("counter_float64", func(t *testing.T) {
var bb bytes.Buffer

WriteCounterFloat64(&bb, "foo_total", 1.23)
sExpected := "foo_total 1.23\n"
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}

ExposeMetadata(true)
bb.Reset()
WriteCounterFloat64(&bb, "foo_total", 1.23)
sExpected = "# HELP foo_total\n# TYPE foo_total counter\nfoo_total 1.23\n"
ExposeMetadata(false)
if s := bb.String(); s != sExpected {
t.Fatalf("unexpected value; got\n%s\nwant\n%s", s, sExpected)
}
})
}

func TestGetDefaultSet(t *testing.T) {
s := GetDefaultSet()
if s != defaultSet {
Expand Down

0 comments on commit 4fc879e

Please sign in to comment.