Skip to content

Commit

Permalink
Support for releases info, tests refactoring (DRY)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Olejnik authored and benjaminguttmann-avtq committed Sep 11, 2023
1 parent b9ffbff commit 0b97750
Show file tree
Hide file tree
Showing 17 changed files with 1,392 additions and 1,972 deletions.
225 changes: 139 additions & 86 deletions README.md

Large diffs are not rendered by default.

87 changes: 9 additions & 78 deletions collectors/bosh_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewBoshCollector(
processesFilter *filters.RegexpFilter,
cidrsFilter *filters.CidrFilter,
) *BoshCollector {
enabledCollectors := []Collector{}
var enabledCollectors []Collector

if collectorsFilter.Enabled(filters.DeploymentsCollector) {
deploymentsCollector := NewDeploymentsCollector(namespace, environment, boshName, boshUUID)
Expand All @@ -59,84 +59,15 @@ func NewBoshCollector(
enabledCollectors = append(enabledCollectors, serviceDiscoveryCollector)
}

totalBoshScrapesMetric := prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "",
Name: "scrapes_total",
Help: "Total number of times BOSH was scraped for metrics.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

totalBoshScrapeErrorsMetric := prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "",
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

lastBoshScrapeErrorMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from BOSH resulted in an error (1 for error, 0 for success).",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

lastBoshScrapeTimestampMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

lastBoshScrapeDurationSecondsMetric := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

metrics := NewBoshCollectorMetrics(namespace, environment, boshName, boshUUID)
return &BoshCollector{
enabledCollectors: enabledCollectors,
deploymentsFetcher: deploymentsFetcher,
totalBoshScrapesMetric: totalBoshScrapesMetric,
totalBoshScrapeErrorsMetric: totalBoshScrapeErrorsMetric,
lastBoshScrapeErrorMetric: lastBoshScrapeErrorMetric,
lastBoshScrapeTimestampMetric: lastBoshScrapeTimestampMetric,
lastBoshScrapeDurationSecondsMetric: lastBoshScrapeDurationSecondsMetric,
totalBoshScrapesMetric: metrics.NewTotalBoshScrapesMetric(),
totalBoshScrapeErrorsMetric: metrics.NewTotalBoshScrapeErrorsMetric(),
lastBoshScrapeErrorMetric: metrics.NewLastBoshScrapeErrorMetric(),
lastBoshScrapeTimestampMetric: metrics.NewLastBoshScrapeTimestampMetric(),
lastBoshScrapeDurationSecondsMetric: metrics.NewLastBoshScrapeDurationSecondsMetric(),
}
}

Expand Down Expand Up @@ -164,13 +95,13 @@ func (c *BoshCollector) Collect(ch chan<- prometheus.Metric) {

scrapeError := 0
c.totalBoshScrapesMetric.Inc()
deployments, err := c.deploymentsFetcher.Deployments()
ds, err := c.deploymentsFetcher.Deployments()
if err != nil {
log.Error(err)
scrapeError = 1
c.totalBoshScrapeErrorsMetric.Inc()
} else {
if err := c.executeCollectors(deployments, ch); err != nil {
if err := c.executeCollectors(ds, ch); err != nil {
log.Error(err)
scrapeError = 1
c.totalBoshScrapeErrorsMetric.Inc()
Expand Down
106 changes: 106 additions & 0 deletions collectors/bosh_collector_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package collectors

import (
"github.com/prometheus/client_golang/prometheus"
)

type BoshCollectorMetrics struct {
namespace string
environment string
boshName string
boshUUID string
}

func NewBoshCollectorMetrics(
namespace string,
environment string,
boshName string,
boshUUID string,
) *BoshCollectorMetrics {
return &BoshCollectorMetrics{
namespace: namespace,
environment: environment,
boshName: boshName,
boshUUID: boshUUID,
}
}

func (m *BoshCollectorMetrics) NewLastBoshScrapeDurationSecondsMetric() prometheus.Gauge {
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: "",
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": m.environment,
"bosh_name": m.boshName,
"bosh_uuid": m.boshUUID,
},
},
)
}

func (m *BoshCollectorMetrics) NewLastBoshScrapeTimestampMetric() prometheus.Gauge {
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: "",
Name: "last_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": m.environment,
"bosh_name": m.boshName,
"bosh_uuid": m.boshUUID,
},
},
)
}

