@@ -20,13 +20,15 @@ package api
20
20
import (
21
21
"encoding/json"
22
22
"net/http"
23
+ "net/http/httptest"
23
24
"testing"
24
25
25
26
"github.com/stretchr/testify/assert"
26
27
"github.com/stretchr/testify/require"
27
28
28
29
"github.com/elastic/apm-server/internal/beater/config"
29
30
"github.com/elastic/apm-server/internal/beater/headers"
31
+ "github.com/elastic/apm-server/internal/beater/monitoringtest"
30
32
"github.com/elastic/apm-server/internal/beater/request"
31
33
"github.com/elastic/apm-server/internal/version"
32
34
)
@@ -36,7 +38,7 @@ func TestRootHandler_AuthorizationMiddleware(t *testing.T) {
36
38
cfg .AgentAuth .SecretToken = "1234"
37
39
38
40
t .Run ("No auth" , func (t * testing.T ) {
39
- rec , err := requestToMuxerWithPattern (cfg , RootPath )
41
+ rec , err := requestToMuxerWithHeader (cfg , RootPath , http . MethodGet , nil )
40
42
require .NoError (t , err )
41
43
assert .Equal (t , http .StatusOK , rec .Code )
42
44
assert .Empty (t , rec .Body .String ())
@@ -63,10 +65,53 @@ func TestRootHandler_PanicMiddleware(t *testing.T) {
63
65
}
64
66
65
67
func TestRootHandler_MonitoringMiddleware (t * testing.T ) {
66
- testMonitoringMiddleware (t , "/" , map [string ]any {
67
- "http.server." + string (request .IDRequestCount ): 1 ,
68
- "http.server." + string (request .IDResponseCount ): 1 ,
69
- "http.server." + string (request .IDResponseValidCount ): 1 ,
70
- "http.server." + string (request .IDResponseValidOK ): 1 ,
71
- })
68
+ validMethodMetrics := map [string ]any {
69
+ "http.server." + string (request .IDRequestCount ): 1 ,
70
+ "http.server." + string (request .IDResponseCount ): 1 ,
71
+ "http.server." + string (request .IDResponseValidCount ): 1 ,
72
+ "http.server." + string (request .IDResponseValidOK ): 1 ,
73
+ "apm-server.root." + string (request .IDRequestCount ): 1 ,
74
+ "apm-server.root." + string (request .IDResponseCount ): 1 ,
75
+ "apm-server.root." + string (request .IDResponseValidCount ): 1 ,
76
+ "apm-server.root." + string (request .IDResponseValidOK ): 1 ,
77
+ }
78
+ invalidMethodMetrics := map [string ]any {
79
+ "http.server." + string (request .IDRequestCount ): 1 ,
80
+ "http.server." + string (request .IDResponseCount ): 1 ,
81
+ "http.server." + string (request .IDResponseErrorsCount ): 1 ,
82
+ "http.server." + string (request .IDResponseErrorsMethodNotAllowed ): 1 ,
83
+ "apm-server.root." + string (request .IDRequestCount ): 1 ,
84
+ "apm-server.root." + string (request .IDResponseCount ): 1 ,
85
+ "apm-server.root." + string (request .IDResponseErrorsCount ): 1 ,
86
+ "apm-server.root." + string (request .IDResponseErrorsMethodNotAllowed ): 1 ,
87
+ }
88
+ for _ , tc := range []struct {
89
+ method string
90
+ wantMetrics map [string ]any
91
+ }{
92
+ {
93
+ method : http .MethodGet ,
94
+ wantMetrics : validMethodMetrics ,
95
+ },
96
+ {
97
+ method : http .MethodHead ,
98
+ wantMetrics : validMethodMetrics ,
99
+ },
100
+ {
101
+ method : http .MethodPut ,
102
+ wantMetrics : invalidMethodMetrics ,
103
+ },
104
+ {
105
+ method : http .MethodPost ,
106
+ wantMetrics : invalidMethodMetrics ,
107
+ },
108
+ } {
109
+ t .Run (tc .method , func (t * testing.T ) {
110
+ h , reader := newTestMux (t , config .DefaultConfig ())
111
+ req := httptest .NewRequest (tc .method , "/" , nil )
112
+ h .ServeHTTP (httptest .NewRecorder (), req )
113
+
114
+ monitoringtest .ExpectContainOtelMetrics (t , reader , tc .wantMetrics )
115
+ })
116
+ }
72
117
}
0 commit comments