Skip to content

Commit 0001728

Browse files
authored
Merge pull request #126 from Arnoways/dev/arnoways/allow_zero_values
Skip metric update if no values are defined in configuration
2 parents 25f9333 + 7f49e59 commit 0001728

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353

5454
// Those are the default buckets
5555
DefaultQueryDurationHistogramBuckets = prometheus.DefBuckets
56-
// To make the buckets configurable let's init it after loading the configuration.
56+
// To make the buckets configurable lets init it after loading the configuration.
5757
queryDurationHistogram *prometheus.HistogramVec
5858
)
5959

@@ -174,8 +174,8 @@ type Query struct {
174174
Name string `yaml:"name"` // the prometheus metric name
175175
Help string `yaml:"help"` // the prometheus metric help text
176176
Labels []string `yaml:"labels"` // expose these columns as labels per gauge
177-
Values []string `yaml:"values"` // expose each of these as an gauge
177+
Values []string `yaml:"values"` // expose each of these as a gauge
178178
Timestamp string `yaml:"timestamp"` // expose as metric timestamp
179179
Query string `yaml:"query"` // a literal query
180-
QueryRef string `yaml:"query_ref"` // references an query in the query map
180+
QueryRef string `yaml:"query_ref"` // references a query in the query map
181181
}

query.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func (q *Query) Run(conn *connection) error {
7979

8080
// updateMetrics parses the result set and returns a slice of const metrics
8181
func (q *Query) updateMetrics(conn *connection, res map[string]interface{}) ([]prometheus.Metric, error) {
82+
// if no value were defined to be parsed, return immediately
83+
if len(q.Values) == 0 {
84+
level.Debug(q.log).Log("msg", "No values defined in configuration, skipping metric update")
85+
return nil, nil
86+
}
8287
updated := 0
8388
metrics := make([]prometheus.Metric, 0, len(q.Values))
8489
for _, valueName := range q.Values {
@@ -172,7 +177,7 @@ func (q *Query) updateMetric(conn *connection, res map[string]interface{}, value
172177
labels = append(labels, conn.user)
173178
labels = append(labels, valueName)
174179
// create a new immutable const metric that can be cached and returned on
175-
// every scrape. Remember that the order of the lable values in the labels
180+
// every scrape. Remember that the order of the label values in the labels
176181
// slice must match the order of the label names in the descriptor!
177182
metric, err := prometheus.NewConstMetric(
178183
q.desc, prometheus.GaugeValue, value, labels...,

0 commit comments

Comments
 (0)