@@ -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
5353type 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
7173var (
@@ -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 ()
0 commit comments