Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley committed May 9, 2024
1 parent a751c62 commit 304b81c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/plugins/agent_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type RootHandler struct {
isGrpcRegistered bool
lastCommandSent time.Time
lastMetricReportSent time.Time
startTime time.Time
}

type NginxHandler struct {
Expand Down Expand Up @@ -254,6 +255,7 @@ func (a *AgentAPI) createHttpServer() {
a.rootHandler = &RootHandler{
config: a.config,
isGrpcRegistered: false,
startTime: time.Now(),
}

a.nginxHandler = &NginxHandler{
Expand Down Expand Up @@ -706,6 +708,7 @@ func (rh *RootHandler) healthCheck(w http.ResponseWriter) error {
})

timeNow := time.Now()
startTimeDiff := timeNow.Sub(rh.startTime)

if !rh.lastCommandSent.IsZero() {
lastCommandSentDiff := timeNow.Sub(rh.lastCommandSent)
Expand All @@ -716,6 +719,9 @@ func (rh *RootHandler) healthCheck(w http.ResponseWriter) error {
} else {
commandServiceStatus = okStatus
}
} else if startTimeDiff > (2 * rh.config.Dataplane.Status.PollInterval) {
commandServiceStatus = errorStatus
overallStatus = errorStatus
}

checks = append(checks, HealthStatusCheck{
Expand All @@ -733,6 +739,9 @@ func (rh *RootHandler) healthCheck(w http.ResponseWriter) error {
} else {
metricsServiceStatus = okStatus
}
} else if startTimeDiff > (2 * rh.config.AgentMetrics.ReportInterval) {
metricsServiceStatus = errorStatus
overallStatus = errorStatus
}

checks = append(checks, HealthStatusCheck{
Expand Down
83 changes: 81 additions & 2 deletions src/plugins/agent_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,59 @@ func TestRootHandler_healthCheck(t *testing.T) {
},
},
{
name: "Test 4: Metrics service connection failed",
name: "Test 4: Command service connection pending",
rootHandler: &RootHandler{
config: agentConfig,
isGrpcRegistered: true,
lastMetricReportSent: time.Now(),
startTime: time.Now(),
},
expected: &HealthResponse{
Status: pendingStatus,
Checks: []HealthStatusCheck{
{
Name: registration,
Status: okStatus,
},
{
Name: commandConnection,
Status: pendingStatus,
},
{
Name: metricsConnection,
Status: okStatus,
},
},
},
},
{
name: "Test 5: Command service never connected",
rootHandler: &RootHandler{
config: agentConfig,
isGrpcRegistered: true,
lastMetricReportSent: time.Now(),
startTime: time.Now().AddDate(0, 0, -1),
},
expected: &HealthResponse{
Status: errorStatus,
Checks: []HealthStatusCheck{
{
Name: registration,
Status: okStatus,
},
{
Name: commandConnection,
Status: errorStatus,
},
{
Name: metricsConnection,
Status: okStatus,
},
},
},
},
{
name: "Test 6: Metrics service connection failed",
rootHandler: &RootHandler{
config: agentConfig,
isGrpcRegistered: true,
Expand All @@ -643,11 +695,12 @@ func TestRootHandler_healthCheck(t *testing.T) {
},
},
{
name: "Test 5: Metrics service connection pending",
name: "Test 7: Metrics service connection pending",
rootHandler: &RootHandler{
config: agentConfig,
isGrpcRegistered: true,
lastCommandSent: time.Now(),
startTime: time.Now(),
},
expected: &HealthResponse{
Status: pendingStatus,
Expand All @@ -667,6 +720,32 @@ func TestRootHandler_healthCheck(t *testing.T) {
},
},
},
{
name: "Test 8: Metrics service never connected",
rootHandler: &RootHandler{
config: agentConfig,
isGrpcRegistered: true,
lastCommandSent: time.Now(),
startTime: time.Now().AddDate(0, 0, -1),
},
expected: &HealthResponse{
Status: errorStatus,
Checks: []HealthStatusCheck{
{
Name: registration,
Status: okStatus,
},
{
Name: commandConnection,
Status: okStatus,
},
{
Name: metricsConnection,
Status: errorStatus,
},
},
},
},
}

for _, tt := range tests {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 304b81c

Please sign in to comment.