Skip to content
Open
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
13 changes: 6 additions & 7 deletions internal/pkg/agent/application/monitoring/liveness.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/elastic/elastic-agent-client/v7/pkg/client"

"github.com/elastic/elastic-agent/internal/pkg/otel/otelhelpers"
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
)

const formValueKey = "failon"
Expand Down Expand Up @@ -44,7 +45,7 @@ func handleFormValues(req *http.Request) (LivenessFailConfig, error) {
case "failed":
return LivenessFailConfig{Degraded: false, Failed: true, Heartbeat: true}, nil
case "degraded":
return LivenessFailConfig{Failed: true, Degraded: true, Heartbeat: true}, nil
return LivenessFailConfig{Degraded: true, Failed: true, Heartbeat: true}, nil
case "heartbeat", "":
return defaultUserCfg, nil
default:
Expand Down Expand Up @@ -76,12 +77,10 @@ func livenessHandler(coord CoordinatorState) func(http.ResponseWriter, *http.Req
return fmt.Errorf("error handling form values: %w", err)
}

// if user has requested `coordinator` mode, just revert to that, skip everything else
if !failConfig.Degraded && !failConfig.Failed && failConfig.Heartbeat {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this part as it's already covered by line 70-73, without the encapsulating if-statement.

if !isUp {
w.WriteHeader(http.StatusServiceUnavailable)
return nil
}
unhealthyState := (failConfig.Failed && state.State == agentclient.Failed) || (failConfig.Degraded && state.State == agentclient.Degraded)
if unhealthyState {
w.WriteHeader(http.StatusInternalServerError)
return nil
}

unhealthyComponent := false
Expand Down
113 changes: 46 additions & 67 deletions internal/pkg/agent/application/monitoring/liveness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
"time"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/status"
"go.opentelemetry.io/collector/component/componentstatus"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componentstatus"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
"github.com/elastic/elastic-agent/pkg/component"
"github.com/elastic/elastic-agent/pkg/component/runtime"
agentclient "github.com/elastic/elastic-agent/pkg/control/v2/client"
)

type mockCoordinator struct {
Expand All @@ -41,15 +41,19 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
defer cancel()
testCases := []struct {
name string
coord mockCoordinator
coord CoordinatorState
expectedCode int
liveness bool
failon string
}{
{
name: "healthy-nocoord",
coord: nil,
expectedCode: 200,
failon: "heartbeat",
},
{
name: "healthy",
expectedCode: 200,
liveness: true,
failon: "heartbeat",
},
{
Expand All @@ -72,7 +76,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 200,
liveness: true,
failon: "heartbeat",
},
{
Expand All @@ -95,7 +98,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 200,
liveness: true,
failon: "heartbeat",
},
{
Expand All @@ -118,7 +120,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 500,
liveness: true,
failon: "degraded",
},
{
Expand All @@ -141,34 +142,10 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 200,
liveness: true,
failon: "failed",
},
{
name: "degraded-liveness-off",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
Components: []runtime.ComponentComponentState{
{
LegacyPID: "2",
State: runtime.ComponentState{State: client.UnitStateDegraded},
Component: component.Component{
ID: "test-component",
InputSpec: &component.InputRuntimeSpec{
BinaryName: "testbeat",
},
},
},
},
},
},
expectedCode: 200,
liveness: false,
failon: "degraded",
},
{
name: "healthy",
name: "healthy-components",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
Expand All @@ -187,7 +164,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 200,
liveness: true,
failon: "degraded",
},
{
Expand All @@ -210,7 +186,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 503,
liveness: true,
failon: "heartbeat",
},
{
Expand All @@ -233,7 +208,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 503,
liveness: true,
failon: "heartbeat",
},
{
Expand All @@ -256,7 +230,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 503,
liveness: true,
failon: "degraded",
},
{
Expand All @@ -279,7 +252,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 503,
liveness: true,
failon: "degraded",
},
{
Expand All @@ -302,30 +274,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 503,
liveness: true,
failon: "degraded",
},
{
name: "healthy-liveness-off",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
Components: []runtime.ComponentComponentState{
{
LegacyPID: "5",
State: runtime.ComponentState{State: client.UnitStateHealthy},
Component: component.Component{
ID: "test-component3",
InputSpec: &component.InputRuntimeSpec{
BinaryName: "testbeat",
},
},
},
},
},
},
expectedCode: 200,
liveness: false,
failon: "degraded",
},
{
Expand Down Expand Up @@ -358,11 +306,10 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 500,
liveness: true,
failon: "degraded",
},
{
name: "healthy-liveness-off-otel",
name: "healthy-otel",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
Expand All @@ -380,7 +327,6 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 200,
liveness: false,
failon: "degraded",
},
{
Expand All @@ -402,7 +348,39 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
},
},
expectedCode: 500,
liveness: true,
failon: "degraded",
},
{
name: "unhealthy-failed-coordinator",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
State: agentclient.Failed,
},
},
expectedCode: 500,
failon: "failed",
},
{
name: "unhealthy-degraded-coordinator",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
State: agentclient.Degraded,
},
},
expectedCode: 500,
failon: "degraded",
},
{
name: "healthy-degraded-coordinator",
coord: mockCoordinator{
isUp: true,
state: coordinator.State{
State: agentclient.Failed,
},
},
expectedCode: 500,
failon: "degraded",
},
}
Expand All @@ -418,8 +396,9 @@ func TestLivenessProcessHTTPHandler(t *testing.T) {
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
res.Body.Close()
defer res.Body.Close()

require.Equal(t, test.expectedCode, res.StatusCode)
})
}

Expand Down
21 changes: 13 additions & 8 deletions internal/pkg/agent/application/monitoring/readiness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,27 @@ func TestReadinessProcessHTTPHandler(t *testing.T) {
defer cancel()
testCases := []struct {
name string
coord mockCoordinator
coord CoordinatorState
expectedCode int
liveness bool
}{
{
name: "healthy-nocoord",
coord: nil,
expectedCode: 200,
liveness: true,
},
{
name: "healthy",
name: "healthy",
coord: mockCoordinator{
isUp: true,
},
expectedCode: 200,
liveness: true,
},
{
name: "unhealthy",
name: "unhealthy",
coord: mockCoordinator{
isUp: false,
},
expectedCode: 503,
liveness: false,
},
}

Expand All @@ -50,7 +53,9 @@ func TestReadinessProcessHTTPHandler(t *testing.T) {
require.NoError(t, err)
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
res.Body.Close()
defer res.Body.Close()

require.Equal(t, test.expectedCode, res.StatusCode)
})
}

Expand Down
Loading