1
1
package metrics_test
2
2
3
3
import (
4
+ "bytes"
5
+ "fmt"
4
6
"testing"
5
7
6
- "github.com/prometheus/client_golang/prometheus"
7
8
"github.com/prometheus/client_golang/prometheus/testutil"
8
9
"github.com/stretchr/testify/assert"
9
10
"github.com/stretchr/testify/require"
@@ -13,29 +14,37 @@ import (
13
14
14
15
func TestMetrics_Init_IsSafe (t * testing.T ) {
15
16
t .Parallel ()
16
- metrics .InitWithStaticMetrics (true , map [string ]string {
17
- "product" : "fps" ,
18
- "f1_id" : "myid" ,
19
- }) // race detector assertion
17
+ labels := map [string ]string {
18
+ "product" : "fps" ,
19
+ "customer" : "fake-customer" ,
20
+ "f1_id" : "myid" ,
21
+ "labelx" : "vx" ,
22
+ }
23
+ metrics .InitWithStaticMetrics (true , labels ) // race detector assertion
20
24
for range 10 {
21
25
go func () {
22
- metrics .InitWithStaticMetrics (true , map [string ]string {
23
- "product" : "fps" ,
24
- "f1_id" : "myid" ,
25
- })
26
+ metrics .InitWithStaticMetrics (true , labels )
26
27
}()
27
28
}
28
29
assert .True (t , metrics .Instance ().IterationMetricsEnabled )
29
30
metrics .Instance ().RecordIterationResult ("test1" , metrics .SuccessResult , 1 )
30
31
assert .Equal (t , 1 , testutil .CollectAndCount (metrics .Instance ().Iteration , "form3_loadtest_iteration" ))
31
- o , err := metrics .Instance ().Iteration .MetricVec .GetMetricWith (prometheus.Labels {
32
- metrics .TestNameLabel : "test1" ,
33
- metrics .StageLabel : metrics .IterationStage ,
34
- metrics .ResultLabel : metrics .SuccessResult .String (),
35
- "product" : "fps" ,
36
- "f1_id" : "myid" ,
37
- })
38
- require .NoError (t , err )
39
- assert .Contains (t , o .Desc ().String (), "product" )
40
- assert .Contains (t , o .Desc ().String (), "f1_id" )
32
+
33
+ expected := `
34
+ # HELP form3_loadtest_iteration Duration of iteration functions.
35
+ # TYPE form3_loadtest_iteration summary
36
+ `
37
+ quantileFormat := `
38
+ form3_loadtest_iteration{customer="fake-customer",f1_id="myid",labelx="vx",product="fps",result="success",stage="iteration",test="test1",quantile="%s"} 1
39
+ `
40
+ for _ , quantile := range []string {"0.5" , "0.75" , "0.9" , "0.95" , "0.99" , "0.9999" , "1.0" } {
41
+ expected += fmt .Sprintf (quantileFormat , quantile )
42
+ }
43
+
44
+ expected += `
45
+ form3_loadtest_iteration_sum{customer="fake-customer",f1_id="myid",labelx="vx",product="fps",result="success",stage="iteration",test="test1"} 1
46
+ form3_loadtest_iteration_count{customer="fake-customer",f1_id="myid",labelx="vx",product="fps",result="success",stage="iteration",test="test1"} 1
47
+ `
48
+ r := bytes .NewReader ([]byte (expected ))
49
+ require .NoError (t , testutil .CollectAndCompare (metrics .Instance ().Iteration , r ))
41
50
}
0 commit comments