You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
kratos/contrib/metrics/prometheus/gauge_test.go

55 lines
1.2 KiB

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")
}
}