Skip to content

Commit

Permalink
feat:aliveness
Browse files Browse the repository at this point in the history
  • Loading branch information
Ithrael committed Mar 6, 2022
1 parent 68e7b18 commit 7897a59
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
4 changes: 3 additions & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"skip_vhost": "^$",
"include_vhost": ".*",
"rabbit_capabilities": "no_sort,bert",
"aliveness_vhost": "/",
"enabled_exporters": [
"exchange",
"node",
"overview",
"queue"
"queue",
"aliveness"
],
"timeout": 30,
"max_queues": 0
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
SkipVHost: regexp.MustCompile("^$"),
IncludeVHost: regexp.MustCompile(".*"),
RabbitCapabilities: parseCapabilities("no_sort,bert"),
AlivenessVhost: "/",
EnabledExporters: []string{"exchange", "node", "overview", "queue"},
Timeout: 30,
MaxQueues: 0,
Expand Down Expand Up @@ -58,6 +59,7 @@ type rabbitExporterConfig struct {
IncludeVHostString string `json:"include_vhost"`
RabbitCapabilitiesString string `json:"rabbit_capabilities"`
RabbitCapabilities rabbitCapabilitySet `json:"-"`
AlivenessVhost string `json:"aliveness_vhost"`
EnabledExporters []string `json:"enabled_exporters"`
Timeout int `json:"timeout"`
MaxQueues int `json:"max_queues"`
Expand Down Expand Up @@ -195,6 +197,10 @@ func initConfig() {
config.EnabledExporters = strings.Split(enabledExporters, ",")
}

if alivenessVhost := os.Getenv("ALIVENESS_VHOST"); alivenessVhost != "" {
config.AlivenessVhost = alivenessVhost
}

if timeout := os.Getenv("RABBIT_TIMEOUT"); timeout != "" {
t, err := strconv.Atoi(timeout)
if err != nil {
Expand Down
104 changes: 104 additions & 0 deletions exporter_aliveness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"

"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)

func init() {
RegisterExporter("aliveness", newExporterAliveness)
}

var (
alivenessLabels = []string{"vhost"}

alivenessGaugeVec = map[string]*prometheus.GaugeVec{
"vhost.aliveness": newGaugeVec("aliveness_test", "vhost aliveness test", alivenessLabels),
}

rabbitmqAlivenessMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "rabbitmq_aliveness_info",
Help: "A metric with value 1 status:ok else 0 labeled by aliveness test status, error, reason",
},
[]string{"status", "error", "reason"},
)
)

type exporterAliveness struct {
alivenessMetrics map[string]*prometheus.GaugeVec
alivenessInfo AlivenessInfo
}

type AlivenessInfo struct {
Status string
Error string
Reason string
}

func newExporterAliveness() Exporter {
alivenessGaugeVecActual := alivenessGaugeVec

if len(config.ExcludeMetrics) > 0 {
for _, metric := range config.ExcludeMetrics {
if alivenessGaugeVecActual[metric] != nil {
delete(alivenessGaugeVecActual, metric)
}
}
}

return &exporterAliveness{
alivenessMetrics: alivenessGaugeVecActual,
alivenessInfo: AlivenessInfo{},
}
}

func (e *exporterAliveness) Collect(ctx context.Context, ch chan<- prometheus.Metric) error {
body, contentType, err := apiRequest(config, "aliveness-test")
if err != nil {
return err
}

reply, err := MakeReply(contentType, body)
if err != nil {
return err
}

rabbitMqAlivenessData := reply.MakeMap()

e.alivenessInfo.Status, _ = reply.GetString("status")
e.alivenessInfo.Error, _ = reply.GetString("error")
e.alivenessInfo.Reason, _ = reply.GetString("reason")

rabbitmqAlivenessMetric.Reset()
var flag float64 = 0
if e.alivenessInfo.Status == "ok" {
flag = 1
}
rabbitmqAlivenessMetric.WithLabelValues(e.alivenessInfo.Status, e.alivenessInfo.Error, e.alivenessInfo.Reason).Set(flag)

log.WithField("alivenesswData", rabbitMqAlivenessData).Debug("Aliveness data")
for key, gauge := range e.alivenessMetrics {
if value, ok := rabbitMqAlivenessData[key]; ok {
log.WithFields(log.Fields{"key": key, "value": value}).Debug("Set aliveness metric for key")
gauge.WithLabelValues(e.alivenessInfo.Status).Set(value)
}
}

if ch != nil {
rabbitmqAlivenessMetric.Collect(ch)
for _, gauge := range e.alivenessMetrics {
gauge.Collect(ch)
}
}
return nil
}

func (e exporterAliveness) Describe(ch chan<- *prometheus.Desc) {
rabbitmqVersionMetric.Describe(ch)
for _, gauge := range e.alivenessMetrics {
gauge.Describe(ch)
}
}
6 changes: 6 additions & 0 deletions rabbitClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"

Expand Down Expand Up @@ -67,6 +68,11 @@ func apiRequest(config rabbitExporterConfig, endpoint string) ([]byte, string, e
args = "?sort="
}

if endpoint == "aliveness-test" {
escapeAlivenessVhost := url.QueryEscape(config.AlivenessVhost)
args = "/" + escapeAlivenessVhost
}

req, err := http.NewRequest("GET", config.RabbitURL+"/api/"+endpoint+args, nil)
if err != nil {
log.WithFields(log.Fields{"error": err, "host": config.RabbitURL}).Error("Error while constructing rabbitHost request")
Expand Down

0 comments on commit 7897a59

Please sign in to comment.