package monitor
import (
"github.com/goodrain/rainbond/mq/api/mq"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "acp_mq"
exporter = "exporter"
)
var (
scrapeDurationDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "collector_duration_seconds"),
"Collector time duration.",
[]string{"collector"}, nil,
)
)
type Exporter struct {
error prometheus.Gauge
totalScrapes prometheus.Counter
scrapeErrors *prometheus.CounterVec
lbPluginUp prometheus.Gauge
queueMessageNumber *prometheus.GaugeVec
mqm mq.ActionMQ
}
var healthDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, exporter, "health_status"),
"health status.",
[]string{"service_name"}, nil,
)
func NewExporter(mqm mq.ActionMQ) *Exporter {
return &Exporter{
mqm: mqm,
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrapes_total",
Help: "Total number of times Entrance was scraped for metrics.",
}),
scrapeErrors: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "scrape_errors_total",
Help: "Total number of times an error occurred scraping a Entrance.",
}, []string{"collector"}),
error: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: exporter,
Name: "last_scrape_error",
Help: "Whether the last scrape of metrics from Entrance resulted in an error (1 for error, 0 for success).",
}),
lbPluginUp: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "up",
Help: "Whether the default lb plugin is up.",
}),
queueMessageNumber: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "queue_message_number",
Help: "Message queue enqueue total.",
}, []string{"topic"}),
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
metricCh := make(chan prometheus.Metric)
doneCh := make(chan struct{})
go func() {
for m := range metricCh {
ch <- m.Desc()
}
close(doneCh)
}()
e.Collect(metricCh)
close(metricCh)
<-doneCh
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.scrape(ch)
ch <- e.totalScrapes
ch <- e.error
e.scrapeErrors.Collect(ch)
for _, topic := range e.mqm.GetAllTopics() {
e.queueMessageNumber.WithLabelValues(topic).Set(float64(e.mqm.MessageQueueSize(topic)))
}
e.queueMessageNumber.Collect(ch)
}
func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
e.totalScrapes.Inc()
ch <- prometheus.MustNewConstMetric(healthDesc, prometheus.GaugeValue, 1, "mq")
}