Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 12 additions & 6 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,19 @@ func (a Agent) getHeartbeat(status string) (Heartbeat, error) {
return Heartbeat{}, bosherr.WrapError(err, "Getting job spec")
}

processes, err := a.jobSupervisor.Processes()
if err != nil {
return Heartbeat{}, bosherr.WrapError(err, "Getting processes")
}

hb := Heartbeat{
Deployment: spec.Deployment,
Job: spec.JobSpec.Name,
Index: spec.Index,
JobState: status,
Vitals: vitals,
NodeID: spec.NodeID,
Deployment: spec.Deployment,
Job: spec.JobSpec.Name,
Index: spec.Index,
JobState: status,
Vitals: vitals,
NodeID: spec.NodeID,
NumberOfProcesses: len(processes),
}

return hb, nil
Expand Down
32 changes: 26 additions & 6 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
fakeas "github.com/cloudfoundry/bosh-agent/v2/agent/applier/applyspec/fakes"
fakeagent "github.com/cloudfoundry/bosh-agent/v2/agent/fakes"
boshhandler "github.com/cloudfoundry/bosh-agent/v2/handler"
boshjobsuper "github.com/cloudfoundry/bosh-agent/v2/jobsupervisor"
fakejobsuper "github.com/cloudfoundry/bosh-agent/v2/jobsupervisor/fakes"
fakembus "github.com/cloudfoundry/bosh-agent/v2/mbus/fakes"
"github.com/cloudfoundry/bosh-agent/v2/platform/platformfakes"
Expand Down Expand Up @@ -125,6 +126,11 @@ func init() { //nolint:funlen,gochecknoinits
}

jobSupervisor.StatusStatus = "fake-state"
jobSupervisor.ProcessesStatus = []boshjobsuper.Process{
{Name: "process1", State: "running"},
{Name: "process2", State: "running"},
{Name: "process3", State: "stopped"},
}

vitalService.GetReturns(boshvitals.Vitals{
Load: []string{"a", "b", "c"},
Expand All @@ -135,12 +141,13 @@ func init() { //nolint:funlen,gochecknoinits
expectedJobIndex := 1
expectedNodeID := "node-id"
expectedHb := agent.Heartbeat{
Deployment: "FakeDeployment",
Job: &expectedJobName,
Index: &expectedJobIndex,
JobState: "fake-state",
NodeID: expectedNodeID,
Vitals: boshvitals.Vitals{Load: []string{"a", "b", "c"}},
Deployment: "FakeDeployment",
Job: &expectedJobName,
Index: &expectedJobIndex,
JobState: "fake-state",
NodeID: expectedNodeID,
Vitals: boshvitals.Vitals{Load: []string{"a", "b", "c"}},
NumberOfProcesses: 3,
}

It("sends initial heartbeat", func() {
Expand Down Expand Up @@ -247,6 +254,19 @@ func init() { //nolint:funlen,gochecknoinits
})
})

Context("when the boshAgent fails to get processes for a heartbeat", func() {
BeforeEach(func() {
jobSupervisor.ProcessesError = errors.New("fake-processes-error")
handler.KeepOnRunning()
})

It("returns the error", func() {
err := boshAgent.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("fake-processes-error"))
})
})

It("sends job monitoring alerts to health manager", func() {
handler.KeepOnRunning()

Expand Down
13 changes: 7 additions & 6 deletions agent/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
// https://www.pivotaltracker.com/story/show/132265151

type Heartbeat struct {
Deployment string `json:"deployment"`
Job *string `json:"job"`
Index *int `json:"index"`
JobState string `json:"job_state"`
Vitals boshvitals.Vitals `json:"vitals"`
NodeID string `json:"node_id"`
Deployment string `json:"deployment"`
Job *string `json:"job"`
Index *int `json:"index"`
JobState string `json:"job_state"`
Vitals boshvitals.Vitals `json:"vitals"`
NodeID string `json:"node_id"`
NumberOfProcesses int `json:"number_of_processes"`
}

// Heartbeat payload example:
Expand Down
10 changes: 6 additions & 4 deletions agent/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ func init() { //nolint:gochecknoinits
"persistent": boshvitals.SpecificDiskVitals{},
},
},
NodeID: "node-id",
NodeID: "node-id",
NumberOfProcesses: 3,
}

expectedJSON := `{"deployment":"FakeDeployment","job":"foo","index":0,"job_state":"running","vitals":{"cpu":{},"disk":{"ephemeral":{},"persistent":{},"system":{}},"mem":{},"swap":{},"uptime":{}},"node_id":"node-id"}`
expectedJSON := `{"deployment":"FakeDeployment","job":"foo","index":0,"job_state":"running","vitals":{"cpu":{},"disk":{"ephemeral":{},"persistent":{},"system":{}},"mem":{},"swap":{},"uptime":{}},"node_id":"node-id","number_of_processes":3}`

hbBytes, err := json.Marshal(hb)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -52,10 +53,11 @@ func init() { //nolint:gochecknoinits
"persistent": boshvitals.SpecificDiskVitals{},
},
},
NodeID: "node-id",
NodeID: "node-id",
NumberOfProcesses: 0,
}

expectedJSON := `{"deployment":"FakeDeployment","job":null,"index":null,"job_state":"running","vitals":{"cpu":{},"disk":{"ephemeral":{},"persistent":{},"system":{}},"mem":{},"swap":{},"uptime":{}},"node_id":"node-id"}`
expectedJSON := `{"deployment":"FakeDeployment","job":null,"index":null,"job_state":"running","vitals":{"cpu":{},"disk":{"ephemeral":{},"persistent":{},"system":{}},"mem":{},"swap":{},"uptime":{}},"node_id":"node-id","number_of_processes":0}`

hbBytes, err := json.Marshal(hb)
Expect(err).ToNot(HaveOccurred())
Expand Down