Skip to content

Commit

Permalink
add asynchronous_metrics table; update prometheus version
Browse files Browse the repository at this point in the history
  • Loading branch information
f1yegor committed Nov 8, 2016
1 parent 6c8c636 commit 3f7c0f4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
51 changes: 35 additions & 16 deletions clickhouse_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions clickhouse_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 3f7c0f4

Please sign in to comment.