Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle requests for /processes/apm-server. #1792

Merged
merged 2 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/pkg/agent/application/monitoring/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func processHandler(coord *coordinator.Coordinator, statsHandler func(http.Respo
state := coord.State(false)

for _, c := range state.Components {
if c.Component.ID == id {
if matchesCloudProcessID(&c.Component, id) {
data := struct {
State string `json:"state"`
Message string `json:"message"`
Expand Down
10 changes: 1 addition & 9 deletions internal/pkg/agent/application/monitoring/processes.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,8 @@ func processesHandler(coord *coordinator.Coordinator) func(http.ResponseWriter,

for _, c := range state.Components {
if c.Component.InputSpec != nil {
displayID := c.Component.ID
if strings.Contains(c.Component.InputSpec.BinaryName, "apm-server") {
// Cloud explicitly looks for an ID of "apm-server" to determine if APM is in managed mode.
// Ensure that this is the ID we use, at the time of writing it is "apm-default".
// Otherwise apm-server won't be routable/accessible in cloud.
// https://github.com/elastic/elastic-agent/issues/1731#issuecomment-1325862913
displayID = "apm-server"
}
procs = append(procs, process{
ID: displayID,
ID: expectedCloudProcessID(&c.Component),
PID: c.LegacyPID,
Binary: c.Component.InputSpec.BinaryName,
Source: sourceFromComponentID(c.Component.ID),
Expand Down
32 changes: 32 additions & 0 deletions internal/pkg/agent/application/monitoring/processes_cloud.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package monitoring

import (
"strings"

"github.com/elastic/elastic-agent/pkg/component"
)

func expectedCloudProcessID(c *component.Component) string {
// Cloud explicitly looks for an ID of "apm-server" to determine if APM is in managed mode.
// Ensure that this is the ID we use, in agent v2 the ID is usually "apm-default".
// Otherwise apm-server won't be routable/accessible in cloud.
// https://github.com/elastic/elastic-agent/issues/1731#issuecomment-1325862913
if strings.Contains(c.InputSpec.BinaryName, "apm-server") {
return "apm-server"
}

return c.ID
}

func matchesCloudProcessID(c *component.Component, id string) bool {
// Similar to the case above, cloud currently makes a call to /processes/apm-server
// to find the APM server address. Rather than change all of the monitoring in cloud,
// it is easier to just make sure the existing ID maps to the APM server component.
if strings.Contains(id, "apm-server") {
if strings.Contains(c.InputSpec.BinaryName, "apm-server") {
return true
}
}

return id == c.ID
}
100 changes: 100 additions & 0 deletions internal/pkg/agent/application/monitoring/processes_cloud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package monitoring

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/elastic/elastic-agent/pkg/component"
)

func TestExpectedCloudProcessID(t *testing.T) {
testcases := []struct {
name string
component component.Component
id string
}{
{
"APM",
component.Component{
ID: "apm-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "apm-server"},
},
"apm-server",
},
{
"NotAPM",
component.Component{
ID: "filestream-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "filebeat"},
},
"filestream-default",
},
{
"AlmostAPM",
component.Component{
ID: "apm-java-attacher-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "apm-java-attacher"},
},
"apm-java-attacher-default",
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.id, expectedCloudProcessID(&tc.component))
})
}
}

func TestMatchesCloudProcessID(t *testing.T) {
testcases := []struct {
name string
processID string
component component.Component
matches bool
}{
{
"MatchesAPMServer",
"apm-server",
component.Component{
ID: "apm-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "apm-server"},
},
true,
},
{
"MatchesAPMDefault",
"apm-default",
component.Component{
ID: "apm-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "apm-server"},
},
true,
},
{
"MatchesFilestream",
"filestream-default",
component.Component{
ID: "filestream-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "filebeat"},
},
true,
},
{
"DoesNotMatch",
"filestream-default",
component.Component{
ID: "metricbeat-default",
InputSpec: &component.InputRuntimeSpec{BinaryName: "metricbeat"},
},
false,
},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.matches, matchesCloudProcessID(&tc.component, tc.processID))
})
}
}