Skip to content

Commit

Permalink
Merge pull request #126 from Arnoways/dev/arnoways/allow_zero_values
Browse files Browse the repository at this point in the history
Skip metric update if no values are defined in configuration
  • Loading branch information
dewey authored Jul 11, 2024
2 parents 25f9333 + 7f49e59 commit 0001728
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (

// Those are the default buckets
DefaultQueryDurationHistogramBuckets = prometheus.DefBuckets
// To make the buckets configurable let's init it after loading the configuration.
// To make the buckets configurable lets init it after loading the configuration.
queryDurationHistogram *prometheus.HistogramVec
)

Expand Down Expand Up @@ -174,8 +174,8 @@ type Query struct {
Name string `yaml:"name"` // the prometheus metric name
Help string `yaml:"help"` // the prometheus metric help text
Labels []string `yaml:"labels"` // expose these columns as labels per gauge
Values []string `yaml:"values"` // expose each of these as an gauge
Values []string `yaml:"values"` // expose each of these as a gauge
Timestamp string `yaml:"timestamp"` // expose as metric timestamp
Query string `yaml:"query"` // a literal query
QueryRef string `yaml:"query_ref"` // references an query in the query map
QueryRef string `yaml:"query_ref"` // references a query in the query map
}
7 changes: 6 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (q *Query) Run(conn *connection) error {

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

0 comments on commit 0001728

Please sign in to comment.