Skip to content

Commit 4a5a7b4

Browse files
authored
Integration tests and v1.0.0-beta5 features (#13)
1 parent c871a06 commit 4a5a7b4

File tree

27 files changed

+1528
-1090
lines changed

27 files changed

+1528
-1090
lines changed

.github/workflows/action.yml

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ jobs:
88
steps:
99
- name: Checkout code
1010
uses: actions/checkout@v2
11-
- uses: actions/setup-go@v1
11+
- uses: actions/setup-go@v2.1.3
1212
with:
1313
go-version: '1.15'
1414
- name: Get the version
1515
id: get_version
1616
run: echo ::set-output name=VERSION::${GITHUB_REF##*/}
17-
- name: Unit tests & coverage
18-
run: |
19-
make test build
20-
- name: Full build & docker image
17+
- name: Unit tests
18+
run: make deps vet format unittest
19+
- name: Build binary
20+
run: make build
21+
- name: Integration tests
22+
run: make integrationtest
23+
- name: Build docker image
2124
run: make docker DOCKER_IMAGE_TAG=${{ steps.get_version.outputs.VERSION }}
22-
- uses: shogo82148/actions-goveralls@v1
25+
- name: Send go coverage report
26+
uses: shogo82148/actions-goveralls@v1
2327
with:
2428
path-to-profile: coverage.out
2529
- name: Create beta Release
@@ -66,24 +70,9 @@ jobs:
6670
asset_path: ./mongodb_query_exporter
6771
asset_name: mongodb_query_exporter
6872
asset_content_type: application/octet-stream
69-
- name: Set up QEMU
70-
uses: docker/setup-qemu-action@v1
71-
- name: Set up Docker Buildx
72-
uses: docker/setup-buildx-action@v1
73-
- name: Login to DockerHub
74-
uses: docker/login-action@v1
75-
if: startsWith(github.ref, 'refs/tags/v')
73+
- name: Publish to DockerHub
74+
uses: elgohr/Publish-Docker-Github-Action@master
7675
with:
76+
name: raffis/mongodb-query-exporter:${{ steps.get_version.outputs.VERSION }}
7777
username: ${{ secrets.DOCKER_USERNAME }}
7878
password: ${{ secrets.DOCKER_PASSWORD }}
79-
- name: Push to Docker Hub
80-
uses: docker/build-push-action@v2
81-
if: startsWith(github.ref, 'refs/tags/v')
82-
with:
83-
push: true
84-
tags: raffis/mongodb-query-exporter:${{ steps.get_version.outputs.VERSION }}
85-
- name: Run helm lint
86-
id: lint
87-
uses: WyriHaximus/github-action-helm3@v2
88-
with:
89-
exec: helm lint chart/

.github/workflows/chart.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Chart
2+
on: push
3+
4+
jobs:
5+
chart:
6+
name: Chart
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Checkout code
10+
uses: actions/checkout@v2
11+
- name: Run helm lint
12+
id: lint
13+
uses: WyriHaximus/github-action-helm3@v2
14+
with:
15+
exec: helm lint chart/prometheus-mongodb-query-exporter

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
## Features
66
* Support for multiple MongoDB servers
7-
* Support to configure custom tls certificate
87
* Support for versioned configurations
9-
* GO public API
8+
* Redesigned GO public API
9+
* New log implementation, the default is now json log format
10+
* Added new counter metric for internal stats about quries `mongodb_query_exporter_query_total`
11+
* Added root hint to go to /metrics
1012

1113
## Changes
14+
* Dropped interval pulling mechanism, pull is now synchronous while getting called by an http request
15+
* Drop support for counters (use gauge instead)
16+
* No internal tracking of upstreams anymore, using prometheus ConstMetric
1217
* go 1.15 update
1318

1419
## Packaging

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ADD . /go/src/github.com/raffis/mongodb-query-exporter
44
WORKDIR /go/src/github.com/raffis/mongodb-query-exporter
55

66

7-
RUN make all
7+
RUN make deps vet format build unittest
88

99
FROM gcr.io/distroless/base
1010
COPY --from=builder /go/src/github.com/raffis/mongodb-query-exporter/mongodb_query_exporter /bin/mongodb_query_exporter

Makefile

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
GO := go
1515
GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
1616
PROMU := $(GOPATH)/bin/promu
17-
pkgs = $(shell $(GO) list ./... | grep -v /vendor/)
17+
pkgs := $(shell $(GO) list ./... | grep -v /vendor/)
18+
units := $(shell $(GO) list ./... | grep -v /vendor/ | grep -v cmd)
19+
integrations := $(shell $(GO) list ./... | grep cmd)
1820

1921
PREFIX ?= $(shell pwd)
2022
BIN_DIR ?= $(shell pwd)
@@ -28,9 +30,15 @@ style:
2830
@echo ">> checking code style"
2931
@! gofmt -d $(shell find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'
3032

31-
test:
32-
@echo ">> running tests"
33-
@$(GO) test -short -race -v -coverprofile=coverage.out $(pkgs)
33+
test: unittest integrationtest
34+
35+
unittest:
36+
@echo ">> running unit tests"
37+
@$(GO) test -short -race -v -coverprofile=coverage.out $(units)
38+
39+
integrationtest:
40+
@echo ">> running integration tests"
41+
@$(GO) test -short -race -v $(integrations)
3442

3543
deps:
3644
@echo ">> install dependencies"

README.md

Lines changed: 121 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Prometheus MongoDB query exporter
2-
![.github/workflows/action.yml](https://github.com/raffis/mongodb-query-exporter/workflows/.github/workflows/action.yml/badge.svg)
2+
[![.github/workflows/action.yml](https://github.com/raffis/mongodb-query-exporter/workflows/.github/workflows/action.yml/badge.svg)](https://github.com/raffis/mongodb-query-exporter/actions)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/raffis/mongodb-query-exporter)](https://goreportcard.com/report/github.com/raffis/mongodb-query-exporter)
44
[![PkgGoDev](https://pkg.go.dev/badge/github.com/raffis/mongodb-query-exporter?tab=subdirectories)](https://pkg.go.dev/github.com/raffis/mongodb-query-exporter?tab=subdirectories)
55
[![Coverage Status](https://coveralls.io/repos/github/raffis/mongodb-query-exporter/badge.svg?branch=master)](https://coveralls.io/github/raffis/mongodb-query-exporter?branch=master)
@@ -9,11 +9,12 @@ MongoDB aggregation query exporter for [Prometheus](https://prometheus.io).
99

1010
## Features
1111

12-
* Support for gauge and counter metrics
12+
* Support for gauge metrics
1313
* Multiple metrics for different db/collections
1414
* Pull and Push (Push is only supported for MongoDB >= 3.6)
1515
* Supports multiple MongoDB servers
1616
* Public API for Golang
17+
* Metric caching support
1718

1819
Note that this is not designed to be a replacement for the [MongoDB exporter](https://github.com/percona/mongodb_exporter) to instrument MongoDB internals. This application exports custom MongoDB metrics in the prometheus format based on the queries (aggregations) you want.
1920

@@ -28,6 +29,12 @@ Get Prometheus MongoDB aggregation query exporter, either as a [binary](https://
2829
### Helm Chart
2930
For kubernetes users there is an official helm chart for the MongoDB query exporter.
3031

32+
Install the chart (Note only helm 3 is supported):
33+
```
34+
helm repo add mongodb-query-exporter
35+
helm install mongodb-query-exporter mongodb-query-exporter/mongodb-query-exporter
36+
```
37+
3138
## Usage
3239

3340
```
@@ -86,8 +93,8 @@ You may also use env variables to configure the exporter:
8693

8794
Note if you have multiple collectors you can inject an env variable for the MongoDB connection URI like:
8895

89-
1. `MDBEXPORTER_COLLECTOR_0_MONGODB_URI=mongodb://srv1:27017`
90-
2. `MDBEXPORTER_COLLECTOR_1_MONGODB_URI=mongodb://srv2:27017`
96+
1. `MDBEXPORTER_SERVER_0_MONGODB_URI=mongodb://srv1:27017`
97+
2. `MDBEXPORTER_SERVER_1_MONGODB_URI=mongodb://srv2:27017`
9198
3. ...
9299

93100
### Format v2.0
@@ -105,63 +112,67 @@ log:
105112
level: info
106113
development: false
107114
disableCaller: false
108-
collectors:
109-
- mongodb:
110-
uri: mongodb://localhost:27017
111-
queryTimeout: 10
112-
maxConnection: 3
113-
defaultInterval: 5
114-
metrics:
115-
- name: myapp_example_simplevalue_total
116-
type: gauge
117-
help: 'Simple gauge metric'
118-
value: total
119-
labels: []
120-
mode: pull
121-
interval: 10
122-
constLabels: []
123-
database: mydb
124-
collection: objects
125-
pipeline: |
126-
[
127-
{"$count":"total"}
128-
]
129-
- name: myapp_example_processes_total
130-
type: gauge
131-
help: 'The total number of processes in a job queue'
132-
value: total
133-
mode: push
134-
labels: [type,status]
135-
constLabels: []
136-
database: mydb
137-
collection: queue
138-
pipeline: |
139-
[
140-
{"$group": {
141-
"_id":{"status":"$status","name":"$class"},
142-
"total":{"$sum":1}
143-
}},
144-
{"$project":{
145-
"_id":0,
146-
"type":"$_id.name",
147-
"total":"$total",
148-
"status": {
149-
"$switch": {
150-
"branches": [
151-
{ "case": { "$eq": ["$_id.status", 0] }, "then": "waiting" },
152-
{ "case": { "$eq": ["$_id.status", 1] }, "then": "postponed" },
153-
{ "case": { "$eq": ["$_id.status", 2] }, "then": "processing" },
154-
{ "case": { "$eq": ["$_id.status", 3] }, "then": "done" },
155-
{ "case": { "$eq": ["$_id.status", 4] }, "then": "failed" },
156-
{ "case": { "$eq": ["$_id.status", 5] }, "then": "canceled" },
157-
{ "case": { "$eq": ["$_id.status", 6] }, "then": "timeout" }
158-
],
159-
"default": "unknown"
160-
}}
161-
}}
162-
]
115+
global:
116+
queryTimeout: 10
117+
maxConnection: 3
118+
defaultCache: 0
119+
servers:
120+
- name: main
121+
uri: mongodb://localhost:27017
122+
metrics:
123+
- name: myapp_example_simplevalue_total
124+
type: gauge #Can also be empty, the default is gauge
125+
servers: [main] #Can also be empty, if empty the metric will be used for every server defined
126+
help: 'Simple gauge metric'
127+
value: total
128+
labels: []
129+
mode: pull
130+
cache: 0
131+
database: mydb
132+
collection: objects
133+
pipeline: |
134+
[
135+
{"$count":"total"}
136+
]
137+
- name: myapp_example_processes_total
138+
type: gauge
139+
help: 'The total number of processes in a job queue'
140+
value: total
141+
mode: push
142+
labels: [type,status]
143+
constLabels:
144+
app: foo
145+
database: mydb
146+
collection: queue
147+
pipeline: |
148+
[
149+
{"$group": {
150+
"_id":{"status":"$status","name":"$class"},
151+
"total":{"$sum":1}
152+
}},
153+
{"$project":{
154+
"_id":0,
155+
"type":"$_id.name",
156+
"total":"$total",
157+
"status": {
158+
"$switch": {
159+
"branches": [
160+
{ "case": { "$eq": ["$_id.status", 0] }, "then": "waiting" },
161+
{ "case": { "$eq": ["$_id.status", 1] }, "then": "postponed" },
162+
{ "case": { "$eq": ["$_id.status", 2] }, "then": "processing" },
163+
{ "case": { "$eq": ["$_id.status", 3] }, "then": "done" },
164+
{ "case": { "$eq": ["$_id.status", 4] }, "then": "failed" },
165+
{ "case": { "$eq": ["$_id.status", 5] }, "then": "canceled" },
166+
{ "case": { "$eq": ["$_id.status", 6] }, "then": "timeout" }
167+
],
168+
"default": "unknown"
169+
}}
170+
}}
171+
]
163172
```
164173
174+
See more examples in the `/examples` folder.
175+
165176
### Format v1.0
166177

167178
The config version v1.0 is the predescer of v2.0 and does not have support for multiple MongoDB servers
@@ -188,7 +199,6 @@ metrics:
188199
labels: []
189200
mode: pull
190201
interval: 10
191-
constLabels: []
192202
database: mydb
193203
collection: objects
194204
pipeline: |
@@ -201,7 +211,8 @@ metrics:
201211
value: total
202212
mode: push
203213
labels: [type,status]
204-
constLabels: []
214+
constLabels:
215+
app: foo
205216
database: mydb
206217
collection: queue
207218
pipeline: |
@@ -231,6 +242,55 @@ metrics:
231242
]
232243
```
233244

245+
## Cache & Push
246+
Prometheus is designed to scrape metrics (meaning pull). During each scrape the mongodb-query-exporter will evaluate all configured metrics.
247+
If you have expensive queries there is an option to cache the aggregation result by setting a cache ttl in secconds.
248+
However it is more effective to **avoid cache** and design good aggregation pipelines or use a different scrape interval or use the **push** mode.
249+
For individual metrics and/or MongoDB servers older than 3.6 it might still be a good option though.
250+
251+
Example:
252+
```yaml
253+
metrics:
254+
- name: myapp_example_simplevalue_total
255+
servers: [main]
256+
help: 'Simple gauge metric which is cached for 5min'
257+
value: total
258+
mode: pull
259+
cache: 300
260+
database: mydb
261+
collection: objects
262+
pipeline: |
263+
[
264+
{"$count":"total"}
265+
]
266+
```
267+
268+
A better approach of reducing load on the MongoDB server is the supported push mode. The push automatically caches the metric at scrape time. However the cache for a metric with mode push
269+
will be invalidated automatically if anything changes on the configured MongoDB collection. Meaning the aggregation will only be executed if there have been changes during scrape intervals.
270+
271+
>**Note**: This requires at least MongoDB 3.6.
272+
273+
Example:
274+
```yaml
275+
metrics:
276+
# With the mode push the pipeline is only executed if a change occured on the collection called objects
277+
- name: myapp_example_simplevalue_total
278+
servers: [main]
279+
help: 'Simple gauge metric'
280+
value: total
281+
mode: push
282+
database: mydb
283+
collection: objects
284+
pipeline: |
285+
[
286+
{"$count":"total"}
287+
]
288+
```
289+
290+
## Debug
291+
The mongodb-query-exporters also publishes a counter metric called `mongodb_query_exporter_query_total` which publishes query results for each configured metric.
292+
Furthermore you might increase the log level to get more insight.
293+
234294
## Go API
235295
Instead using the mongodb-query-exporter you may use the API to integrate the exporter within your go project.
236296
Please check out the [go package reference](https://pkg.go.dev/badge/github.com/raffis/mongodb-query-exporter?tab=subdirectories).

chart/prometheus-mongodb-query-exporter/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ keywords:
66
- exporter
77
- metrics
88
- mongodb
9-
- query-
109
- aggregation
10+
- query
1111
- prometheus
1212
name: prometheus-mongodb-query-exporter
1313
sources:
1414
- https://github.com/raffis/mongodb-query-exporter
15-
version: 1.0.0
15+
version: 1.0.1

chart/prometheus-mongodb-query-exporter/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Installs the [MongoDB Query Exporter](https://github.com/raffis/mongodb-query-ex
77
To install the chart with the release name `my-release`:
88

99
```console
10-
$ helm upgrade --install my-release mongodb-query-exporter/prometheus-mongodb-exporter
10+
$ helm upgrade --install my-release mongodb-query-exporter/prometheus-mongodb-exporter --set mongodb.0 mongodb://mymongodb:27017
1111
```
1212

1313
This command deploys the MongoDB Exporter with the default configuration. The [configuration](#configuration) section lists the parameters that can be configured during installation.

0 commit comments

Comments
 (0)