Skip to content

Commit

Permalink
Improve prometheus_helper coverage and collector_manager coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kuskoman committed Mar 21, 2023
1 parent 90dbdff commit 4475b3b
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 9 deletions.
82 changes: 74 additions & 8 deletions collectors/collector_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package collectors

import (
"context"
"errors"
"sync"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -16,9 +18,21 @@ func TestNewCollectorManager(t *testing.T) {
}
}

type mockCollector struct{}
type mockCollector struct {
shouldFail bool
}

func newMockCollector(shouldFail bool) *mockCollector {
return &mockCollector{
shouldFail: shouldFail,
}
}

func (m *mockCollector) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
if m.shouldFail {
return errors.New("mock collector failed")
}

ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc("mock_metric", "mock metric description", nil, nil),
prometheus.GaugeValue,
Expand All @@ -28,20 +42,72 @@ func (m *mockCollector) Collect(ctx context.Context, ch chan<- prometheus.Metric
}

func TestCollect(t *testing.T) {
t.Run("should fail", func(t *testing.T) {
cm := &CollectorManager{
collectors: map[string]Collector{
"mock": newMockCollector(true),
},
scrapeDurations: getScrapeDurationsCollector(),
}

ch := make(chan prometheus.Metric)

var wg sync.WaitGroup
wg.Add(1)
go func() {
cm.Collect(ch)
wg.Done()
}()

select {
case <-ch:
t.Error("Expected no metric to be sent to the channel")
case <-func() chan struct{} {
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
return done
}():
// No metric was sent to the channel, which is expected.
}
})

t.Run("should succeed", func(t *testing.T) {
cm := &CollectorManager{
collectors: map[string]Collector{
"mock": newMockCollector(false),
},
scrapeDurations: getScrapeDurationsCollector(),
}

ch := make(chan prometheus.Metric)
go cm.Collect(ch)

metric := <-ch

desc := metric.Desc()
expectedDesc := "Desc{fqName: \"mock_metric\", help: \"mock metric description\", constLabels: {}, variableLabels: []}"
if desc.String() != expectedDesc {
t.Errorf("Expected metric description to be '%s', got %s", expectedDesc, desc.String())
}
})
}

func TestDescribe(t *testing.T) {
cm := &CollectorManager{
collectors: map[string]Collector{
"mock": &mockCollector{},
"mock": newMockCollector(false),
},
scrapeDurations: getScrapeDurationsCollector(),
}

ch := make(chan prometheus.Metric)
go cm.Collect(ch)

metric := <-ch
ch := make(chan *prometheus.Desc, 1)
cm.Describe(ch)

desc := metric.Desc()
expectedDesc := "Desc{fqName: \"mock_metric\", help: \"mock metric description\", constLabels: {}, variableLabels: []}"
desc := <-ch
expectedDesc := "Desc{fqName: \"logstash_exporter_scrape_duration_seconds\", help: \"logstash_exporter: Duration of a scrape job.\", constLabels: {}, variableLabels: [collector result]}"
if desc.String() != expectedDesc {
t.Errorf("Expected metric description to be '%s', got %s", expectedDesc, desc.String())
}
Expand Down
2 changes: 1 addition & 1 deletion prometheus_helper/prometheus_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type CustomCollector struct {

// Describe implements the prometheus.Collector interface.
func (c *CustomCollector) Describe(ch chan<- *prometheus.Desc) {
c.metric.Desc()
ch <- c.metric.Desc()
}

// Collect implements the prometheus.Collector interface.
Expand Down
44 changes: 44 additions & 0 deletions prometheus_helper/prometheus_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,47 @@ func TestExtractFqdnName(t *testing.T) {
}
})
}

func TestExtractValueFromMetric(t *testing.T) {
t.Run("should extract value from a metric", func(t *testing.T) {
metricDesc := prometheus.NewDesc("test_metric", "test metric help", nil, nil)
metricValue := 42.0
metric := prometheus.MustNewConstMetric(metricDesc, prometheus.GaugeValue, metricValue)

extractedValue, err := ExtractValueFromMetric(metric)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if extractedValue != metricValue {
t.Errorf("Expected extracted value to be %f, got %f", metricValue, extractedValue)
}
})

t.Run("should return an error when unable to write metric", func(t *testing.T) {
metricDesc := prometheus.NewDesc("test_metric", "test metric help", nil, nil)
exampleErr := fmt.Errorf("example error")
invalidMetric := prometheus.NewInvalidMetric(metricDesc, exampleErr)

customCollector := &CustomCollector{
metric: invalidMetric,
}

registry := prometheus.NewRegistry()
err := registry.Register(customCollector)

if err != nil {
t.Errorf("Unexpected error: %v", err)
}

extractedValue, err := ExtractValueFromMetric(invalidMetric)

if err == nil {
t.Errorf("Expected error but got nil")
}

if extractedValue != 0 {
t.Errorf("Expected extracted value to be 0, got %f", extractedValue)
}
})
}

0 comments on commit 4475b3b

Please sign in to comment.