test(contrib): add unit test for contrib/metrics/prometheus (#2182)

Co-authored-by: rogerogers <rogers@rogerogers.com>
pull/2185/head
rogerogers 2 years ago committed by GitHub
parent 187c65bf8c
commit 63827466a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 65
      contrib/metrics/prometheus/counter_test.go
  2. 55
      contrib/metrics/prometheus/gauge_test.go
  3. 1
      contrib/metrics/prometheus/go.mod
  4. 77
      contrib/metrics/prometheus/histogram_test.go
  5. 47
      contrib/metrics/prometheus/summary_test.go

@ -0,0 +1,65 @@
package prometheus
import (
"bytes"
"fmt"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
func gatherLatest(reg *prometheus.Registry) (result string, err error) {
mfs, err := reg.Gather()
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, expfmt.FmtText)
for _, mf := range mfs {
if err = enc.Encode(mf); err != nil {
return "", err
}
}
return buf.String(), nil
}
func TestCounter(t *testing.T) {
expect := `# HELP test_request_test_metric test
# TYPE test_request_test_metric counter
test_request_test_metric{code="test",kind="test",operation="test",reason="test"} %d
`
counterVec := prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "test",
Name: "test_metric",
Subsystem: "request",
Help: "test",
}, []string{"kind", "operation", "code", "reason"})
counter := NewCounter(counterVec)
counter.With("test", "test", "test", "test").Inc()
reg := prometheus.NewRegistry()
reg.MustRegister(counterVec)
result, err := gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, 1) {
t.Fatal("metrics error")
}
counter.With("test", "test", "test", "test").Add(10)
result, err = gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, 11) {
t.Fatal("metrics error")
}
}

@ -0,0 +1,55 @@
package prometheus
import (
"fmt"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func TestGuage(t *testing.T) {
expect := `# HELP test_request_test_guage_metric test
# TYPE test_request_test_guage_metric gauge
test_request_test_guage_metric{code="test",kind="test",operation="test",reason="test"} %d
`
guageVec := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "test",
Name: "test_guage_metric",
Subsystem: "request",
Help: "test",
}, []string{"kind", "operation", "code", "reason"})
guage := NewGauge(guageVec)
guage.With("test", "test", "test", "test").Set(1)
reg := prometheus.NewRegistry()
reg.MustRegister(guageVec)
result, err := gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, 1) {
t.Fatal("metrics error")
}
guage.With("test", "test", "test", "test").Add(1)
result, err = gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, 2) {
t.Fatal("metrics error")
}
guage.With("test", "test", "test", "test").Sub(1)
result, err = gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, 1) {
t.Fatal("metrics error")
}
}

@ -5,6 +5,7 @@ go 1.16
require ( require (
github.com/go-kratos/kratos/v2 v2.3.1 github.com/go-kratos/kratos/v2 v2.3.1
github.com/prometheus/client_golang v1.12.2 github.com/prometheus/client_golang v1.12.2
github.com/prometheus/common v0.32.1
) )
replace github.com/go-kratos/kratos/v2 => ../../../ replace github.com/go-kratos/kratos/v2 => ../../../

@ -0,0 +1,77 @@
package prometheus
import (
"fmt"
"strconv"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func intToFloatString(in int) string {
return strconv.FormatFloat(float64(in), 'f', -1, 64)
}
func TestHistogram(t *testing.T) {
expect := `# HELP test_request_test_metrics test
# TYPE test_request_test_metrics histogram
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="0.05"} %s
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="0.1"} %s
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="0.25"} %s
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="0.5"} %s
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="1"} %s
test_request_test_metrics_bucket{code="test",kind="test",operation="test",reason="test",le="+Inf"} %s
test_request_test_metrics_sum{code="test",kind="test",operation="test",reason="test"} %s
test_request_test_metrics_count{code="test",kind="test",operation="test",reason="test"} %s
`
histogramVec := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: "test",
Name: "test_metrics",
Subsystem: "request",
Help: "test",
Buckets: []float64{0.05, 0.1, 0.250, 0.5, 1},
}, []string{"kind", "operation", "code", "reason"})
histogram := NewHistogram(histogramVec)
histogram.With("test", "test", "test", "test").Observe(0.5)
reg := prometheus.NewRegistry()
reg.MustRegister(histogramVec)
result, err := gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect,
intToFloatString(0),
intToFloatString(0),
intToFloatString(0),
intToFloatString(1),
intToFloatString(1),
intToFloatString(1),
"0.5",
intToFloatString(1)) {
t.Fatal("metrics error")
}
histogram.With("test", "test", "test", "test").Observe(0.1)
result, err = gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect,
intToFloatString(0),
intToFloatString(1),
intToFloatString(1),
intToFloatString(2),
intToFloatString(2),
intToFloatString(2),
"0.6",
intToFloatString(2),
) {
t.Fatal("metrics error")
}
}

@ -0,0 +1,47 @@
package prometheus
import (
"fmt"
"testing"
"github.com/prometheus/client_golang/prometheus"
)
func TestSummary(t *testing.T) {
expect := `# HELP test_request_test_metric test
# TYPE test_request_test_metric summary
test_request_test_metric_sum{code="test",kind="test",operation="test",reason="test"} %s
test_request_test_metric_count{code="test",kind="test",operation="test",reason="test"} %s
`
summaryVec := prometheus.NewSummaryVec(prometheus.SummaryOpts{
Namespace: "test",
Name: "test_metric",
Subsystem: "request",
Help: "test",
}, []string{"kind", "operation", "code", "reason"})
summary := NewSummary(summaryVec)
summary.With("test", "test", "test", "test").Observe(1)
reg := prometheus.NewRegistry()
reg.MustRegister(summaryVec)
result, err := gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, intToFloatString(1), intToFloatString(1)) {
t.Fatal("metrics error")
}
summary.With("test", "test", "test", "test").Observe(10)
result, err = gatherLatest(reg)
if err != nil {
t.Fatal(err)
}
if result != fmt.Sprintf(expect, intToFloatString(11), intToFloatString(2)) {
t.Fatal("metrics error")
}
}
Loading…
Cancel
Save