Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ The metrics are by default exposed at `/metrics`.
curl localhost:9412/metrics
```

## Configuration
## Exporter configuration

The exporter is looking for a configuration in `~/.mongodb_query_exporter/config.yaml` and `/etc/mongodb_query_exporter/config.yaml` or if set the path from the env `MDBEXPORTER_CONFIG`.

Expand All @@ -105,7 +105,7 @@ Note if you have multiple MongoDB servers you can inject an env variable for eac
2. `MDBEXPORTER_SERVER_1_MONGODB_URI=mongodb://srv2:27017`
3. ...

### Configure your metrics
## Configure metrics

Since the v1.0.0 release you should use the config version v3.0 to profit from the latest features.
See the configuration version matrix bellow.
Expand Down Expand Up @@ -220,6 +220,35 @@ aggregations:

See more examples in the `/example` folder.

### Info metrics

By defining no actual value field but set `overrideEmpty` to `true` a metric can sill be exported
with labels from the aggregation pipeline but the value is set to a static value taken from `emptyValue`.
This is useful for exporting info metrics which can later be used for join queries.

```yaml
servers:
- name: main
uri: mongodb://localhost:27017
aggregations:
- database: mydb
collection: objects
metrics:
- name: myapp_info
help: 'Info metric'
overrideEmpty: true
emptyValue: 1
labels:
- mylabel1
- mylabel2
constLabels:
region: eu-central-1
cache: 0
mode: pull
pipeline: `...`
```


## Supported config versions

| Config version | Supported since |
Expand Down
12 changes: 11 additions & 1 deletion internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,17 @@ func (c *Collector) aggregate(aggregation *Aggregation, srv *server, ch chan<- p
}

func createMetric(srv *server, metric *Metric, result AggregationResult) (prometheus.Metric, error) {
value, err := metric.getValue(result)
var (
value float64
err error
)

if metric.Value == "" && metric.OverrideEmpty {
value = float64(metric.EmptyValue)
} else {
value, err = metric.getValue(result)
}

if err != nil {
return nil, err
}
Expand Down
41 changes: 41 additions & 0 deletions internal/collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,47 @@ func TestInitializeMetrics(t *testing.T) {
simple_gauge_label{foo="bar",server="main"} 1
`,
},
{
name: "Empty result and overrideEmpty is not set results in no metric",
aggregation: &Aggregation{
Metrics: []*Metric{
{
Name: "no_result",
Type: "gauge",
Help: "foobar",
Value: "total",
Labels: []string{"foo"},
},
},
Pipeline: "[{\"$match\":{\"foo\":\"bar\"}}]",
},
docs: []interface{}{},
expected: ``,
},
{
name: "Metric without a value but overrideEmpty is still created",
aggregation: &Aggregation{
Metrics: []*Metric{
{
Name: "simple_info_metric",
Type: "gauge",
Help: "foobar",
OverrideEmpty: true,
EmptyValue: 1,
Labels: []string{"foo"},
},
},
Pipeline: "[{\"$match\":{\"foo\":\"bar\"}}]",
},
docs: []interface{}{AggregationResult{
"foo": "bar",
}},
expected: `
# HELP simple_info_metric foobar
# TYPE simple_info_metric gauge
simple_info_metric{foo="bar",server="main"} 1
`,
},
{
name: "Export multiple metrics from the same aggregation",
aggregation: &Aggregation{
Expand Down