Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 14 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@ jobs:
bin/golangci-lint run -c=.golangci-required.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-check
bin/golangci-lint run -c=.golangci.yml --out-format=line-number | env REVIEWDOG_GITHUB_API_TOKEN=${{ secrets.GITHUB_TOKEN }} bin/reviewdog -f=golangci-lint -level=error -reporter=github-pr-review
make check-license

- name: Run code coverage
run: |
TEST_MONGODB_IMAGE=${{ matrix.image }} make test-cluster
make test-cover
make test-cluster-clean

- name: Upload coverage results
uses: codecov/codecov-action@v3
with:
file: cover.out
flags: agent
env_vars: GO_VERSION,TEST_MONGODB_IMAGE
fail_ci_if_error: false
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
.dist
.env

.vscode/
.idea

bin
build
dist

cover.out
mongodb_exporter
25 changes: 14 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ define TEST_ENV
TEST_MONGODB_S1_PRIMARY_PORT=$(TEST_MONGODB_S1_PRIMARY_PORT) \
TEST_MONGODB_S1_SECONDARY1_PORT=$(TEST_MONGODB_S1_SECONDARY1_PORT) \
TEST_MONGODB_S1_SECONDARY2_PORT=$(TEST_MONGODB_S1_SECONDARY2_PORT) \
TEST_MONGODB_S1_ARTBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \
TEST_MONGODB_S1_ARBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \
TEST_MONGODB_S2_RS=$(TEST_MONGODB_S2_RS) \
TEST_MONGODB_S2_PRIMARY_PORT=$(TEST_MONGODB_S2_PRIMARY_PORT) \
TEST_MONGODB_S2_SECONDARY1_PORT=$(TEST_MONGODB_S2_SECONDARY1_PORT) \
TEST_MONGODB_S2_SECONDARY2_PORT=$(TEST_MONGODB_S2_SECONDARY2_PORT) \
TEST_MONGODB_S2_ARTBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \
TEST_MONGODB_S2_ARBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \
TEST_MONGODB_CONFIGSVR_RS=$(TEST_MONGODB_CONFIGSVR_RS) \
TEST_MONGODB_CONFIGSVR1_PORT=$(TEST_MONGODB_CONFIGSVR1_PORT) \
TEST_MONGODB_CONFIGSVR2_PORT=$(TEST_MONGODB_CONFIGSVR2_PORT) \
Expand All @@ -67,7 +67,7 @@ endef
env:
@echo $(TEST_ENV) | tr ' ' '\n' >.env

init: ## Install linters.
init: ## Install linters
cd tools && go generate -x -tags=tools

build: ## Compile using plain go build
Expand All @@ -81,30 +81,33 @@ release: ## Build the binaries using goreleaser

FILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*")

format: ## Format source code.
format: ## Format source code
go mod tidy
bin/gofumpt -l -w $(FILES)
bin/gci write --section Standard --section Default --section "Prefix(github.com/percona/mongodb_exporter)" .

check: ## Run checks/linters
bin/golangci-lint run

check-license: ## Check license in headers.
check-license: ## Check license in headers
@go run .github/check-license.go

help: ## Display this help message.
help: ## Display this help message
@echo "Please use \`make <target>\` where <target> is one of:"
@grep '^[a-zA-Z]' $(MAKEFILE_LIST) | \
awk -F ':.*?## ' 'NF==2 {printf " %-26s%s\n", $$1, $$2}'

test: env ## Run all tests.
test: env ## Run all tests
go test -v -count 1 -timeout 30s ./...

test-race: env ## Run all tests with race flag.
test-race: env ## Run all tests with race flag
go test -race -v -timeout 30s ./...

test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MONGODB_IMAGE to set flavor and version. Example: TEST_MONGODB_IMAGE=mongo:3.6 make test-cluster
test-cover: env ## Run tests and collect cross-package coverage information
go test -race -timeout 30s -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./...

test-cluster: env ## Starts MongoDB test cluster. Use env var TEST_MONGODB_IMAGE to set flavor and version. Example: TEST_MONGODB_IMAGE=mongo:4.4 make test-cluster
docker-compose up -d

