From 3f7c0f444d9f29f9e30edf30a2edc97b97aec6d9 Mon Sep 17 00:00:00 2001 From: f1yegor Date: Tue, 8 Nov 2016 11:13:38 +0100 Subject: [PATCH] add asynchronous_metrics table; update prometheus version --- clickhouse_exporter.go | 51 +++++++++++++++++++++++++------------ clickhouse_exporter_test.go | 4 +-- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/clickhouse_exporter.go b/clickhouse_exporter.go index c412084..4f82635 100644 --- a/clickhouse_exporter.go +++ b/clickhouse_exporter.go @@ -30,10 +30,11 @@ var ( // Exporter collects clickhouse stats from the given URI and exports them using // the prometheus metrics package. type Exporter struct { - metricsURI string - eventsURI string - mutex sync.RWMutex - client *http.Client + metricsURI string + asyncMetricsURI string + eventsURI string + mutex sync.RWMutex + client *http.Client scrapeFailures prometheus.Counter @@ -44,8 +45,9 @@ type Exporter struct { // NewExporter returns an initialized Exporter. func NewExporter(uri string) *Exporter { return &Exporter{ - metricsURI: uri + "?query=" + url.QueryEscape("select * from system.metrics"), - eventsURI: uri + "?query=" + url.QueryEscape("select * from system.events"), + metricsURI: uri + "?query=" + url.QueryEscape("select * from system.metrics"), + asyncMetricsURI: uri + "?query=" + url.QueryEscape("select * from system.asynchronous_metrics"), + eventsURI: uri + "?query=" + url.QueryEscape("select * from system.events"), scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{ Namespace: namespace, Name: "exporter_scrape_failures_total", @@ -93,28 +95,40 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error { for _, m := range metrics { newMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, - Name: toSnake(m.key), + Name: metricName(m.key), Help: "Number of " + m.key + " currently processed", }, []string{}).WithLabelValues() newMetric.Set(float64(m.value)) newMetric.Collect(ch) - //e.gauges = append(e.gauges, newMetric) } - events, err := e.parseResponse(e.eventsURI) + asyncMetrics, err := e.parseResponse(e.asyncMetricsURI) if err != nil { - return fmt.Errorf("Error scraping clickhouse url %v: %v", e.eventsURI, err) + return fmt.Errorf("Error scraping clickhouse url %v: %v", e.asyncMetricsURI, err) } - for _, ev := range events { - newMetric := prometheus.NewCounterVec(prometheus.CounterOpts{ + for _, am := range asyncMetrics { + newMetric := prometheus.NewGaugeVec(prometheus.GaugeOpts{ Namespace: namespace, - Name: toSnake(ev.key) + "_total", - Help: "Number of " + ev.key + " total processed", + Name: metricName(am.key), + Help: "Number of " + am.key + " async processed", }, []string{}).WithLabelValues() - newMetric.Set(float64(ev.value)) + newMetric.Set(float64(am.value)) newMetric.Collect(ch) - //e.counters = append(e.counters, newMetric) + } + + events, err := e.parseResponse(e.eventsURI) + if err != nil { + return fmt.Errorf("Error scraping clickhouse url %v: %v", e.eventsURI, err) + } + + for _, ev := range events { + newMetric, _ := prometheus.NewConstMetric( + prometheus.NewDesc( + metricName(ev.key)+"_total", + "Number of "+ev.key+" total processed", []string{}, nil), + prometheus.CounterValue, float64(ev.value)) + ch <- newMetric } return nil } @@ -192,6 +206,11 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { return } +func metricName(in string) string { + out := toSnake(in) + return strings.Replace(out, ".", "_", -1) +} + // toSnake convert the given string to snake case following the Golang format: // acronyms are converted to lower-case and preceded by an underscore. func toSnake(in string) string { diff --git a/clickhouse_exporter_test.go b/clickhouse_exporter_test.go index c09fed3..535ce55 100644 --- a/clickhouse_exporter_test.go +++ b/clickhouse_exporter_test.go @@ -46,8 +46,8 @@ func TestClickhouseStatus(t *testing.T) { defer close(ch) e.Collect(ch) }() - // because asks 2 tables - for i := 1; i <= 2*metricCount; i++ { + // because asks 3 tables + for i := 1; i <= 3*metricCount; i++ { m := <-ch if m == nil { t.Error("expected metric but got nil")