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
20 changes: 13 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,21 @@ func (a Agent) getHeartbeat(status string) (Heartbeat, error) {
return Heartbeat{}, bosherr.WrapError(err, "Getting job spec")
}

var numberOfProcesses *int
processes, err := a.jobSupervisor.Processes()
if err == nil {
n := len(processes)
numberOfProcesses = &n
}
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: numberOfProcesses,
}

return hb, nil
}

Expand Down
69 changes: 63 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 @@ -134,13 +140,15 @@ func init() { //nolint:funlen,gochecknoinits
expectedJobName := "fake-job"
expectedJobIndex := 1
expectedNodeID := "node-id"
expectedNumberOfProcesses := 3
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: &expectedNumberOfProcesses,
}

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

Context("when the boshAgent fails to get processes for a heartbeat", func() {
BeforeEach(func() {
jobName := "fake-job"
nodeID := "node-id"
jobIndex := 1
specService.Spec = boshas.V1ApplySpec{
Deployment: "FakeDeployment",
JobSpec: boshas.JobSpec{Name: &jobName},
Index: &jobIndex,
NodeID: nodeID,
}

jobSupervisor.StatusStatus = "fake-state"
jobSupervisor.ProcessesError = errors.New("fake-processes-error")

vitalService.GetReturns(boshvitals.Vitals{
Load: []string{"a", "b", "c"},
}, nil)

handler.KeepOnRunning()
handler.SendErr = errors.New("stop")
})

It("sends heartbeat with NumberOfProcesses as nil", func() {
err := boshAgent.Run()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stop"))
Expect(err.Error()).ToNot(ContainSubstring("fake-processes-error"))

expectedJobName := "fake-job"
expectedJobIndex := 1
expectedHb := agent.Heartbeat{
Deployment: "FakeDeployment",
Job: &expectedJobName,
Index: &expectedJobIndex,
JobState: "fake-state",
NodeID: "node-id",
Vitals: boshvitals.Vitals{Load: []string{"a", "b", "c"}},
NumberOfProcesses: nil,
}

Expect(handler.SendInputs()).To(ContainElement(fakembus.SendInput{
Target: boshhandler.HealthMonitor,
Topic: boshhandler.Heartbeat,
Message: expectedHb,
}))
})
})

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
32 changes: 29 additions & 3 deletions agent/heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func init() { //nolint:gochecknoinits
It("serializes heartbeat with all fields", func() {
name := "foo"
index := 0
numberOfProcesses := 3

hb := Heartbeat{
Deployment: "FakeDeployment",
Expand All @@ -29,10 +30,11 @@ func init() { //nolint:gochecknoinits
"persistent": boshvitals.SpecificDiskVitals{},
},
},
NodeID: "node-id",
NodeID: "node-id",
NumberOfProcesses: &numberOfProcesses,
}

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 @@ -42,6 +44,30 @@ func init() { //nolint:gochecknoinits

Context("when job name, index are not available", func() {
It("serializes job name and index as nulls to indicate that there is no job assigned to this agent", func() {
numberOfProcesses := 0

hb := Heartbeat{
Deployment: "FakeDeployment",
JobState: "running",
Vitals: boshvitals.Vitals{
Disk: boshvitals.DiskVitals{
"system": boshvitals.SpecificDiskVitals{},
"ephemeral": boshvitals.SpecificDiskVitals{},
"persistent": boshvitals.SpecificDiskVitals{},
},
},
NodeID: "node-id",
NumberOfProcesses: &numberOfProcesses,
}

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())
Expect(string(hbBytes)).To(Equal(expectedJSON))
})

It("serializes NumberOfProcesses as null when not available", func() {
hb := Heartbeat{
Deployment: "FakeDeployment",
JobState: "running",
Expand All @@ -55,7 +81,7 @@ func init() { //nolint:gochecknoinits
NodeID: "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"}`
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":null}`

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