Skip to content

Commit

Permalink
Merge branch 'main' into host-labels
Browse files Browse the repository at this point in the history
  • Loading branch information
BupycHuk authored Sep 19, 2024
2 parents b236ccb + 6dcda3a commit e332f24
Show file tree
Hide file tree
Showing 17 changed files with 390 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ REPO ?= percona/$(NAME)
GORELEASER_FLAGS ?=
UID ?= $(shell id -u)

export TEST_MONGODB_IMAGE?=mongo:4.2
export TEST_MONGODB_IMAGE?=mongo:4.4
export TEST_MONGODB_ADMIN_USERNAME?=
export TEST_MONGODB_ADMIN_PASSWORD?=
export TEST_MONGODB_USERNAME?=
Expand Down
26 changes: 13 additions & 13 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
services:
mongo-1-1:
container_name: "mongo-1-1"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_S1_PRIMARY_PORT:-17001}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --bind_ip 0.0.0.0
Expand All @@ -14,7 +14,7 @@ services:

mongo-1-2:
container_name: "mongo-1-2"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_S1_SECONDARY1_PORT:-17002}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --bind_ip 0.0.0.0
Expand All @@ -23,7 +23,7 @@ services:

mongo-1-3:
container_name: "mongo-1-3"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_S1_SECONDARY2_PORT:-17003}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --bind_ip 0.0.0.0
Expand All @@ -32,7 +32,7 @@ services:

mongo-1-arbiter:
container_name: "mongo-1-arbiter"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_S1_ARBITER:-17011}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16
Expand All @@ -41,7 +41,7 @@ services:

mongo-rs1-setup:
container_name: "mongo-rs1-setup"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
depends_on:
- "mongo-1-1"
- "mongo-1-2"
Expand Down Expand Up @@ -150,7 +150,7 @@ services:

mongo-rs2-setup:
container_name: "mongo-rs2-setup"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
depends_on:
- "mongo-2-1"
- "mongo-2-2"
Expand All @@ -174,7 +174,7 @@ services:
# Config servers
mongo-cnf-2:
container_name: "mongo-cnf-2"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_CONFIGSVR1_PORT:-17007}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
Expand All @@ -183,7 +183,7 @@ services:

mongo-cnf-3:
container_name: "mongo-cnf-3"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_CONFIGSVR2_PORT:-17008}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
Expand All @@ -192,7 +192,7 @@ services:

mongo-cnf-1:
container_name: "mongo-cnf-1"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_CONFIGSVR3_PORT:-17009}:27017"
command: mongod --dbpath /data/db --replSet cnf-serv --configsvr --port 27017 --oplogSize 16
Expand All @@ -204,7 +204,7 @@ services:

mongo-cnf-setup:
container_name: "mongo-cnf-setup"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
depends_on:
- "mongo-cnf-1"
- "mongo-cnf-2"
Expand All @@ -224,7 +224,7 @@ services:

mongos:
container_name: "mongos"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_MONGOS_PORT:-17000}:27017"
networks:
Expand All @@ -240,7 +240,7 @@ services:

mongo-shard-setup:
container_name: "mongo-shard-setup"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
depends_on:
- "mongos"
networks:
Expand Down Expand Up @@ -270,7 +270,7 @@ services:

