Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the "system_replicas_is_session_expired" metric to the exporter. #187

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions pkg/apis/metrics/clickhouse_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (
)

const (
querySystemReplicasSQL = `
SELECT
database,
table,
toString(is_session_expired) AS is_session_expired
FROM system.replicas`

queryMetricsSQL = `
SELECT
concat('metric.', metric) AS metric,
Expand Down Expand Up @@ -110,16 +117,14 @@ func (f *ClickHouseFetcher) newConn() *clickhouse.Conn {
func (f *ClickHouseFetcher) clickHouseQueryMetrics() ([][]string, error) {
data := make([][]string, 0)
conn := f.newConn()
if rows, err := conn.Query(heredoc.Doc(queryMetricsSQL)); err != nil {
rows, err := conn.Query(heredoc.Doc(queryMetricsSQL))
if err != nil {
return nil, err
} else {
for rows.Next() {
var metric, value, description, _type string
if err := rows.Scan(&metric, &value, &description, &_type); err == nil {
data = append(data, []string{metric, value, description, _type})
} else {
// Skip erroneous line
}
}
for rows.Next() {
var metric, value, description, _type string
if err := rows.Scan(&metric, &value, &description, &_type); err == nil {
data = append(data, []string{metric, value, description, _type})
}
}
return data, nil
Expand All @@ -130,16 +135,32 @@ func (f *ClickHouseFetcher) clickHouseQueryMetrics() ([][]string, error) {
func (f *ClickHouseFetcher) clickHouseQueryTableSizes() ([][]string, error) {
data := make([][]string, 0)
conn := f.newConn()
if rows, err := conn.Query(heredoc.Doc(queryTableSizesSQL)); err != nil {
rows, err := conn.Query(heredoc.Doc(queryTableSizesSQL))
if err != nil {
return nil, err
} else {
for rows.Next() {
var database, table, partitions, parts, bytes, uncompressed, _rows string
if err := rows.Scan(&database, &table, &partitions, &parts, &bytes, &uncompressed, &_rows); err == nil {
data = append(data, []string{database, table, partitions, parts, bytes, uncompressed, _rows})
} else {
// Skip erroneous line
}
}
for rows.Next() {
var database, table, partitions, parts, bytes, uncompressed, _rows string
if err := rows.Scan(&database, &table, &partitions, &parts, &bytes, &uncompressed, &_rows); err == nil {
data = append(data, []string{database, table, partitions, parts, bytes, uncompressed, _rows})
}
}
return data, nil
}

// clickHouseQuerySystemReplicas requests replica information from the ClickHouse database using REST interface
// data is a concealed output
func (f *ClickHouseFetcher) clickHouseQuerySystemReplicas() ([][]string, error) {
data := make([][]string, 0)
conn := f.newConn()
rows, err := conn.Query(heredoc.Doc(querySystemReplicasSQL))
if err != nil {
return nil, err
}
for rows.Next() {
var database, table, isSessionExpired string
if err := rows.Scan(&database, &table, &isSessionExpired); err == nil {
data = append(data, []string{database, table, isSessionExpired})
}
}
return data, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/metrics/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,15 @@ func (e *Exporter) collectFromHost(chi *WatchedChi, hostname string, c chan<- pr
e.enqueueToRemoveFromWatched(chi)
return
}

glog.Infof("Querying system replicas for %s\n", hostname)
if systemReplicas, err := fetcher.clickHouseQuerySystemReplicas(); err == nil {
glog.Infof("Extracted %d system replicas for %s\n", len(systemReplicas), hostname)
writer.WriteSystemReplicas(systemReplicas)
} else {
// In case of an error fetching data from clickhouse store CHI name in e.cleanup
glog.Infof("Error querying system replicas for %s: %s\n", hostname, err)
e.enqueueToRemoveFromWatched(chi)
return
}
}
17 changes: 13 additions & 4 deletions pkg/apis/metrics/prometheus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package metrics

import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"strconv"
"strings"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)

const (
Expand All @@ -44,7 +45,7 @@ func NewPrometheusWriter(
}
}

// writeMetricsDataToPrometheus pushes set of prometheus.Metric objects created from the ClickHouse system data
// WriteMetrics pushes set of prometheus.Metric objects created from the ClickHouse system data
// Expected data structure: metric, value, description, type (gauge|counter)
// TODO add namespace handling. It is just skipped for now
func (w *PrometheusWriter) WriteMetrics(data [][]string) {
Expand All @@ -70,7 +71,7 @@ func (w *PrometheusWriter) WriteMetrics(data [][]string) {
}
}

// writeTableSizesDataToPrometheus pushes set of prometheus.Metric objects created from the ClickHouse system data
// WriteTableSizes pushes set of prometheus.Metric objects created from the ClickHouse system data
// Expected data structure: database, table, partitions, parts, bytes, uncompressed_bytes, rows
// TODO add namespace handling. It is just skipped for now
func (w *PrometheusWriter) WriteTableSizes(data [][]string) {
Expand All @@ -96,6 +97,14 @@ func (w *PrometheusWriter) WriteTableSizes(data [][]string) {
}
}

func (w *PrometheusWriter) WriteSystemReplicas(data [][]string) {
for _, metric := range data {
writeSingleMetricToPrometheus(w.out, "system_replicas_is_session_expired", "Number of expired Zookeeper sessions of the table", metric[2], prometheus.GaugeValue,
[]string{"chi", "hostname", "database", "table"},
w.chi.Name, w.hostname, metric[0], metric[1])
}
}

func writeSingleMetricToPrometheus(out chan<- prometheus.Metric, name string, desc string, value string, metricType prometheus.ValueType, labels []string, labelValues ...string) {
floatValue, _ := strconv.ParseFloat(value, 64)
m, err := prometheus.NewConstMetric(
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/metrics/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// startMetricsExporter start Prometheus metrics exporter in background
// StartMetricsREST start Prometheus metrics exporter in background
func StartMetricsREST(
username, password string, port int,
metricsAddress, metricsPath string,
Expand Down