1
1
package metrics
2
2
3
3
import (
4
+ "fmt"
5
+ "io"
4
6
"net/http"
7
+ "strings"
5
8
"testing"
6
9
"time"
7
10
11
+ "github.com/prometheus/client_golang/prometheus"
12
+ "github.com/prometheus/client_golang/prometheus/testutil"
8
13
. "gopkg.in/check.v1"
9
14
)
10
15
@@ -23,20 +28,52 @@ func (ms *MetricsSuite) SetUpSuite(c *C) {
23
28
ms .m = m
24
29
}
25
30
31
+ // assert that the curried metric actually uses the same underlying storage
32
+ func (ms * MetricsSuite ) TestCurryWith (c * C ) {
33
+ rpcTotalsC := RPCCount .MustCurryWith (prometheus.Labels {"host" : "test" })
34
+ rpcTotalsC .With (prometheus.Labels {"code" : "400" }).Add (1.0 )
35
+
36
+ rpcCtr := testutil .ToFloat64 (RPCCount .With (prometheus.Labels {"host" : "test" , "code" : "400" }))
37
+ c .Assert (rpcCtr , Equals , 1.0 )
38
+
39
+ RPCCount .Reset ()
40
+ }
41
+
26
42
func (ms * MetricsSuite ) TestMetrics (c * C ) {
27
43
GetFilterLogsPerChain .WithLabelValues ("chain1" ).Inc ()
28
44
GetFilterLogsPerChain .WithLabelValues ("chain2" ).Inc ()
29
45
GetFilterLogsPerChain .WithLabelValues ("chain2" ).Inc ()
30
46
time .Sleep (1 * time .Second )
31
- res , err := http .Get ("http://127.0.0.1:8886/metrics" )
47
+
48
+ chain1Ctr := testutil .ToFloat64 (GetFilterLogsPerChain .WithLabelValues ("chain1" ))
49
+ c .Assert (chain1Ctr , Equals , 1.0 )
50
+
51
+ httpClient , err := GetInstrumentedHTTPClient ("http://127.0.0.1:8886/myauthuuid" )
32
52
c .Assert (err , IsNil )
33
- c .Assert (res .StatusCode , Equals , http .StatusOK )
34
- defer res .Body .Close ()
35
- //out, err := ioutil.ReadAll(res.Body)
36
- //fmt.Println(string(out))
37
53
38
- res , err = http .Get ("http://127.0.0.1:8886" )
54
+ res , err := httpClient .Get ("http://127.0.0.1:8886" )
39
55
c .Assert (err , IsNil )
56
+ defer res .Body .Close ()
40
57
c .Assert (res .StatusCode , Equals , http .StatusOK )
58
+
59
+ res , err = httpClient .Get ("http://127.0.0.1:8886/metrics" )
60
+ c .Assert (err , IsNil )
41
61
defer res .Body .Close ()
62
+ c .Assert (res .StatusCode , Equals , http .StatusOK )
63
+ body , err := io .ReadAll (res .Body )
64
+ c .Assert (err , IsNil )
65
+ metricsBody := string (body )
66
+ c .Assert (strings .Contains (metricsBody , fmt .Sprintf ("%s_%s" , ZetaClientNamespace , "rpc_count" )), Equals , true )
67
+
68
+ // assert that rpc count is being incremented at all
69
+ rpcCount := testutil .ToFloat64 (RPCCount )
70
+ c .Assert (rpcCount , Equals , 2.0 )
71
+
72
+ // assert that rpc count is being incremented correctly
73
+ rpcCount = testutil .ToFloat64 (RPCCount .With (prometheus.Labels {"host" : "127.0.0.1:8886" , "code" : "200" }))
74
+ c .Assert (rpcCount , Equals , 2.0 )
75
+
76
+ // assert that rpc count is not being incremented incorrectly
77
+ rpcCount = testutil .ToFloat64 (RPCCount .With (prometheus.Labels {"host" : "127.0.0.1:8886" , "code" : "502" }))
78
+ c .Assert (rpcCount , Equals , 0.0 )
42
79
}
0 commit comments