standalone:
container_name: "standalone"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
image: ${TEST_MONGODB_IMAGE:-mongo:4.4}
ports:
- "${TEST_MONGODB_STANDALONE_PORT:-27017}:27017"
command: mongod --port 27017 --oplogSize 16
Expand Down
1 change: 0 additions & 1 deletion exporter/diagnostic_data_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ func TestDiagnosticDataErrors(t *testing.T) {
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
Expand Down
51 changes: 45 additions & 6 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Opts struct {
EnableCollStats bool
EnableProfile bool
EnableShards bool
EnableFCV bool // Feature Compatibility Version.

EnableOverrideDescendingIndex bool

Expand Down Expand Up @@ -131,18 +132,18 @@ func (e *Exporter) getTotalCollectionsCount() int {
func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topologyInfo labelsGetter, requestOpts Opts) *prometheus.Registry {
registry := prometheus.NewRegistry()

gc := newGeneralCollector(ctx, client, e.opts.Logger)
nodeType, err := getNodeType(ctx, client)
if err != nil {
e.logger.Errorf("Registry - Cannot get node type to check if this is a mongos : %s", err)
}

gc := newGeneralCollector(ctx, client, nodeType, e.opts.Logger)
registry.MustRegister(gc)

if client == nil {
return registry
}

nodeType, err := getNodeType(ctx, client)
if err != nil {
e.logger.Errorf("Registry - Cannot get node type to check if this is a mongos : %s", err)
}

// Enable collectors like collstats and indexstats depending on the number of collections
// present in the database.
limitsOk := false
Expand All @@ -165,6 +166,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
e.opts.EnableCurrentopMetrics = true
e.opts.EnableProfile = true
e.opts.EnableShards = true
e.opts.EnableFCV = true
e.opts.EnablePBMMetrics = true
}

Expand All @@ -179,6 +181,7 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
e.opts.EnableCurrentopMetrics = false
e.opts.EnableProfile = false
e.opts.EnableShards = false
e.opts.EnableFCV = false
e.opts.EnablePBMMetrics = false
}

Expand Down Expand Up @@ -240,6 +243,11 @@ func (e *Exporter) makeRegistry(ctx context.Context, client *mongo.Client, topol
registry.MustRegister(sc)
}

if e.opts.EnableFCV && nodeType != typeMongos {
fcvc := newFeatureCompatibilityCollector(ctx, client, e.opts.Logger)
registry.MustRegister(fcvc)
}

if e.opts.EnablePBMMetrics && requestOpts.EnablePBMMetrics {
pbmc := newPbmCollector(ctx, client, e.opts.URI, e.opts.Logger)
registry.MustRegister(pbmc)
Expand Down Expand Up @@ -295,6 +303,37 @@ func (e *Exporter) Handler() http.Handler {

requestOpts := GetRequestOpts(r.URL.Query()["collect[]"], e.opts)

if len(filters) == 0 {

Check failure on line 306 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

undefined: filters

Check failure on line 306 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

undefined: filters

Check failure on line 306 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

undefined: filters

Check failure on line 306 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

undefined: filters
requestOpts = *e.opts
}

for _, filter := range filters {

Check failure on line 310 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (mongo:4.4)

undefined: filters

Check failure on line 310 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (mongo:5.0)

undefined: filters

Check failure on line 310 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:4.4)

undefined: filters

Check failure on line 310 in exporter/exporter.go

View workflow job for this annotation

GitHub Actions / Build (percona/percona-server-mongodb:5.0)

undefined: filters
switch filter {
case "diagnosticdata":
requestOpts.EnableDiagnosticData = true
case "replicasetstatus":
requestOpts.EnableReplicasetStatus = true
case "dbstats":
requestOpts.EnableDBStats = true
case "topmetrics":
requestOpts.EnableTopMetrics = true
case "currentopmetrics":
requestOpts.EnableCurrentopMetrics = true
case "indexstats":
requestOpts.EnableIndexStats = true
case "collstats":
requestOpts.EnableCollStats = true
case "profile":
requestOpts.EnableProfile = true
case "shards":
requestOpts.EnableShards = true
case "fcv":
requestOpts.EnableFCV = true
case "pbm":
requestOpts.EnablePBMMetrics = true
}
}

client, err = e.getClient(ctx)
if err != nil {
e.logger.Errorf("Cannot connect to MongoDB: %v", err)
Expand Down
92 changes: 39 additions & 53 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,75 +199,61 @@ func TestMongoS(t *testing.T) {
}
}

func TestMongoUp(t *testing.T) {
ctx := context.Background()

exporterOpts := &Opts{
Logger: logrus.New(),
URI: "mongodb://127.0.0.1:123456/admin",
DirectConnect: true,
GlobalConnPool: false,
CollectAll: true,
}

client, err := connect(ctx, exporterOpts)
assert.Error(t, err)

e := New(exporterOpts)

gc := newGeneralCollector(ctx, client, e.opts.Logger)

r := e.makeRegistry(ctx, client, new(labelsGetterMock), *e.opts)

res := r.Unregister(gc)
assert.Equal(t, true, res)
}

func TestMongoUpMetric(t *testing.T) {
ctx := context.Background()

type testcase struct {
URI string
Want int
name string
URI string
clusterRole string
Want int
}

testCases := []testcase{
{URI: "mongodb://127.0.0.1:12345/admin", Want: 0},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_STANDALONE_PORT", "27017")), Want: 1},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_STANDALONE_PORT", "27017")), Want: 1, clusterRole: "mongod"},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_S1_PRIMARY_PORT", "27017")), Want: 1, clusterRole: "mongod"},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_S1_SECONDARY1_PORT", "27017")), Want: 1, clusterRole: "mongod"},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_S1_ARBITER_PORT", "27017")), Want: 1, clusterRole: "arbiter"},
{URI: fmt.Sprintf("mongodb://127.0.0.1:%s/admin", tu.GetenvDefault("TEST_MONGODB_MONGOS_PORT", "27017")), Want: 1, clusterRole: "mongos"},
}

