forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics_test.go
76 lines (62 loc) · 1.71 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package telemetry
import (
"encoding/json"
"strings"
"testing"
"time"
metrics "github.com/armon/go-metrics"
"github.com/prometheus/common/expfmt"
"github.com/stretchr/testify/require"
)
func TestMetrics_Disabled(t *testing.T) {
m, err := New(Config{Enabled: false})
require.Nil(t, m)
require.Nil(t, err)
}
func TestMetrics_InMem(t *testing.T) {
m, err := New(Config{
Enabled: true,
EnableHostname: false,
ServiceName: "test",
})
require.NoError(t, err)
require.NotNil(t, m)
emitMetrics()
gr, err := m.Gather(FormatText)
require.NoError(t, err)
require.Equal(t, gr.ContentType, "application/json")
jsonMetrics := make(map[string]interface{})
require.NoError(t, json.Unmarshal(gr.Metrics, &jsonMetrics))
counters := jsonMetrics["Counters"].([]interface{})
require.Equal(t, counters[0].(map[string]interface{})["Count"].(float64), 10.0)
require.Equal(t, counters[0].(map[string]interface{})["Name"].(string), "test.dummy_counter")
}
func TestMetrics_Prom(t *testing.T) {
m, err := New(Config{
Enabled: true,
EnableHostname: false,
ServiceName: "test",
PrometheusRetentionTime: 60,
EnableHostnameLabel: false,
})
require.NoError(t, err)
require.NotNil(t, m)
require.True(t, m.prometheusEnabled)
emitMetrics()
gr, err := m.Gather(FormatPrometheus)
require.NoError(t, err)
require.Equal(t, gr.ContentType, string(expfmt.FmtText))
require.True(t, strings.Contains(string(gr.Metrics), "test_dummy_counter 30"))
}
func emitMetrics() {
ticker := time.NewTicker(time.Second)
timeout := time.After(30 * time.Second)
for {
select {
case <-ticker.C:
metrics.IncrCounter([]string{"dummy_counter"}, 1.0)
case <-timeout:
return
}
}
}