Skip to content

Commit 16e72d7

Browse files
committed
Use the apm-server version everywhere* (#4725)
* publish: parse version to set version_major Instead of hardcoding the major version, parse the full version. * beater: pass apm-server version into root handler Rather than returning the libbeat version from the root/healthcheck handler, inject the apm-server version so it can be returned. Also, in the integration tests use a hard-coded version "1.2.3", so we don't have to change approval files each time we bump the version. This should enable us to automate the version bump. # Conflicts: # beater/api/mux.go # beater/api/root/test_approved/integration/TestRootHandler_AuthorizationMiddleware/Authorized.approved.json # beater/test_approved_es_documents/TestPublishIntegrationErrors.approved.json # beater/test_approved_es_documents/TestPublishIntegrationEvents.approved.json # beater/test_approved_es_documents/TestPublishIntegrationMetricsets.approved.json # beater/test_approved_es_documents/TestPublishIntegrationMinimalEvents.approved.json # beater/test_approved_es_documents/TestPublishIntegrationProfileCPUProfile.approved.json # beater/test_approved_es_documents/TestPublishIntegrationProfileCPUProfileMetadata.approved.json # beater/test_approved_es_documents/TestPublishIntegrationProfileHeapProfile.approved.json # beater/test_approved_es_documents/TestPublishIntegrationSpans.approved.json # beater/test_approved_es_documents/TestPublishIntegrationTransactions.approved.json # publish/pub.go
1 parent 04d4f5c commit 16e72d7

26 files changed

Lines changed: 699 additions & 721 deletions

beater/api/mux.go

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package api
2020
import (
2121
"net/http"
2222

23+
"github.com/elastic/beats/v7/libbeat/beat"
2324
"github.com/elastic/beats/v7/libbeat/logp"
2425
"github.com/elastic/beats/v7/libbeat/monitoring"
2526

@@ -63,13 +64,8 @@ const (
6364
IntakeRUMV3Path = "/intake/v3/rum/events"
6465
)
6566

66-
type route struct {
67-
path string
68-
handlerFn func(*config.Config, *authorization.Builder, publish.Reporter) (request.Handler, error)
69-
}
70-
7167
// NewMux registers apm handlers to paths building up the APM Server API.
72-
func NewMux(beaterConfig *config.Config, report publish.Reporter) (*http.ServeMux, error) {
68+
func NewMux(beatInfo beat.Info, beaterConfig *config.Config, report publish.Reporter) (*http.ServeMux, error) {
7369
pool := request.NewContextPool()
7470
mux := http.NewServeMux()
7571
logger := logp.NewLogger(logs.Handler)
@@ -79,26 +75,36 @@ func NewMux(beaterConfig *config.Config, report publish.Reporter) (*http.ServeMu
7975
return nil, err
8076
}
8177

78+
builder := routeBuilder{
79+
info: beatInfo,
80+
cfg: beaterConfig,
81+
authBuilder: auth,
82+
reporter: report,
83+
}
84+
85+
type route struct {
86+
path string
87+
handlerFn func() (request.Handler, error)
88+
}
8289
routeMap := []route{
83-
{RootPath, rootHandler},
84-
{AssetSourcemapPath, sourcemapHandler},
85-
{AgentConfigPath, backendAgentConfigHandler},
86-
{AgentConfigRUMPath, rumAgentConfigHandler},
87-
{IntakeRUMPath, rumIntakeHandler},
88-
{IntakeRUMV3Path, rumV3IntakeHandler},
89-
{IntakePath, backendIntakeHandler},
90+
{RootPath, builder.rootHandler},
91+
{AssetSourcemapPath, builder.sourcemapHandler},
92+
{AgentConfigPath, builder.backendAgentConfigHandler},
93+
{AgentConfigRUMPath, builder.rumAgentConfigHandler},
94+
{IntakeRUMPath, builder.rumIntakeHandler},
95+
{IntakeRUMV3Path, builder.rumV3IntakeHandler},
96+
{IntakePath, builder.backendIntakeHandler},
9097
// The profile endpoint is in Beta
91-
{ProfilePath, profileHandler},
98+
{ProfilePath, builder.profileHandler},
9299
}
93100

94101
for _, route := range routeMap {
95-
h, err := route.handlerFn(beaterConfig, auth, report)
102+
h, err := route.handlerFn()
96103
if err != nil {
97104
return nil, err
98105
}
99106
logger.Infof("Path %s added to request handler", route.path)
100107
mux.Handle(route.path, pool.HTTPHandler(h))
101-
102108
}
103109
if beaterConfig.Expvar.IsEnabled() {
104110
path := beaterConfig.Expvar.URL
@@ -108,41 +114,53 @@ func NewMux(beaterConfig *config.Config, report publish.Reporter) (*http.ServeMu
108114
return mux, nil
109115
}
110116

111-
func profileHandler(cfg *config.Config, builder *authorization.Builder, reporter publish.Reporter) (request.Handler, error) {
112-
h := profile.Handler(reporter)
113-
authHandler := builder.ForPrivilege(authorization.PrivilegeEventWrite.Action)
114-
return middleware.Wrap(h, backendMiddleware(cfg, authHandler, profile.MonitoringMap)...)
117+
type routeBuilder struct {
118+
info beat.Info
119+
cfg *config.Config
120+
authBuilder *authorization.Builder
121+
reporter publish.Reporter
115122
}
116123

117-
func backendIntakeHandler(cfg *config.Config, builder *authorization.Builder, reporter publish.Reporter) (request.Handler, error) {
118-
h := intake.Handler(stream.BackendProcessor(cfg), reporter)
119-
authHandler := builder.ForPrivilege(authorization.PrivilegeEventWrite.Action)
120-
return middleware.Wrap(h, backendMiddleware(cfg, authHandler, intake.MonitoringMap)...)
124+
func (r *routeBuilder) profileHandler() (request.Handler, error) {
125+
h := profile.Handler(r.reporter)
126+
authHandler := r.authBuilder.ForPrivilege(authorization.PrivilegeEventWrite.Action)
127+
return middleware.Wrap(h, backendMiddleware(r.cfg, authHandler, profile.MonitoringMap)...)
121128
}
122129

123-
func rumIntakeHandler(cfg *config.Config, _ *authorization.Builder, reporter publish.Reporter) (request.Handler, error) {
124-
h := intake.Handler(stream.RUMV2Processor(cfg), reporter)
125-
return middleware.Wrap(h, rumMiddleware(cfg, nil, intake.MonitoringMap)...)
130+
func (r *routeBuilder) backendIntakeHandler() (request.Handler, error) {
131+
h := intake.Handler(stream.BackendProcessor(r.cfg), r.reporter)
132+
authHandler := r.authBuilder.ForPrivilege(authorization.PrivilegeEventWrite.Action)
133+
return middleware.Wrap(h, backendMiddleware(r.cfg, authHandler, intake.MonitoringMap)...)
126134
}
127135

128-
func rumV3IntakeHandler(cfg *config.Config, _ *authorization.Builder, reporter publish.Reporter) (request.Handler, error) {
129-
h := intake.Handler(stream.RUMV3Processor(cfg), reporter)
130-
return middleware.Wrap(h, rumMiddleware(cfg, nil, intake.MonitoringMap)...)
136+
func (r *routeBuilder) rumIntakeHandler() (request.Handler, error) {
137+
h := intake.Handler(stream.RUMV2Processor(r.cfg), r.reporter)
138+
return middleware.Wrap(h, rumMiddleware(r.cfg, nil, intake.MonitoringMap)...)
131139
}
132140

133-
func sourcemapHandler(cfg *config.Config, builder *authorization.Builder, reporter publish.Reporter) (request.Handler, error) {
134-
h := sourcemap.Handler(reporter)
135-
authHandler := builder.ForPrivilege(authorization.PrivilegeSourcemapWrite.Action)
136-
return middleware.Wrap(h, sourcemapMiddleware(cfg, authHandler)...)
141+
func (r *routeBuilder) rumV3IntakeHandler() (request.Handler, error) {
142+
h := intake.Handler(stream.RUMV3Processor(r.cfg), r.reporter)
143+
return middleware.Wrap(h, rumMiddleware(r.cfg, nil, intake.MonitoringMap)...)
137144
}
138145

139-
func backendAgentConfigHandler(cfg *config.Config, builder *authorization.Builder, _ publish.Reporter) (request.Handler, error) {
140-
authHandler := builder.ForPrivilege(authorization.PrivilegeAgentConfigRead.Action)
141-
return agentConfigHandler(cfg, authHandler, backendMiddleware)
146+
func (r *routeBuilder) sourcemapHandler() (request.Handler, error) {
147+
h := sourcemap.Handler(r.reporter)
148+
authHandler := r.authBuilder.ForPrivilege(authorization.PrivilegeSourcemapWrite.Action)
149+
return middleware.Wrap(h, sourcemapMiddleware(r.cfg, authHandler)...)
142150
}
143151

144-
func rumAgentConfigHandler(cfg *config.Config, _ *authorization.Builder, _ publish.Reporter) (request.Handler, error) {
145-
return agentConfigHandler(cfg, nil, rumMiddleware)
152+
func (r *routeBuilder) rootHandler() (request.Handler, error) {
153+
h := root.Handler(root.HandlerConfig{Version: r.info.Version})
154+
return middleware.Wrap(h, rootMiddleware(r.cfg, r.authBuilder.ForAnyOfPrivileges(authorization.ActionAny))...)
155+
}
156+
157+
func (r *routeBuilder) backendAgentConfigHandler() (request.Handler, error) {
158+
authHandler := r.authBuilder.ForPrivilege(authorization.PrivilegeAgentConfigRead.Action)
159+
return agentConfigHandler(r.cfg, authHandler, backendMiddleware)
160+
}
161+
162+
func (r *routeBuilder) rumAgentConfigHandler() (request.Handler, error) {
163+
return agentConfigHandler(r.cfg, nil, rumMiddleware)
146164
}
147165

148166
type middlewareFunc func(*config.Config, *authorization.Handler, map[request.ResultID]*monitoring.Int) []middleware.Middleware
@@ -161,11 +179,6 @@ func agentConfigHandler(cfg *config.Config, authHandler *authorization.Handler,
161179
return middleware.Wrap(h, append(middlewareFunc(cfg, authHandler, agent.MonitoringMap), ks)...)
162180
}
163181

164-
func rootHandler(cfg *config.Config, builder *authorization.Builder, _ publish.Reporter) (request.Handler, error) {
165-
return middleware.Wrap(root.Handler(),
166-
rootMiddleware(cfg, builder.ForAnyOfPrivileges(authorization.ActionAny))...)
167-
}
168-
169182
func apmMiddleware(m map[request.ResultID]*monitoring.Int) []middleware.Middleware {
170183
return []middleware.Middleware{
171184
middleware.LogMiddleware(),

beater/api/mux_config_agent_test.go

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@ package api
1919

2020
import (
2121
"net/http"
22-
"net/http/httptest"
2322
"testing"
2423

25-
"github.com/stretchr/testify/assert"
2624
"github.com/stretchr/testify/require"
2725

2826
"github.com/elastic/apm-server/approvaltest"
2927
"github.com/elastic/apm-server/beater/api/config/agent"
30-
"github.com/elastic/apm-server/beater/beatertest"
3128
"github.com/elastic/apm-server/beater/config"
3229
"github.com/elastic/apm-server/beater/headers"
3330
"github.com/elastic/apm-server/beater/request"
@@ -72,27 +69,16 @@ func TestConfigAgentHandler_KillSwitchMiddleware(t *testing.T) {
7269
}
7370

7471
func TestConfigAgentHandler_PanicMiddleware(t *testing.T) {
75-
h := testHandler(t, backendAgentConfigHandler)
76-
rec := &beatertest.WriterPanicOnce{}
77-
c := request.NewContext()
78-
c.Reset(rec, httptest.NewRequest(http.MethodGet, "/", nil))
79-
h(c)
80-
require.Equal(t, http.StatusInternalServerError, rec.StatusCode)
81-
approvaltest.ApproveJSON(t, approvalPathConfigAgent(t.Name()), rec.Body.Bytes())
72+
testPanicMiddleware(t, "/config/v1/agents", approvalPathConfigAgent(t.Name()))
8273
}
8374

8475
func TestConfigAgentHandler_MonitoringMiddleware(t *testing.T) {
85-
h := testHandler(t, backendAgentConfigHandler)
86-
c, _ := beatertest.ContextWithResponseRecorder(http.MethodPost, "/")
87-
88-
expected := map[request.ResultID]int{
76+
testMonitoringMiddleware(t, "/config/v1/agents", agent.MonitoringMap, map[request.ResultID]int{
8977
request.IDRequestCount: 1,
9078
request.IDResponseCount: 1,
9179
request.IDResponseErrorsCount: 1,
92-
request.IDResponseErrorsForbidden: 1}
93-
equal, result := beatertest.CompareMonitoringInt(h, c, expected, agent.MonitoringMap)
94-
assert.True(t, equal, result)
95-
80+
request.IDResponseErrorsForbidden: 1,
81+
})
9682
}
9783

9884
func configEnabledConfigAgent() *config.Config {

beater/api/mux_intake_backend_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ package api
1919

2020
import (
2121
"net/http"
22-
"net/http/httptest"
2322
"testing"
2423

2524
"github.com/stretchr/testify/assert"
2625
"github.com/stretchr/testify/require"
2726

2827
"github.com/elastic/apm-server/approvaltest"
2928
"github.com/elastic/apm-server/beater/api/intake"
30-
"github.com/elastic/apm-server/beater/beatertest"
3129
"github.com/elastic/apm-server/beater/config"
3230
"github.com/elastic/apm-server/beater/headers"
3331
"github.com/elastic/apm-server/beater/request"
@@ -57,27 +55,17 @@ func TestIntakeBackendHandler_AuthorizationMiddleware(t *testing.T) {
5755
}
5856

5957
func TestIntakeBackendHandler_PanicMiddleware(t *testing.T) {
60-
h := testHandler(t, backendIntakeHandler)
61-
rec := &beatertest.WriterPanicOnce{}
62-
c := request.NewContext()
63-
c.Reset(rec, httptest.NewRequest(http.MethodGet, "/", nil))
64-
h(c)
65-
assert.Equal(t, http.StatusInternalServerError, rec.StatusCode)
66-
approvaltest.ApproveJSON(t, approvalPathIntakeBackend(t.Name()), rec.Body.Bytes())
58+
testPanicMiddleware(t, "/intake/v2/events", approvalPathIntakeBackend(t.Name()))
6759
}
6860

6961
func TestIntakeBackendHandler_MonitoringMiddleware(t *testing.T) {
70-
h := testHandler(t, backendIntakeHandler)
71-
c, _ := beatertest.ContextWithResponseRecorder(http.MethodGet, "/")
7262
// send GET request resulting in 405 MethodNotAllowed error
73-
expected := map[request.ResultID]int{
63+
testMonitoringMiddleware(t, "/intake/v2/events", intake.MonitoringMap, map[request.ResultID]int{
7464
request.IDRequestCount: 1,
7565
request.IDResponseCount: 1,
7666
request.IDResponseErrorsCount: 1,
77-
request.IDResponseErrorsMethodNotAllowed: 1}
78-
79-
equal, result := beatertest.CompareMonitoringInt(h, c, expected, intake.MonitoringMap)
80-
assert.True(t, equal, result)
67+
request.IDResponseErrorsMethodNotAllowed: 1,
68+
})
8169
}
8270

8371
func approvalPathIntakeBackend(f string) string {

beater/api/mux_intake_rum_test.go

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727

2828
"github.com/elastic/apm-server/approvaltest"
2929
"github.com/elastic/apm-server/beater/api/intake"
30-
"github.com/elastic/apm-server/beater/beatertest"
3130
"github.com/elastic/apm-server/beater/config"
3231
"github.com/elastic/apm-server/beater/headers"
3332
"github.com/elastic/apm-server/beater/middleware"
@@ -94,39 +93,32 @@ func TestRUMHandler_KillSwitchMiddleware(t *testing.T) {
9493
func TestRUMHandler_CORSMiddleware(t *testing.T) {
9594
cfg := cfgEnabledRUM()
9695
cfg.RumConfig.AllowOrigins = []string{"foo"}
97-
h, err := rumIntakeHandler(cfg, nil, beatertest.NilReporter)
98-
require.NoError(t, err)
99-
c, w := beatertest.ContextWithResponseRecorder(http.MethodPost, "/")
100-
c.Request.Header.Set(headers.Origin, "bar")
101-
h(c)
102-
103-
assert.Equal(t, http.StatusForbidden, w.Code)
96+
h := newTestMux(t, cfg)
97+
98+
for _, path := range []string{"/intake/v2/rum/events", "/intake/v3/rum/events"} {
99+
req := httptest.NewRequest(http.MethodPost, path, nil)
100+
req.Header.Set(headers.Origin, "bar")
101+
w := httptest.NewRecorder()
102+
h.ServeHTTP(w, req)
103+
assert.Equal(t, http.StatusForbidden, w.Code)
104+
}
104105
}
105106

106107
func TestIntakeRUMHandler_PanicMiddleware(t *testing.T) {
107-
h, err := rumIntakeHandler(config.DefaultConfig(), nil, beatertest.NilReporter)
108-
require.NoError(t, err)
109-
rec := &beatertest.WriterPanicOnce{}
110-
c := request.NewContext()
111-
c.Reset(rec, httptest.NewRequest(http.MethodGet, "/", nil))
112-
h(c)
113-
assert.Equal(t, http.StatusInternalServerError, rec.StatusCode)
114-
approvaltest.ApproveJSON(t, approvalPathIntakeRUM(t.Name()), rec.Body.Bytes())
108+
testPanicMiddleware(t, "/intake/v2/rum/events", approvalPathIntakeRUM(t.Name()))
109+
testPanicMiddleware(t, "/intake/v3/rum/events", approvalPathIntakeRUM(t.Name()))
115110
}
116111

117112
func TestRumHandler_MonitoringMiddleware(t *testing.T) {
118-
h, err := rumIntakeHandler(config.DefaultConfig(), nil, beatertest.NilReporter)
119-
require.NoError(t, err)
120-
c, _ := beatertest.ContextWithResponseRecorder(http.MethodPost, "/")
121113
// send GET request resulting in 403 Forbidden error
122-
expected := map[request.ResultID]int{
123-
request.IDRequestCount: 1,
124-
request.IDResponseCount: 1,
125-
request.IDResponseErrorsCount: 1,
126-
request.IDResponseErrorsForbidden: 1}
127-
128-
equal, result := beatertest.CompareMonitoringInt(h, c, expected, intake.MonitoringMap)
129-
assert.True(t, equal, result)
114+
for _, path := range []string{"/intake/v2/rum/events", "/intake/v3/rum/events"} {
115+
testMonitoringMiddleware(t, path, intake.MonitoringMap, map[request.ResultID]int{
116+
request.IDRequestCount: 1,
117+
request.IDResponseCount: 1,
118+
request.IDResponseErrorsCount: 1,
119+
request.IDResponseErrorsForbidden: 1,
120+
})
121+
}
130122
}
131123

132124
func cfgEnabledRUM() *config.Config {

beater/api/mux_root_test.go

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ package api
1919

2020
import (
2121
"net/http"
22-
"net/http/httptest"
2322
"testing"
2423

2524
"github.com/stretchr/testify/assert"
2625
"github.com/stretchr/testify/require"
2726

2827
"github.com/elastic/apm-server/approvaltest"
2928
"github.com/elastic/apm-server/beater/api/root"
30-
"github.com/elastic/apm-server/beater/beatertest"
3129
"github.com/elastic/apm-server/beater/config"
3230
"github.com/elastic/apm-server/beater/headers"
3331
"github.com/elastic/apm-server/beater/request"
@@ -54,29 +52,16 @@ func TestRootHandler_AuthorizationMiddleware(t *testing.T) {
5452
}
5553

5654
func TestRootHandler_PanicMiddleware(t *testing.T) {
57-
h := testHandler(t, rootHandler)
58-
rec := &beatertest.WriterPanicOnce{}
59-
c := request.NewContext()
60-
c.Reset(rec, httptest.NewRequest(http.MethodGet, "/", nil))
61-
h(c)
62-
63-
assert.Equal(t, http.StatusInternalServerError, rec.StatusCode)
64-
approvaltest.ApproveJSON(t, approvalPathRoot(t.Name()), rec.Body.Bytes())
55+
testPanicMiddleware(t, "/", approvalPathRoot(t.Name()))
6556
}
6657

6758
func TestRootHandler_MonitoringMiddleware(t *testing.T) {
68-
h := testHandler(t, rootHandler)
69-
c, _ := beatertest.ContextWithResponseRecorder(http.MethodGet, "/")
70-
71-
// send GET request resulting in 403 Forbidden error as RUM is disabled by default
72-
expected := map[request.ResultID]int{
59+
testMonitoringMiddleware(t, "/", root.MonitoringMap, map[request.ResultID]int{
7360
request.IDRequestCount: 1,
7461
request.IDResponseCount: 1,
7562
request.IDResponseValidCount: 1,
76-
request.IDResponseValidOK: 1}
77-
78-
equal, result := beatertest.CompareMonitoringInt(h, c, expected, root.MonitoringMap)
79-
assert.True(t, equal, result)
63+
request.IDResponseValidOK: 1,
64+
})
8065
}
8166

8267
func approvalPathRoot(f string) string { return "root/test_approved/integration/" + f }

0 commit comments

Comments
 (0)