Skip to content

Commit d777ee2

Browse files
authored
Merge pull request #32 from guillaumelecerf/feature/handle-empty-resultset-as-zero
Add an option to override value for empty result set
2 parents 972cf54 + 3e58509 commit d777ee2

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

collector/collector.go

+35-17
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ type Config struct {
5151
// A metric defines what metric should be generated from what MongoDB aggregation.
5252
// The pipeline configures (as JSON) the aggreation query
5353
type Metric struct {
54-
Name string
55-
Type string
56-
Servers []string
57-
Help string
58-
Value string
59-
Cache int64
60-
ConstLabels prometheus.Labels
61-
Mode string
62-
Labels []string
63-
Database string
64-
Collection string
65-
Pipeline string
66-
desc *prometheus.Desc
67-
pipeline bson.A
68-
validUntil time.Time
54+
Name string
55+
Type string
56+
Servers []string
57+
Help string
58+
Value string
59+
OverrideEmpty bool
60+
EmptyValue int64
61+
Cache int64
62+
ConstLabels prometheus.Labels
63+
Mode string
64+
Labels []string
65+
Database string
66+
Collection string
67+
Pipeline string
68+
desc *prometheus.Desc
69+
pipeline bson.A
70+
validUntil time.Time
6971
}
7072

7173
var (
@@ -358,10 +360,10 @@ func (c *Collector) generateMetrics(metric *Metric, srv *server, ch chan<- prome
358360

359361
var multierr *multierror.Error
360362
var i int
363+
var result = make(AggregationResult)
361364

362365
for cursor.Next(ctx) {
363366
i++
364-
var result AggregationResult
365367

366368
err := cursor.Decode(&result)
367369
c.logger.Debugf("found record %s from metric %s", result, metric.Name)
@@ -383,7 +385,23 @@ func (c *Collector) generateMetrics(metric *Metric, srv *server, ch chan<- prome
383385
}
384386

385387
if i == 0 {
386-
return fmt.Errorf("metric %s aggregation returned an emtpy result set", metric.Name)
388+
if ! metric.OverrideEmpty {
389+
return fmt.Errorf("metric %s aggregation returned an empty result set", metric.Name)
390+
}
391+
392+
c.logger.Debugf("OverrideEmpty option is set for metric %s, overriding value with %d", metric.Name, metric.EmptyValue)
393+
394+
result[metric.Value] = int64(metric.EmptyValue)
395+
for _, label := range metric.Labels {
396+
result[label] = ""
397+
}
398+
m, err := createMetric(metric, result)
399+
if err != nil {
400+
return err
401+
}
402+
403+
c.updateCache(metric, srv, m)
404+
ch <- m
387405
}
388406

389407
return multierr.ErrorOrNil()

collector/collector_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ func TestInitializeMetrics(t *testing.T) {
123123
//error: "1 error occurred:\n\t* value not found in result set\n\n",
124124
expected: ``,
125125
},
126+
metricTest{
127+
name: "Unlabeled gauge no value found in result but OverrideEmpty is set with EmptyValue 0",
128+
metric: &Metric{
129+
Name: "simple_gauge_value_not_found_overriden",
130+
Type: "gauge",
131+
Help: "overridden",
132+
OverrideEmpty: true,
133+
EmptyValue: 12,
134+
Pipeline: "[{\"$match\":{\"foo\":\"bar\"}}]",
135+
},
136+
expected: `
137+
# HELP simple_gauge_value_not_found_overriden overridden
138+
# TYPE simple_gauge_value_not_found_overriden gauge
139+
simple_gauge_value_not_found_overriden 12
140+
`,
141+
},
126142
metricTest{
127143
name: "Unlabeled gauge value not of type float",
128144
metric: &Metric{

example/configv2.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ metrics:
1919
servers: [main] #Can also be empty, if empty the metric will be used for every server defined
2020
help: 'Simple gauge metric'
2121
value: total
22+
overrideEmpty: true # if an empty result set is returned..
23+
emptyValue: 0 # create a metric with value 0
2224
labels: []
2325
mode: pull
2426
cache: 0

0 commit comments

Comments
 (0)