/
prometheus_writer.go
178 lines (159 loc) · 6.45 KB
/
prometheus_writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2019 Altinity Ltd and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package metrics
import (
"strconv"
"strings"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "chi"
subsystem = "clickhouse"
)
type PrometheusWriter struct {
out chan<- prometheus.Metric
chi *WatchedChi
hostname string
}
func NewPrometheusWriter(
out chan<- prometheus.Metric,
chi *WatchedChi,
hostname string,
) *PrometheusWriter {
return &PrometheusWriter{
out: out,
chi: chi,
hostname: hostname,
}
}
// 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) {
for _, metric := range data {
if len(metric) < 2 {
continue
}
var metricType prometheus.ValueType
if metric[3] == "counter" {
metricType = prometheus.CounterValue
} else {
metricType = prometheus.GaugeValue
}
writeSingleMetricToPrometheus(w.out,
convertMetricName(metric[0]),
metric[2],
metric[1],
metricType,
[]string{"chi", "namespace", "hostname"},
w.chi.Name,
w.chi.Namespace,
w.hostname,
)
}
}
// 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) {
for _, metric := range data {
if len(metric) < 2 {
continue
}
writeSingleMetricToPrometheus(w.out, "table_partitions", "Number of partitions of the table", metric[2], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.hostname, metric[0], metric[1])
writeSingleMetricToPrometheus(w.out, "table_parts", "Number of parts of the table", metric[3], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
writeSingleMetricToPrometheus(w.out, "table_parts_bytes", "Table size in bytes", metric[4], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
writeSingleMetricToPrometheus(w.out, "table_parts_bytes_uncompressed", "Table size in bytes uncompressed", metric[5], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
writeSingleMetricToPrometheus(w.out, "table_parts_rows", "Number of rows in the table", metric[6], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
}
}
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", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
}
}
func (w *PrometheusWriter) WriteMutations(data [][]string) {
for _, metric := range data {
writeSingleMetricToPrometheus(w.out, "table_mutations", "Number of active mutations for the table", metric[2], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
writeSingleMetricToPrometheus(w.out, "table_mutations_parts_to_do", "Number of data parts that need to be mutated for the mutation to finish", metric[3], prometheus.GaugeValue,
[]string{"chi", "namespace", "hostname", "database", "table"},
w.chi.Name, w.chi.Namespace, w.hostname, metric[0], metric[1])
}
}
func (w *PrometheusWriter) WriteErrorFetch(fetch_type string) {
writeSingleMetricToPrometheus(w.out, "metric_fetch_errors", "Number of errors fetching metrics from ClickHouse", "1", prometheus.CounterValue,
[]string{"chi", "namespace", "hostname", "fetch_type"},
w.chi.Name, w.chi.Namespace, w.hostname, fetch_type)
}
func (w *PrometheusWriter) WriteOKFetch(fetch_type string) {
writeSingleMetricToPrometheus(w.out, "metric_fetches", "Number of successful metric fetches from ClickHouse", "1", prometheus.CounterValue,
[]string{"chi", "namespace", "hostname", "fetch_type"},
w.chi.Name, w.chi.Namespace, w.hostname, fetch_type)
}
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(
newDescription(name, desc, labels),
metricType,
floatValue,
labelValues...,
)
if err != nil {
glog.Infof("Error creating metric %s: %s", name, err)
return
}
select {
case out <- m:
default:
glog.Infof("Error sending metric to the channel %s", name)
}
}
// newDescription creates a new prometheus.Desc object
func newDescription(name, help string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, name),
help,
labels,
nil,
)
}
// convertMetricName converts the given string to snake case following the Golang format:
// acronyms are converted to lower-case and preceded by an underscore.
func convertMetricName(in string) string {
/*runes := []rune(in)
length := len(runes)
var out []rune
for i := 0; i < length; i++ {
if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
out = append(out, '_')
}
out = append(out, unicode.ToLower(runes[i]))
}*/
return strings.Replace(string(in), ".", "_", -1)
}