func (m *BoshCollectorMetrics) NewLastBoshScrapeErrorMetric() prometheus.Gauge {
return prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: m.namespace,
Subsystem: "",
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from BOSH resulted in an error (1 for error, 0 for success).",
ConstLabels: prometheus.Labels{
"environment": m.environment,
"bosh_name": m.boshName,
"bosh_uuid": m.boshUUID,
},
},
)
}

func (m *BoshCollectorMetrics) NewTotalBoshScrapeErrorsMetric() prometheus.Counter {
return prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: m.namespace,
Subsystem: "",
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping BOSH.",
ConstLabels: prometheus.Labels{
"environment": m.environment,
"bosh_name": m.boshName,
"bosh_uuid": m.boshUUID,
},
},
)
}

func (m *BoshCollectorMetrics) NewTotalBoshScrapesMetric() prometheus.Counter {
return prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: m.namespace,
Subsystem: "",
Name: "scrapes_total",
Help: "Total number of times BOSH was scraped for metrics.",
ConstLabels: prometheus.Labels{
"environment": m.environment,
"bosh_name": m.boshName,
"bosh_uuid": m.boshUUID,
},
},
)
}
78 changes: 7 additions & 71 deletions collectors/bosh_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var _ = Describe("BoshCollector", func() {
azsFilter *filters.AZsFilter
processesFilter *filters.RegexpFilter
cidrsFilter *filters.CidrFilter
metrics *BoshCollectorMetrics
boshCollector *BoshCollector

totalBoshScrapesMetric prometheus.Counter
Expand All @@ -55,6 +56,7 @@ var _ = Describe("BoshCollector", func() {
environment = testEnvironment
boshName = testBoshName
boshUUID = testBoshUUID
metrics = NewBoshCollectorMetrics(testNamespace, testEnvironment, testBoshName, testBoshUUID)
tmpfile, err = os.CreateTemp("", "service_discovery_collector_test_")
Expect(err).ToNot(HaveOccurred())
serviceDiscoveryFilename = tmpfile.Name()
Expand All @@ -71,79 +73,13 @@ var _ = Describe("BoshCollector", func() {
processesFilter, err = filters.NewRegexpFilter([]string{})
Expect(err).ToNot(HaveOccurred())

totalBoshScrapesMetric = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "",
Name: "scrapes_total",
Help: "Total number of times BOSH was scraped for metrics.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

totalBoshScrapesMetric = metrics.NewTotalBoshScrapesMetric()
totalBoshScrapesMetric.Inc()

totalBoshScrapeErrorsMetric = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Subsystem: "",
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

lastBoshScrapeErrorMetric = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from BOSH resulted in an error (1 for error, 0 for success).",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

totalBoshScrapeErrorsMetric = metrics.NewTotalBoshScrapeErrorsMetric()
lastBoshScrapeErrorMetric = metrics.NewLastBoshScrapeErrorMetric()
lastBoshScrapeErrorMetric.Set(float64(0))

lastBoshScrapeTimestampMetric = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_timestamp",
Help: "Number of seconds since 1970 since last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)

lastBoshScrapeDurationSecondsMetric = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "",
Name: "last_scrape_duration_seconds",
Help: "Duration of the last scrape from BOSH.",
ConstLabels: prometheus.Labels{
"environment": environment,
"bosh_name": boshName,
"bosh_uuid": boshUUID,
},
},
)
lastBoshScrapeTimestampMetric = metrics.NewLastBoshScrapeTimestampMetric()
lastBoshScrapeDurationSecondsMetric = metrics.NewLastBoshScrapeDurationSecondsMetric()
})

AfterEach(func() {
Expand Down
2 changes: 1 addition & 1 deletion collectors/collectors_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ const (
)

func TestCollectors(t *testing.T) {
RegisterFailHandler(Fail)
RegisterFailHandler(AbortSuite)
RunSpecs(t, "Collectors Suite")
}
Loading

0 comments on commit 0b97750

Please sign in to comment.