Skip to content

Commit

Permalink
added cluster and node label to up metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kbudde committed Mar 11, 2019
1 parent c7a726c commit 5d38b91
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 36 deletions.
34 changes: 23 additions & 11 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func RegisterExporter(name string, f func() Exporter) {

type exporter struct {
mutex sync.RWMutex
upMetric prometheus.Gauge
upMetric *prometheus.GaugeVec
endpointUpMetric *prometheus.GaugeVec
endpointScrapeDurationMetric *prometheus.GaugeVec
exporter map[string]Exporter
Expand All @@ -58,9 +58,9 @@ func newExporter() *exporter {
}

return &exporter{
upMetric: newGauge("up", "Was the last scrape of rabbitmq successful."),
endpointUpMetric: newGaugeVec("module_up", "Was the last scrape of rabbitmq successful per module.", []string{"module"}),
endpointScrapeDurationMetric: newGaugeVec("module_scrape_duration_seconds", "Duration of the last scrape in seconds", []string{"module"}),
upMetric: newGaugeVec("up", "Was the last scrape of rabbitmq successful.", []string{"cluster", "node"}),
endpointUpMetric: newGaugeVec("module_up", "Was the last scrape of rabbitmq successful per module.", []string{"cluster", "node", "module"}),
endpointScrapeDurationMetric: newGaugeVec("module_scrape_duration_seconds", "Duration of the last scrape in seconds", []string{"cluster", "node", "module"}),
exporter: enabledExporter,
overviewExporter: newExporterOverview(),
}
Expand Down Expand Up @@ -108,9 +108,9 @@ func (e *exporter) Collect(ch chan<- prometheus.Metric) {
BuildInfo.Collect(ch)

if allUp {
e.upMetric.Set(1)
e.upMetric.WithLabelValues(e.overviewExporter.NodeInfo().ClusterName, e.overviewExporter.NodeInfo().Node).Set(1)
} else {
e.upMetric.Set(0)
e.upMetric.WithLabelValues(e.overviewExporter.NodeInfo().ClusterName, e.overviewExporter.NodeInfo().Node).Set(0)
}
e.upMetric.Collect(ch)
e.endpointUpMetric.Collect(ch)
Expand All @@ -122,15 +122,27 @@ func (e *exporter) Collect(ch chan<- prometheus.Metric) {
func collectWithDuration(ctx context.Context, ex Exporter, name string, ch chan<- prometheus.Metric) error {
startModule := time.Now()
err := ex.Collect(ctx, ch)
node := ""
if n, ok := ctx.Value(clusterName).(string); ok {
node = n
}
cluster := ""
if n, ok := ctx.Value(clusterName).(string); ok {
cluster = n
}

if scrapeDuration, ok := ctx.Value(endpointScrapeDuration).(*prometheus.GaugeVec); ok {
scrapeDuration.WithLabelValues(name).Set(time.Since(startModule).Seconds())
if cluster != "" && node != "" { //values are not available until first scrape of overview succeeded
scrapeDuration.WithLabelValues(cluster, node, name).Set(time.Since(startModule).Seconds())
}
}
if up, ok := ctx.Value(endpointUpMetric).(*prometheus.GaugeVec); ok {
if err != nil {
up.WithLabelValues(name).Set(0)
} else {
up.WithLabelValues(name).Set(1)
if cluster != "" && node != "" { //values are not available until first scrape of overview succeeded
if err != nil {
up.WithLabelValues(cluster, node, name).Set(0)
} else {
up.WithLabelValues(cluster, node, name).Set(1)
}
}
}
return err
Expand Down
51 changes: 26 additions & 25 deletions exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestWholeApp(t *testing.T) {
}
body := w.Body.String()
t.Log(body)
expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 1`)

// overview
expectSubstring(t, body, `rabbitmq_exchanges{cluster="my-rabbit@ae74c041248b"} 8`)
Expand Down Expand Up @@ -135,7 +135,7 @@ func TestWholeAppInverted(t *testing.T) {
}
body := w.Body.String()
t.Log(body)
expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 1`)

// overview is always scraped and exported
expectSubstring(t, body, `rabbitmq_exchanges{cluster="my-rabbit@ae74c041248b"} 8`)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestAppMaxQueues(t *testing.T) {
}
body := w.Body.String()

expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 1`)

// overview
expectSubstring(t, body, `rabbitmq_exchanges{cluster="my-rabbit@ae74c041248b"} 8`)
Expand Down Expand Up @@ -231,15 +231,15 @@ func TestRabbitError(t *testing.T) {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
body := w.Body.String()
// fmt.Println(body)
expectSubstring(t, body, `rabbitmq_up 0`)

expectSubstring(t, body, `rabbitmq_up{cluster="",node=""} 0`) //TODO: on first scrape values are empty
if strings.Contains(body, "rabbitmq_channelsTotal") {
t.Errorf("Metric 'rabbitmq_channelsTotal' unexpected as the server did not respond")
}
}

//TestResetMetricsOnRabbitFailure verifies the behaviour of the exporter if the rabbitmq fails after one successfull retrieval of the data
// List of metrics should be empty except rabbitmq_up should be 0
// List of metrics should be empty except rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} should be 0
func TestResetMetricsOnRabbitFailure(t *testing.T) {
rabbitUP := true
rabbitQueuesUp := true
Expand Down Expand Up @@ -295,13 +295,14 @@ func TestResetMetricsOnRabbitFailure(t *testing.T) {
t.Errorf("Home page didn't return %v", http.StatusOK)
}
body := w.Body.String()
t.Log(body)

expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="exchange"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="node"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="overview"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="queue"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="connections"} 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="exchange",node="my-rabbit@ae74c041248b"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="node",node="my-rabbit@ae74c041248b"} 1`)
//expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="overview",node="my-rabbit@ae74c041248b"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="queue",node="my-rabbit@ae74c041248b"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="connections",node="my-rabbit@ae74c041248b"} 1`)

// overview
expectSubstring(t, body, `rabbitmq_exchanges{cluster="my-rabbit@ae74c041248b"} 8`)
Expand Down Expand Up @@ -339,12 +340,12 @@ func TestResetMetricsOnRabbitFailure(t *testing.T) {
}
body := w.Body.String()

expectSubstring(t, body, `rabbitmq_up 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="exchange"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="node"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="overview"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{module="queue"} 0`) //down
expectSubstring(t, body, `rabbitmq_module_up{module="connections"} 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="exchange",node="my-rabbit@ae74c041248b"} 1`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="node",node="my-rabbit@ae74c041248b"} 1`)
//expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="overview",node="my-rabbit@ae74c041248b"} 1`) //TODO: value not available on first scrape
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="queue",node="my-rabbit@ae74c041248b"} 0`) //down
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="connections",node="my-rabbit@ae74c041248b"} 1`)

// overview
expectSubstring(t, body, `rabbitmq_exchanges{cluster="my-rabbit@ae74c041248b"} 8`)
Expand Down Expand Up @@ -382,12 +383,12 @@ func TestResetMetricsOnRabbitFailure(t *testing.T) {
}
body := w.Body.String()

expectSubstring(t, body, `rabbitmq_up 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="exchange"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="node"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="overview"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="queue"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{module="connections"} 0`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="exchange",node="my-rabbit@ae74c041248b"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="node",node="my-rabbit@ae74c041248b"} 0`)
//expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="overview",node="my-rabbit@ae74c041248b"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="queue",node="my-rabbit@ae74c041248b"} 0`)
expectSubstring(t, body, `rabbitmq_module_up{cluster="my-rabbit@ae74c041248b",module="connections",node="my-rabbit@ae74c041248b"} 0`)

// overview
dontExpectSubstring(t, body, `rabbitmq_exchangesTotal`)
Expand Down Expand Up @@ -441,7 +442,7 @@ func TestQueueState(t *testing.T) {
body := w.Body.String()
t.Log(body)

expectSubstring(t, body, `rabbitmq_up 1`)
expectSubstring(t, body, `rabbitmq_up{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b"} 1`)

// queue
expectSubstring(t, body, `rabbitmq_queue_state{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",state="flow",vhost="/"} 1`)
Expand Down

0 comments on commit 5d38b91

Please sign in to comment.