diff --git a/src/plugins/agent_api.go b/src/plugins/agent_api.go index b01060ab79..ba212bd7cc 100644 --- a/src/plugins/agent_api.go +++ b/src/plugins/agent_api.go @@ -75,6 +75,7 @@ type RootHandler struct { isGrpcRegistered bool lastCommandSent time.Time lastMetricReportSent time.Time + startTime time.Time } type NginxHandler struct { @@ -254,6 +255,7 @@ func (a *AgentAPI) createHttpServer() { a.rootHandler = &RootHandler{ config: a.config, isGrpcRegistered: false, + startTime: time.Now(), } a.nginxHandler = &NginxHandler{ @@ -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) @@ -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{ @@ -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{ diff --git a/src/plugins/agent_api_test.go b/src/plugins/agent_api_test.go index 758dfa9a0e..39a05368f0 100644 --- a/src/plugins/agent_api_test.go +++ b/src/plugins/agent_api_test.go @@ -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, @@ -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, @@ -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 { diff --git a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go index b01060ab79..ba212bd7cc 100644 --- a/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go +++ b/test/performance/vendor/github.com/nginx/agent/v2/src/plugins/agent_api.go @@ -75,6 +75,7 @@ type RootHandler struct { isGrpcRegistered bool lastCommandSent time.Time lastMetricReportSent time.Time + startTime time.Time } type NginxHandler struct { @@ -254,6 +255,7 @@ func (a *AgentAPI) createHttpServer() { a.rootHandler = &RootHandler{ config: a.config, isGrpcRegistered: false, + startTime: time.Now(), } a.nginxHandler = &NginxHandler{ @@ -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) @@ -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{ @@ -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{