for _, tc := range testCases {
exporterOpts := &Opts{
Logger: logrus.New(),
URI: tc.URI,
ConnectTimeoutMS: 200,
DirectConnect: true,
GlobalConnPool: false,
CollectAll: true,
}
t.Run(tc.clusterRole+"/"+tc.URI, func(t *testing.T) {
exporterOpts := &Opts{
Logger: logrus.New(),
URI: tc.URI,
ConnectTimeoutMS: 200,
DirectConnect: true,
GlobalConnPool: false,
CollectAll: true,
}

client, err := connect(ctx, exporterOpts)
if tc.Want == 1 {
assert.NoError(t, err, "Must be able to connect to %s", tc.URI)
} else {
assert.Error(t, err, "Must be unable to connect to %s", tc.URI)
}
client, err := connect(ctx, exporterOpts)
if tc.Want == 1 {
assert.NoError(t, err, "Must be able to connect to %s", tc.URI)
} else {
assert.Error(t, err, "Must be unable to connect to %s", tc.URI)
}

e := New(exporterOpts)
gc := newGeneralCollector(ctx, client, e.opts.Logger)
r := e.makeRegistry(ctx, client, new(labelsGetterMock), *e.opts)
e := New(exporterOpts)
nodeType, _ := getNodeType(ctx, client)
gc := newGeneralCollector(ctx, client, nodeType, e.opts.Logger)
r := e.makeRegistry(ctx, client, new(labelsGetterMock), *e.opts)

expected := strings.NewReader(`
expected := strings.NewReader(fmt.Sprintf(`
# HELP mongodb_up Whether MongoDB is up.
# TYPE mongodb_up gauge
mongodb_up ` + strconv.Itoa(tc.Want) + "\n")
filter := []string{
"mongodb_up",
}
err = testutil.CollectAndCompare(gc, expected, filter...)
assert.NoError(t, err, "mongodb_up metric should be %d", tc.Want)
mongodb_up {cluster_role="%s"} %s`, tc.clusterRole, strconv.Itoa(tc.Want)) + "\n")

filter := []string{
"mongodb_up",
}
err = testutil.CollectAndCompare(gc, expected, filter...)
assert.NoError(t, err, "mongodb_up metric should be %d", tc.Want)

res := r.Unregister(gc)
assert.Equal(t, true, res)
res := r.Unregister(gc)
assert.Equal(t, true, res)
})
}
}
Loading

0 comments on commit e332f24

Please sign in to comment.