Skip to content

Commit

Permalink
suppress metrics if data was not fetched.
Browse files Browse the repository at this point in the history
  • Loading branch information
kbudde committed Sep 18, 2016
1 parent a4894b1 commit 17bbadb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 10 deletions.
35 changes: 25 additions & 10 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ type exporter struct {
overviewMetrics map[string]prometheus.Gauge
upMetric prometheus.Gauge
exchangeMetrics map[string]*prometheus.CounterVec
overviewFetched bool
queuesFetched bool
exchangesFetched bool
}

func newExporter() *exporter {
Expand All @@ -31,6 +34,10 @@ func (e *exporter) fetchRabbit() {
rabbitMqQueueData, queueError := getStatsInfo(config, "queues")
exchangeData, exchangeError := getStatsInfo(config, "exchanges")

e.overviewFetched = overviewError == nil
e.queuesFetched = queueError == nil
e.exchangesFetched = exchangeError == nil

if overviewError != nil || queueError != nil || exchangeError != nil {
e.upMetric.Set(0)
} else {
Expand Down Expand Up @@ -98,6 +105,9 @@ func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
for _, exchangeMetric := range e.exchangeMetrics {
exchangeMetric.Describe(ch)
}

e.upMetric.Describe(ch)
BuildInfo.Describe(ch)
}

func (e *exporter) Collect(ch chan<- prometheus.Metric) {
Expand All @@ -118,20 +128,25 @@ func (e *exporter) Collect(ch chan<- prometheus.Metric) {

e.upMetric.Collect(ch)

for _, gauge := range e.overviewMetrics {
gauge.Collect(ch)
if e.overviewFetched {
for _, gauge := range e.overviewMetrics {
gauge.Collect(ch)
}
}

for _, gaugevec := range e.queueMetricsGauge {
gaugevec.Collect(ch)
}
for _, countervec := range e.queueMetricsCounter {
countervec.Collect(ch)
if e.queuesFetched {
for _, gaugevec := range e.queueMetricsGauge {
gaugevec.Collect(ch)
}
for _, countervec := range e.queueMetricsCounter {
countervec.Collect(ch)
}
}

for _, exchangeMetric := range e.exchangeMetrics {
exchangeMetric.Collect(ch)
if e.exchangesFetched {
for _, exchangeMetric := range e.exchangeMetrics {
exchangeMetric.Collect(ch)
}
}

BuildInfo.Collect(ch)
}
26 changes: 26 additions & 0 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func TestWholeApp(t *testing.T) {

exporter := newExporter()
prometheus.MustRegister(exporter)
defer prometheus.Unregister(exporter)

req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
Expand All @@ -169,4 +170,29 @@ func TestWholeApp(t *testing.T) {
expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_exchange_messages_published_in_total{exchange="myExchange",vhost="/"} 5`)
expectSubstring(t, body, `rabbitmq_queue_messages_delivered_total{queue="myQueue1",vhost="/"} 0`)

}

func TestRabbitError(t *testing.T) {
server := createTestserver(500, http.StatusText(500))
defer server.Close()
os.Setenv("RABBIT_URL", server.URL)
initConfig()

exporter := newExporter()
prometheus.MustRegister(exporter)
defer prometheus.Unregister(exporter)

req, _ := http.NewRequest("GET", "", nil)
w := httptest.NewRecorder()
prometheus.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
body := w.Body.String()
// fmt.Println(body)
expectSubstring(t, body, `rabbitmq_up 0`)
if strings.Contains(body, "rabbitmq_channelsTotal") {
t.Errorf("Metric 'rabbitmq_channelsTotal' unexpected as the server did not respond")
}
}
7 changes: 7 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import log "github.com/Sirupsen/logrus"

func init() {
log.SetLevel(log.FatalLevel)
}

0 comments on commit 17bbadb

Please sign in to comment.