Skip to content

PMM-10072 Filter out profiler collection #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S1_PRIMARY_PORT:-17001}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --profile 2
links:
- mongo-1-2:mongo-1-2
- mongo-1-3:mongo-1-3
Expand All @@ -15,14 +15,14 @@ services:
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S1_SECONDARY1_PORT:-17002}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --profile 2

mongo-1-3:
container_name: "mongo-1-3"
image: ${TEST_MONGODB_IMAGE:-mongo:4.2}
ports:
- "${TEST_MONGODB_S1_SECONDARY2_PORT:-17003}:27017"
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16
command: mongod --replSet rs1 --shardsvr --port 27017 --oplogSize 16 --profile 2

mongo-1-arbiter:
container_name: "mongo-1-arbiter"
Expand Down
20 changes: 19 additions & 1 deletion exporter/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func listCollections(ctx context.Context, client *mongo.Client, database string,
return nil, errors.Wrap(err, "cannot get the list of collections for discovery")
}

return collections, nil
filteredCollections := filterSystemCollections(collections)

return filteredCollections, nil
}

// databases returns the list of databases matching the filters.
Expand Down Expand Up @@ -200,6 +202,22 @@ func listAllCollections(ctx context.Context, client *mongo.Client, filterInNames
return namespaces, nil
}

func filterSystemCollections(cols []string) []string {
systemCollections := map[string]bool{
"system.profile": true,
}

filtered := make([]string, 0, len(cols))
for _, col := range cols {
if systemCollections[col] {
continue
}
filtered = append(filtered, col)
}

return filtered
}

func nonSystemCollectionsCount(ctx context.Context, client *mongo.Client, includeNamespaces []string, filterInCollections []string) (int, error) {
databases, err := databases(ctx, client, includeNamespaces, systemDBs)
if err != nil {
Expand Down
35 changes: 9 additions & 26 deletions exporter/dbstats_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,41 @@ package exporter

import (
"context"
"fmt"
"strings"
"testing"
"time"

"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"

"github.com/percona/mongodb_exporter/internal/tu"
)

const (
dbName = "testdb"
)

func TestDBStatsCollector(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

client := tu.DefaultTestClient(ctx, t)

database := client.Database(dbName)
database.Drop(ctx) //nolint

defer func() {
err := database.Drop(ctx)
assert.NoError(t, err)
}()

for i := 0; i < 3; i++ {
coll := fmt.Sprintf("testcol_%02d", i)
for j := 0; j < 10; j++ {
_, err := database.Collection(coll).InsertOne(ctx, bson.M{"f1": j, "f2": "2"})
assert.NoError(t, err)
}
}
setupDB(ctx, t, client)
defer cleanupDB(ctx, client)

ti := labelsGetterMock{}

c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, []string{dbName}, false)
c := newDBStatsCollector(ctx, client, logrus.New(), false, ti, testDBs, false)
expected := strings.NewReader(`
# HELP mongodb_dbstats_collections dbstats.
# TYPE mongodb_dbstats_collections untyped
mongodb_dbstats_collections{database="testdb"} 3
mongodb_dbstats_collections{database="testdb01"} 5
mongodb_dbstats_collections{database="testdb02"} 5
# HELP mongodb_dbstats_indexes dbstats.
# TYPE mongodb_dbstats_indexes untyped
mongodb_dbstats_indexes{database="testdb"} 3
mongodb_dbstats_indexes{database="testdb01"} 4
mongodb_dbstats_indexes{database="testdb02"} 4
# HELP mongodb_dbstats_objects dbstats.
# TYPE mongodb_dbstats_objects untyped
mongodb_dbstats_objects{database="testdb"} 30` + "\n")
mongodb_dbstats_objects{database="testdb01"} 80
mongodb_dbstats_objects{database="testdb02"} 80` + "\n")

// Only look at metrics created by our activity
filters := []string{
Expand Down