Skip to content

Commit 71dcbd1

Browse files
authored
Merge branch 'main' into PMM-9870-fix-collstats-indexsizes-metrics
2 parents e92b341 + dc46ed5 commit 71dcbd1

File tree

10 files changed

+147
-21
lines changed

10 files changed

+147
-21
lines changed

.github/workflows/go.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,22 @@ jobs:
4343
with:
4444
go-version-file: ${{ github.workspace }}/go.mod
4545

46-
- name: Test
46+
- name: Run tests with code coverage
4747
run: |
4848
TEST_MONGODB_IMAGE=${{ matrix.image }} make test-cluster
4949
sleep 10
50-
make test-race
50+
make test-cover
5151
make test-cluster-clean
5252
53+
54+
- name: Upload coverage results
55+
uses: codecov/codecov-action@v3
56+
with:
57+
file: cover.out
58+
flags: agent
59+
env_vars: GO_VERSION,TEST_MONGODB_IMAGE
60+
fail_ci_if_error: false
61+
5362
- name: Run debug commands on failure
5463
if: ${{ failure() }}
5564
run: |

.github/workflows/scorecard.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Scorecard
2+
on:
3+
# To guarantee Maintained check is occasionally updated. See
4+
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained
5+
schedule:
6+
- cron: "24 3 * * 1"
7+
push:
8+
branches:
9+
- main
10+
11+
# Declare default permissions as read only.
12+
permissions: read-all
13+
14+
jobs:
15+
analysis:
16+
name: Analysis
17+
runs-on: ubuntu-latest
18+
permissions:
19+
# Needed to upload the results to code-scanning dashboard.
20+
security-events: write
21+
# Needed to publish results and get a badge (see publish_results below).
22+
id-token: write
23+
24+
steps:
25+
- name: Checkout code
26+
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
27+
with:
28+
persist-credentials: false
29+
30+
- name: Run analysis
31+
uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1
32+
with:
33+
results_file: results.sarif
34+
results_format: sarif
35+
publish_results: true
36+
37+
- name: Upload results
38+
uses: actions/upload-artifact@97a0fba1372883ab732affbe8f94b823f91727db # v3.pre.node20
39+
with:
40+
name: SARIF file
41+
path: results.sarif
42+
retention-days: 5
43+
44+
# Upload the results to GitHub's code scanning dashboard (optional).
45+
- name: "Upload to code-scanning"
46+
uses: github/codeql-action/upload-sarif@v3
47+
with:
48+
sarif_file: results.sarif

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
.dist
22
.env
3+
34
.vscode/
45
.idea
6+
57
bin
68
build
79
dist
10+
11+
cover.out
812
mongodb_exporter
913
.DS_Store

Makefile

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ define TEST_ENV
5050
TEST_MONGODB_S1_PRIMARY_PORT=$(TEST_MONGODB_S1_PRIMARY_PORT) \
5151
TEST_MONGODB_S1_SECONDARY1_PORT=$(TEST_MONGODB_S1_SECONDARY1_PORT) \
5252
TEST_MONGODB_S1_SECONDARY2_PORT=$(TEST_MONGODB_S1_SECONDARY2_PORT) \
53-
TEST_MONGODB_S1_ARTBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \
53+
TEST_MONGODB_S1_ARBITER_PORT=$(TEST_MONGODB_S1_ARBITER_PORT) \
5454
TEST_MONGODB_S2_RS=$(TEST_MONGODB_S2_RS) \
5555
TEST_MONGODB_S2_PRIMARY_PORT=$(TEST_MONGODB_S2_PRIMARY_PORT) \
5656
TEST_MONGODB_S2_SECONDARY1_PORT=$(TEST_MONGODB_S2_SECONDARY1_PORT) \
5757
TEST_MONGODB_S2_SECONDARY2_PORT=$(TEST_MONGODB_S2_SECONDARY2_PORT) \
58-
TEST_MONGODB_S2_ARTBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \
58+
TEST_MONGODB_S2_ARBITER_PORT=$(TEST_MONGODB_S2_ARBITER_PORT) \
5959
TEST_MONGODB_CONFIGSVR_RS=$(TEST_MONGODB_CONFIGSVR_RS) \
6060
TEST_MONGODB_CONFIGSVR1_PORT=$(TEST_MONGODB_CONFIGSVR1_PORT) \
6161
TEST_MONGODB_CONFIGSVR2_PORT=$(TEST_MONGODB_CONFIGSVR2_PORT) \
@@ -67,7 +67,7 @@ endef
6767
env:
6868
@echo $(TEST_ENV) | tr ' ' '\n' >.env
6969