test-cluster-clean: env ## Stops MongoDB test cluster.
docker-compose down --remove-orphans
test-cluster-clean: env ## Stops MongoDB test cluster
docker-compose down --remove-orphans --volumes --timeout 1
15 changes: 8 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ services:
container_name: "mongo-2-2"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017"
- "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017"
command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16

mongo-2-3:
container_name: "mongo-2-3"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S2_SECONDARY1_PORT:-17005}:27017"
- "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017"
command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16

mongo-2-1:
container_name: "mongo-2-1"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S2_SECONDARY2_PORT:-17006}:27017"
- "${TEST_MONGODB_S2_PRIMARY_PORT:-17004}:27017"
command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16
links:
- mongo-2-2:mongo-2-2
Expand All @@ -83,7 +83,7 @@ services:
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S2_ARBITER:-17012}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16
command: mongod --replSet rs2 --shardsvr --port 27017 --oplogSize 16

mongo-rs2-setup:
container_name: "mongo-rs2-setup"
Expand All @@ -97,6 +97,7 @@ services:
- mongo-2-1:mongo-2-1
- mongo-2-2:mongo-2-2
- mongo-2-3:mongo-2-3
- mongo-2-arbiter:mongo-2-arbiter
volumes:
- ./docker/scripts:/scripts
environment:
Expand All @@ -112,21 +113,21 @@ services:
container_name: "mongo-cnf-2"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
- "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16

mongo-cnf-3:
container_name: "mongo-cnf-3"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
- "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16

mongo-cnf-1:
container_name: "mongo-cnf-1"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
- "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
links:
- mongo-cnf-2:mongo-cnf-2
Expand Down
32 changes: 11 additions & 21 deletions docker/scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#!/bin/bash

mongodb1=`getent hosts ${MONGO1} | awk '{ print $1 }'`
mongodb2=`getent hosts ${MONGO2} | awk '{ print $1 }'`
mongodb3=`getent hosts ${MONGO3} | awk '{ print $1 }'`
arbiter=`getent hosts ${ARBITER} | awk '{ print $1 }'`

port=${PORT:-27017}

echo "Waiting for startup.."
until mongo --host ${mongodb1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
until mongo --host ${MONGO1}:${port} --eval 'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)' &>/dev/null; do
printf '.'
sleep 1
done
Expand All @@ -20,23 +15,23 @@ echo setup.sh time now: `date +"%T" `

function cnf_servers() {
echo "setup cnf servers"
mongo --host ${mongodb1}:${port} <<EOF
mongo --host ${MONGO1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"protocolVersion": 1,
"configsvr": true,
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}"
"host": "${MONGO1}:${port}"
},
{
"_id": 1,
"host": "${mongodb2}:${port}"
"host": "${MONGO2}:${port}"
},
{
"_id": 2,
"host": "${mongodb3}:${port}"
"host": "${MONGO3}:${port}"
}
]
};
Expand All @@ -47,32 +42,27 @@ EOF

function general_servers() {
echo "setup servers"
mongo --host ${mongodb1}:${port} <<EOF
mongo --host ${MONGO1}:${port} <<EOF
var cfg = {
"_id": "${RS}",
"protocolVersion": 1,
"members": [
{
"_id": 0,
"host": "${mongodb1}:${port}"
},
{
"_id": 1,
"host": "${mongodb2}:${port}"
},
{
"_id": 2,
"host": "${mongodb3}:${port}"
"host": "${MONGO1}:${port}"
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });

rs.addArb("${arbiter}:${port}")
rs.add({host:"${MONGO2}", arbiterOnly:false})
rs.add({host:"${MONGO3}", arbiterOnly:false})
rs.addArb("${ARBITER}:${port}")
EOF
}


case $1 in
cnf_servers)
cnf_servers
Expand Down
4 changes: 2 additions & 2 deletions exporter/topology_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ func TestTopologyLabels(t *testing.T) {
{
containerName: "mongo-1-arbiter",
want: map[string]string{
labelReplicasetName: "",
labelReplicasetState: "0",
labelReplicasetName: "rs1",
labelReplicasetState: "7",
labelClusterRole: "shardsvr",
labelClusterID: "",
},
Expand Down