@@ -27,49 +27,68 @@ func TestMetricsMiddleware(t *testing.T) {
2727 router .GET ("/error" , func (c * gin.Context ) {
2828 _ = c .AbortWithError (http .StatusInternalServerError , errors .New ("oops error" ))
2929 })
30+ router .GET ("/404" , func (c * gin.Context ) {
31+ _ = c .AbortWithError (http .StatusNotFound , errors .New ("404" ))
32+ })
3033
3134 // 2 successes, 1 errors
3235 _ = performRequest ("GET" , "/success?haha=1&hoho=2" , router )
3336 _ = performRequest ("GET" , "/error?hehe=1&huhu=3" , router )
3437 _ = performRequest ("GET" , "/success/hihi" , router )
38+ _ = performRequest ("GET" , "/404" , router )
3539
3640 metricFamilies , err := r .Gather ()
3741 require .NoError (t , err )
38-
39- const executionFailedTotal = "execution_failed_total"
40- const executionSucceededTotal = "execution_succeeded_total"
41-
42+ const (
43+ requestSucceededTotalKey = "request_succeeded_total"
44+ requestClientErrTotalKey = "request_client_error_total"
45+ requestServerErrTotalKey = "request_server_error_total"
46+ )
4247 // metricFamily.Name --> label --> counter value
4348 expected := map [string ]map [string ]int {
44- executionSucceededTotal : {
49+ requestSucceededTotalKey : {
4550 "/success" : 1 ,
4651 "/success/:test" : 1 ,
4752 "/error" : 0 ,
53+ "/404" : 0 ,
4854 },
49- executionFailedTotal : {
55+ requestServerErrTotalKey : {
5056 "/success" : 0 ,
5157 "/success/:test" : 0 ,
5258 "/error" : 1 ,
59+ "/404" : 0 ,
60+ },
61+ requestClientErrTotalKey : {
62+ "/success" : 0 ,
63+ "/success/:test" : 0 ,
64+ "/error" : 0 ,
65+ "/404" : 1 ,
5366 },
5467 }
55-
5668 for _ , metricFamily := range metricFamilies {
5769 expectedLabelCounterMap , ok := expected [* metricFamily .Name ]
5870 if ! ok {
5971 continue
6072 }
61-
6273 require .Len (t , metricFamily .Metric , len (expectedLabelCounterMap ))
6374 for _ , metric := range metricFamily .Metric {
64- require .Len (t , metric .Label , 2 )
65- var chosenLabelIdx = - 1
75+ require .Len (t , metric .Label , 3 )
76+ labelIndexes := map [string ]int {
77+ labelMethod : - 1 ,
78+ labelPath : - 1 ,
79+ labelStatus : - 1 ,
80+ }
6681 for idx , label := range metric .Label {
67- if * label .Name == labelPath {
68- chosenLabelIdx = idx
69- }
82+ labelIndexes [* label .Name ] = idx
83+ }
84+ require .Equal (t , len (labelIndexes ), 3 )
85+ for _ , labelIdx := range labelIndexes {
86+ require .NotEqual (t , - 1 , labelIdx )
7087 }
71- require .NotEqual (t , - 1 , chosenLabelIdx )
72- require .Equal (t , float64 (expectedLabelCounterMap [* metric .Label [chosenLabelIdx ].Value ]), * metric .Counter .Value )
88+ pathIdx := labelIndexes [labelPath ]
89+ path := * metric .Label [pathIdx ].Value
90+ expectedPathMetric := float64 (expectedLabelCounterMap [path ])
91+ require .Equal (t , expectedPathMetric , * metric .Counter .Value )
7392 }
7493 }
7594}
0 commit comments