70-
init: ## Install linters.
70+
init: ## Install linters
7171
cd tools && go generate -x -tags=tools
7272

7373
build: ## Compile using plain go build
@@ -82,32 +82,34 @@ release: ## Build the binaries using goreleaser
8282

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

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

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

93-
check-license: ## Check license in headers.
93+
check-license: ## Check license in headers
9494
@go run .github/check-license.go
9595

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

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

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

107+
test-cover: env ## Run tests and collect cross-package coverage information
108+
go test -race -timeout 30s -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./...
109+
107110
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
108111
docker compose up --build -d
109112
./docker/scripts/setup-pbm.sh
110113

111114
test-cluster-clean: env ## Stops MongoDB test cluster.
112-
docker compose down --remove-orphans
113-
115+
docker compose down --remove-orphans --volumes

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
[![Build Status](https://github.com/percona/mongodb_exporter/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/percona/mongodb_exporter/actions/workflows/go.yml?query=branch%3Amain)
44
[![codecov.io Code Coverage](https://img.shields.io/codecov/c/github/percona/mongodb_exporter.svg?maxAge=2592000)](https://codecov.io/github/percona/mongodb_exporter?branch=main)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/percona/mongodb_exporter)](https://goreportcard.com/report/github.com/percona/mongodb_exporter)
6+
[![OpenSSF Scorecard](https://api.scorecard.dev/projects/github.com/percona/mongodb_exporter/badge)](https://scorecard.dev/viewer/?uri=github.com/percona/mongodb_exporter)
67
[![CLA assistant](https://cla-assistant.percona.com/readme/badge/percona/mongodb_exporter)](https://cla-assistant.percona.com/percona/mongodb_exporter)
8+
[![Forum](https://img.shields.io/badge/Forum-join-brightgreen)](https://forums.percona.com/)
79

810

911
This is the new MongoDB exporter implementation that handles ALL metrics exposed by MongoDB monitoring commands.

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ services:
176176
container_name: "mongo-cnf-2"
177177
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
178178
ports:
179-
- "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
179+
- "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
180180
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
181181
networks:
182182
- cnf-serv
@@ -185,7 +185,7 @@ services:
185185
container_name: "mongo-cnf-3"
186186
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
187187
ports:
188-
- "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
188+
- "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
189189
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
190190
networks:
191191
- cnf-serv
@@ -194,7 +194,7 @@ services:
194194
container_name: "mongo-cnf-1"
195195
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
196196
ports:
197-
- "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
197+
- "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
198198
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
199199
networks:
200200
- cnf-serv

docker/scripts/setup.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ function general_servers() {
100100
EOF
101101
}
102102

103+
103104
case $1 in
104105
cnf_servers)
105106
cnf_servers

exporter/diagnostic_data_collector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (d *diagnosticDataCollector) collect(ch chan<- prometheus.Metric) {
117117
debugResult(logger, m)
118118

119119
// MongoDB 8.0 splits the diagnostic data into multiple blocks, so we need to merge them
120-
if d.buildInfo.VersionArray[0] >= 8 { //nolint:gomnd
120+
if _, ok := m["common"]; ok {
121121
b := bson.M{}
122122
for _, mv := range m {
123123
block, ok := mv.(bson.M)

main.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,36 @@ func parseURIList(uriList []string, logger *logrus.Logger, splitCluster bool) []
276276
return URIs
277277
}
278278

279+
// buildURIManually builds the URI manually by checking if the user and password are supplied
280+
func buildURIManually(uri string, user string, password string) string {
281+
uriArray := strings.SplitN(uri, "://", 2) //nolint:mnd
282+
prefix := uriArray[0] + "://"
283+
uri = uriArray[1]
284+
285+
// IF user@pass not contained in uri AND custom user and pass supplied in arguments
286+
// DO concat a new uri with user and pass arguments value
287+
if !strings.Contains(uri, "@") && user != "" && password != "" {
288+
// add user and pass to the uri
289+
uri = fmt.Sprintf("%s:%s@%s", user, password, uri)
290+
}
291+
292+
// add back prefix after adding the user and pass
293+
uri = prefix + uri
294+
295+
return uri
296+
}
297+
279298
func buildURI(uri string, user string, password string, log *logrus.Logger) string {
280299
defaultPrefix := "mongodb://" // default prefix
281-
matchRegexp := regexp.MustCompile(`^mongodb(\+srv)?://`)
282300

283-
// Split the uri defaultPrefix if there is any
284-
if !matchRegexp.MatchString(uri) {
301+
if !strings.HasPrefix(uri, defaultPrefix) && !strings.HasPrefix(uri, "mongodb+srv://") {
285302
uri = defaultPrefix + uri
286303
}
287304
parsedURI, err := url.Parse(uri)
288305
if err != nil {
289-
log.Fatalf("Failed to parse URI %s: %v", uri, err)
290-
return uri
306+
// PMM generates URI with escaped path to socket file, so url.Parse fails
307+
// in this case we build URI manually
308+
return buildURIManually(uri, user, password)
291309
}
292310

293311
if parsedURI.User == nil && user != "" && password != "" {

main_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,48 @@ func TestBuildURI(t *testing.T) {
227227
newPassword: "yyy?!#$%^&*()_+",
228228
expect: "mongodb://xxx%3F%21%23$%25%5E&%2A%28%29_+:yyy%3F%21%23$%25%5E&%2A%28%[email protected]",
229229
},
230+
{
231+
situation: "path to socket",
232+
origin: "mongodb:///tmp/mongodb-27017.sock",
233+
newUser: "",
234+
newPassword: "",
235+
expect: "mongodb:///tmp/mongodb-27017.sock",
236+
},
237+
{
238+
situation: "path to socket with params",
239+
origin: "mongodb://username:s3cur3%20p%40$$w0r4.@%2Fvar%2Frun%2Fmongodb%2Fmongodb.sock/database?connectTimeoutMS=1000&directConnection=true&serverSelectionTimeoutMS=1000",
240+
newUser: "",
241+
newPassword: "",
242+
expect: "mongodb://username:s3cur3%20p%40$$w0r4.@%2Fvar%2Frun%2Fmongodb%2Fmongodb.sock/database?connectTimeoutMS=1000&directConnection=true&serverSelectionTimeoutMS=1000",
243+
},
244+
{
245+
situation: "path to socket with auth",
246+
origin: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
247+
newUser: "",
248+
newPassword: "",
249+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
250+
},
251+
{
252+
situation: "path to socket with auth and user params",
253+
origin: "mongodb:///tmp/mongodb-27017.sock",
254+
newUser: "xxx",
255+
newPassword: "yyy",
256+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
257+
},
258+
{
259+
situation: "path to socket without prefix",
260+
origin: "/tmp/mongodb-27017.sock",
261+
newUser: "",
262+
newPassword: "",
263+
expect: "mongodb:///tmp/mongodb-27017.sock",
264+
},
265+
{
266+
situation: "path to socket without prefix with auth",
267+
origin: "/tmp/mongodb-27017.sock",
268+
newUser: "xxx",
269+
newPassword: "yyy",
270+
expect: "mongodb://xxx:yyy@/tmp/mongodb-27017.sock",
271+
},
230272
}
231273
for _, tc := range tests {
232274
t.Run(tc.situation, func(t *testing.T) {

0 commit comments

Comments
 (0)