Skip to content

Commit

Permalink
Tests and fix to isPipelineHealthy()
Browse files Browse the repository at this point in the history
  • Loading branch information
excalq committed Apr 26, 2023
1 parent 3627bec commit 963168c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
59 changes: 59 additions & 0 deletions collectors/nodestats/nodestats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,62 @@ func TestCollectError(t *testing.T) {
t.Error("Expected err not to be nil")
}
}

func TestIsPipelineHealthy(t *testing.T) {
collector := NewPipelineSubcollector()
tests := []struct {
name string
stats responses.PipelineReloadResponse
expected float64
}{
{
name: "Both timestamps nil",
stats: responses.PipelineReloadResponse{
LastFailureTimestamp: nil,
LastSuccessTimestamp: nil,
},
expected: 1,
},
{
name: "Failure timestamp set",
stats: responses.PipelineReloadResponse{
LastFailureTimestamp: &time.Time{},
LastSuccessTimestamp: nil,
},
expected: 0,
},
{
name: "Success timestamp earlier than failure timestamp",
stats: responses.PipelineReloadResponse{
LastFailureTimestamp: &time.Time{},
LastSuccessTimestamp: func() *time.Time { t := time.Time{}.Add(-1 * time.Hour); return &t }(),
},
expected: 0,
},
{
name: "Success timestamp later than failure timestamp",
stats: responses.PipelineReloadResponse{
LastFailureTimestamp: &time.Time{},
LastSuccessTimestamp: func() *time.Time { t := time.Time{}.Add(1 * time.Hour); return &t }(),
},
expected: 1,
},
{
name: "Missing fields, assume healthy",
stats: responses.PipelineReloadResponse{},
expected: 1,
},
}

// Run test cases
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := collector.isPipelineHealthy(tt.stats)
if result != tt.expected {
t.Errorf("Expected %v, but got %v", tt.expected, result)
return
}
})
}

}
11 changes: 9 additions & 2 deletions collectors/nodestats/pipeline_subcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func (collector *PipelineSubcollector) Collect(pipeStats *responses.SinglePipeli
ch <- prometheus.NewMetricWithTimestamp(*pipeStats.Reloads.LastFailureTimestamp, prometheus.MustNewConstMetric(collector.ReloadsLastFailureTimestamp, prometheus.GaugeValue, 1, pipelineID))
}

if pipeStats.Reloads.LastSuccessTimestamp != nil {
ch <- prometheus.NewMetricWithTimestamp(*pipeStats.Reloads.LastSuccessTimestamp, prometheus.MustNewConstMetric(collector.ReloadsLastSuccessTimestamp, prometheus.GaugeValue, 1, pipelineID))
}
if pipeStats.Reloads.LastFailureTimestamp != nil {
ch <- prometheus.NewMetricWithTimestamp(*pipeStats.Reloads.LastFailureTimestamp, prometheus.MustNewConstMetric(collector.ReloadsLastFailureTimestamp, prometheus.GaugeValue, 1, pipelineID))
}

ch <- prometheus.MustNewConstMetric(collector.QueueEventsCount, prometheus.CounterValue, float64(pipeStats.Queue.EventsCount), pipelineID)
ch <- prometheus.MustNewConstMetric(collector.QueueEventsQueueSize, prometheus.CounterValue, float64(pipeStats.Queue.QueueSizeInBytes), pipelineID)
ch <- prometheus.MustNewConstMetric(collector.QueueMaxQueueSizeInBytes, prometheus.CounterValue, float64(pipeStats.Queue.MaxQueueSizeInBytes), pipelineID)
Expand All @@ -85,8 +92,8 @@ func (collector *PipelineSubcollector) Collect(pipeStats *responses.SinglePipeli
}

func (collector *PipelineSubcollector) isPipelineHealthy(pipeReloadStats responses.PipelineReloadResponse) float64 {
// 1. If both timestamps are nil, the pipeline is healthy
if pipeReloadStats.LastSuccessTimestamp == nil && pipeReloadStats.LastFailureTimestamp == nil {
// 1. If last failure timestamp (or both) are nil, the pipeline is healthy
if pipeReloadStats.LastFailureTimestamp == nil {
return 1
// 2. If last_failure_timestamp is set and last success timestamp is nil, the pipeline is unhealthy
} else if pipeReloadStats.LastFailureTimestamp != nil && pipeReloadStats.LastSuccessTimestamp == nil {
Expand Down

0 comments on commit 963168c

Please sign in to comment.