Skip to content

Commit 8d22b6e

Browse files
committed
fix: nil error logging for bertmap removed
1 parent 63655ff commit 8d22b6e

File tree

2 files changed

+119
-8
lines changed

2 files changed

+119
-8
lines changed

bertmap.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ func (rep *rabbitBERTReply) MakeMap() MetricMap {
4949
term := rep.objects
5050

5151
err := parseProplist(&flMap, "", term)
52-
log.WithField("error", err).Warn("Error parsing rabbitmq reply (bert, MakeMap)")
52+
if err != nil {
53+
log.WithField("error", err).Warn("Error parsing rabbitmq reply (bert, MakeMap)")
54+
}
5355
return flMap
5456
}
5557

@@ -338,6 +340,8 @@ func (rep *rabbitBERTReply) GetString(label string) (string, bool) {
338340
}
339341
return true
340342
})
341-
log.WithField("error", err).Warn("Error parsing rabbitmq reply (bert, GetString)")
343+
if err != nil {
344+
log.WithField("error", err).Warn("Error parsing rabbitmq reply (bert, GetString)")
345+
}
342346
return resValue, result
343347
}

exporter_test.go

+113-6
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ func setupServer(t *testing.T, overview, queues, exchange, nodes, connections st
4242
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4343
w.WriteHeader(http.StatusOK)
4444
w.Header().Set("Content-Type", "application/json")
45-
if r.RequestURI == "/api/overview" {
45+
if strings.HasPrefix(r.RequestURI, "/api/overview") {
4646
fmt.Fprintln(w, overview)
47-
} else if r.RequestURI == "/api/queues" {
47+
} else if strings.HasPrefix(r.RequestURI, "/api/queues") {
4848
fmt.Fprintln(w, queues)
49-
} else if r.RequestURI == "/api/exchanges" {
49+
} else if strings.HasPrefix(r.RequestURI, "/api/exchanges") {
5050
fmt.Fprintln(w, exchange)
51-
} else if r.RequestURI == "/api/nodes" {
51+
} else if strings.HasPrefix(r.RequestURI, "/api/nodes") {
5252
fmt.Fprintln(w, nodes)
53-
} else if r.RequestURI == "/api/connections" {
53+
} else if strings.HasPrefix(r.RequestURI, "/api/connections") {
5454
fmt.Fprintln(w, connections)
5555
} else {
5656
t.Errorf("Invalid request. URI=%v", r.RequestURI)
@@ -426,6 +426,113 @@ func TestResetMetricsOnRabbitFailure(t *testing.T) {
426426

427427
}
428428

429+
func TestExporter(t *testing.T) {
430+
tests := []struct {
431+
name string
432+
setup func()
433+
expect []string
434+
dontExpect []string
435+
lines int
436+
}{
437+
{
438+
name: "Base",
439+
setup: func() {},
440+
expect: []string{
441+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",vhost="/"} 6`,
442+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue3",self="1",vhost="/"} 23`,
443+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue4",self="1",vhost="vhost4"} 0`,
444+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",vhost="/"} 25`,
445+
},
446+
dontExpect: []string{},
447+
lines: 401,
448+
},
449+
{
450+
name: "Include specific queue",
451+
setup: func() {
452+
config.IncludeQueues = regexp.MustCompile("myQueue3")
453+
//config.SkipQueues = regexp.MustCompile(".*")
454+
},
455+
expect: []string{
456+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue3",self="1",vhost="/"} 23`,
457+
},
458+
dontExpect: []string{
459+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",vhost="/"} 6`,
460+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue4",self="1",vhost="vhost4"} 0`,
461+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",vhost="/"} 25`,
462+
},
463+
},
464+
{
465+
name: "IncludeQueues (Substring)",
466+
setup: func() {
467+
config.IncludeQueues = regexp.MustCompile("Queue")
468+
//config.SkipQueues = regexp.MustCompile(".*")
469+
},
470+
expect: []string{
471+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue3",self="1",vhost="/"} 23`,
472+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",vhost="/"} 6`,
473+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue4",self="1",vhost="vhost4"} 0`,
474+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",vhost="/"} 25`,
475+
},
476+
dontExpect: []string{},
477+
},
478+
{
479+
name: "Skip queues (Substring)",
480+
setup: func() {
481+
// config.IncludeQueues = regexp.MustCompile("Queue")
482+
config.SkipQueues = regexp.MustCompile("[34]")
483+
},
484+
expect: []string{
485+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",vhost="/"} 6`,
486+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",vhost="/"} 25`,
487+
`rabbitmq_queue_messages_published_total{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue1",self="1",vhost="/"} 6`,
488+
`rabbitmq_queue_messages_published_total{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",vhost="/"} 0`,
489+
},
490+
dontExpect: []string{
491+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue4",self="1",vhost="vhost4"} 0`,
492+
`rabbitmq_queue_messages_ready{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue3",self="1",vhost="/"} 23`,
493+
`rabbitmq_queue_messages_published_total{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue3",self="1",vhost="/"} 0`,
494+
`rabbitmq_queue_messages_published_total{cluster="my-rabbit@ae74c041248b",durable="true",policy="",queue="myQueue4",self="1",vhost="vhost4"} 0`,
495+
},
496+
},
497+
}
498+
499+
for _, tt := range tests {
500+
t.Run(tt.name, func(t *testing.T) {
501+
server := setupServer(t, overviewTestData, queuesTestData, exchangeAPIResponse, nodesAPIResponse, connectionAPIResponse)
502+
defer server.Close()
503+
504+
initConfig()
505+
config.RabbitURL = server.URL
506+
tt.setup()
507+
508+
exporter := newExporter()
509+
prometheus.MustRegister(exporter)
510+
defer prometheus.Unregister(exporter)
511+
512+
req, _ := http.NewRequest("GET", "", nil)
513+
w := httptest.NewRecorder()
514+
promhttp.Handler().ServeHTTP(w, req)
515+
if w.Code != http.StatusOK {
516+
t.Errorf("Home page didn't return %v", http.StatusOK)
517+
}
518+
body := w.Body.String()
519+
// t.Log(body)
520+
521+
if lines := len(strings.Split(body, "\n")); tt.lines > 0 && lines != tt.lines {
522+
t.Errorf("Unexpected number of lines. Expected %d, got %d", tt.lines, lines)
523+
}
524+
525+
for _, v := range tt.expect {
526+
expectSubstring(t, body, v)
527+
}
528+
529+
for _, v := range tt.dontExpect {
530+
dontExpectSubstring(t, body, v)
531+
}
532+
})
533+
}
534+
}
535+
429536
func TestQueueState(t *testing.T) {
430537
server := setupServer(t, overviewTestData, queuesTestData, exchangeAPIResponse, nodesAPIResponse, connectionAPIResponse)
431538
defer server.Close()
@@ -458,7 +565,7 @@ func TestQueueState(t *testing.T) {
458565
expectSubstring(t, body, `rabbitmq_queue_state{cluster="my-rabbit@ae74c041248b",durable="true",policy="ha-2",queue="myQueue2",self="1",state="idle",vhost="/"} 1`)
459566

460567
// connections
461-
expectSubstring(t, body, `rabbitmq_connection_status{cluster="my-rabbit@ae74c041248b",node="rabbit@rmq-cluster-node-04",peer_host="172.31.0.130",self="0",state="running",user="rmq_oms",vhost="/"} 1`)
568+
expectSubstring(t, body, `rabbitmq_connection_status{cluster="my-rabbit@ae74c041248b",node="rabbit@rmq-cluster-node-04",peer_host="172.31.0.1129",self="0",state="running",user="rmq_oms",vhost="/"} 1`)
462569
expectSubstring(t, body, `rabbitmq_connection_status{cluster="my-rabbit@ae74c041248b",node="my-rabbit@ae74c041248b",peer_host="172.31.0.130",self="1",state="running",user="rmq_oms",vhost="/"} 1`)
463570

464571
}

0 commit comments

Comments
 (0)