@@ -51,21 +51,23 @@ type Config struct {
51
51
// A metric defines what metric should be generated from what MongoDB aggregation.
52
52
// The pipeline configures (as JSON) the aggreation query
53
53
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
69
71
}
70
72
71
73
var (
@@ -358,10 +360,10 @@ func (c *Collector) generateMetrics(metric *Metric, srv *server, ch chan<- prome
358
360
359
361
var multierr * multierror.Error
360
362
var i int
363
+ var result = make (AggregationResult )
361
364
362
365
for cursor .Next (ctx ) {
363
366
i ++
364
- var result AggregationResult
365
367
366
368
err := cursor .Decode (& result )
367
369
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
383
385
}
384
386
385
387
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
387
405
}
388
406
389
407
return multierr .ErrorOrNil ()
0 commit comments