-
Notifications
You must be signed in to change notification settings - Fork 451
PMM-8770 enable safe metrics collection #389
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
Merged
percona-csalguero
merged 27 commits into
main
from
PMM-8770_enable_safe_metrics_collection
Dec 1, 2021
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
81428a0
PMM-8489 Fixed topology labels on mongos
percona-csalguero 3b76431
Updated mog for go 1.16
percona-csalguero 4684025
Merge branch 'main' into PMM-8489_topology_labels_on_mongos
percona-csalguero 7fc91f9
Merge branch 'main' into PMM-8489_topology_labels_on_mongos
percona-csalguero 14c7229
Updated dependency
percona-csalguero 346e399
Updated toolkit dep to 3.x
percona-csalguero 98bdcba
Updated go.sum for go 1.16
percona-csalguero 0a04652
Merge branch 'main' into PMM-8489_topology_labels_on_mongos
JiriCtvrtka 73d0e96
Merge branch 'main' into PMM-8489_topology_labels_on_mongos
percona-csalguero 761774b
Fixed mod conflicts
percona-csalguero 8905fce
Merge branch 'PMM-8489_topology_labels_on_mongos'
percona-csalguero a5bb9e4
Revert "Merge branch 'PMM-8489_topology_labels_on_mongos'"
percona-csalguero 6dd4086
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 4e6b7c0
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 8403c40
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 85ef452
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 405bfc8
PMM-8770 Updated discovery mode
percona-csalguero e177ed9
PMM-8770 Added common functions
percona-csalguero 4417dd5
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 2cccd46
Merge branch 'main' into PMM-8770_enable_safe_metrics_collection
percona-csalguero 0957e4a
PMM-8770 New collstats limit
percona-csalguero f54ec94
Merge branch 'main' of percona.github.com:percona/mongodb_exporter
percona-csalguero 2e09dbb
Merge branch 'main' into PMM-8770_enable_safe_metrics_collection
percona-csalguero 4a0db92
Fixed race condition
percona-csalguero 236894e
Top collector won't be enabled on mongos
percona-csalguero cb19f17
Fixes for CR
percona-csalguero b24a2f0
Renamed collector.topmetric param
percona-csalguero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package exporter | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/AlekSi/pointer" | ||
| "github.com/pkg/errors" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
| "go.mongodb.org/mongo-driver/bson/primitive" | ||
percona-csalguero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "go.mongodb.org/mongo-driver/mongo" | ||
| "go.mongodb.org/mongo-driver/mongo/options" | ||
| ) | ||
|
|
||
| var systemDBs = []string{"admin", "config", "local"} //nolint:gochecknoglobals | ||
|
|
||
| func listCollections(ctx context.Context, client *mongo.Client, collections []string, database string) ([]string, error) { | ||
| filter := bson.D{} // Default=empty -> list all collections | ||
|
|
||
| // if there is a filter with the list of collections we want, create a filter like | ||
| // $or: { | ||
| // {"$regex": "collection1"}, | ||
| // {"$regex": "collection2"}, | ||
| // } | ||
| if len(collections) > 0 { | ||
| matchExpressions := []bson.D{} | ||
|
|
||
| for _, collection := range collections { | ||
percona-csalguero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| matchExpressions = append(matchExpressions, | ||
| bson.D{{Key: "name", Value: primitive.Regex{Pattern: collection, Options: "i"}}}) | ||
percona-csalguero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| filter = bson.D{{Key: "$or", Value: matchExpressions}} | ||
| } | ||
|
|
||
| databases, err := client.Database(database).ListCollectionNames(ctx, filter) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "cannot get the list of collections for discovery") | ||
| } | ||
|
|
||
| return databases, nil | ||
| } | ||
|
|
||
| func databases(ctx context.Context, client *mongo.Client, exclude []string) ([]string, error) { | ||
| opts := &options.ListDatabasesOptions{NameOnly: pointer.ToBool(true), AuthorizedDatabases: pointer.ToBool(true)} | ||
| filterExpressions := []bson.D{} | ||
| for _, dbname := range exclude { | ||
percona-csalguero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| filterExpressions = append(filterExpressions, | ||
| bson.D{{Key: "name", Value: bson.D{{Key: "$ne", Value: dbname}}}}, | ||
| ) | ||
| } | ||
|
|
||
| filter := bson.D{{Key: "$and", Value: filterExpressions}} | ||
|
|
||
| dbNames, err := client.ListDatabaseNames(ctx, filter, opts) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "cannot get the database names list") | ||
| } | ||
|
|
||
| return dbNames, nil | ||
| } | ||
|
|
||
| func listAllCollections(ctx context.Context, client *mongo.Client, filter []string) (map[string][]string, error) { | ||
| namespaces := make(map[string][]string) | ||
| // exclude system databases | ||
| dbnames, err := databases(ctx, client, systemDBs) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "cannot get the list of all collections in the server") | ||
| } | ||
|
|
||
| for _, dbname := range dbnames { | ||
| colls, err := listCollections(ctx, client, filter, dbname) | ||
| if err != nil { | ||
| return nil, errors.Wrapf(err, "cannot list the collections for %q", dbname) | ||
| } | ||
| namespaces[dbname] = colls | ||
| } | ||
|
|
||
| return namespaces, nil | ||
| } | ||
|
|
||
| func allCollectionsCount(ctx context.Context, client *mongo.Client, filter []string) (int, error) { | ||
| databases, err := databases(ctx, client, systemDBs) | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "cannot retrieve the collection names for count collections") | ||
| } | ||
|
|
||
| var count int | ||
|
|
||
| for _, dbname := range databases { | ||
| colls, err := listCollections(ctx, client, filter, dbname) | ||
| if err != nil { | ||
| return 0, errors.Wrap(err, "cannot get collections count") | ||
| } | ||
| count += len(colls) | ||
| } | ||
|
|
||
| return count, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package exporter | ||
|
|
||
| import ( | ||
| "context" | ||
| "sort" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "go.mongodb.org/mongo-driver/bson" | ||
|
|
||
| "github.com/percona/mongodb_exporter/internal/tu" | ||
| ) | ||
|
|
||
| func TestListCollections(t *testing.T) { | ||
| t.Parallel() | ||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
| defer cancel() | ||
|
|
||
| client := tu.DefaultTestClient(ctx, t) | ||
|
|
||
| databases := []string{"testdb01", "testdb02"} | ||
| collections := []string{"col01", "col02", "colxx", "colyy"} | ||
|
|
||
| defer func() { | ||
| for _, dbname := range databases { | ||
| client.Database(dbname).Drop(ctx) //nolint:errcheck | ||
| } | ||
| }() | ||
|
|
||
| for _, dbname := range databases { | ||
| for _, coll := range collections { | ||
| for j := 0; j < 10; j++ { | ||
| _, err := client.Database(dbname).Collection(coll).InsertOne(ctx, bson.M{"f1": j, "f2": "2"}) | ||
| assert.NoError(t, err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| want := []string{"col01", "col02", "colxx"} | ||
| collections, err := listCollections(ctx, client, []string{"col0", "colx"}, databases[0]) | ||
| sort.Strings(collections) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, want, collections) | ||
|
|
||
| count, err := allCollectionsCount(ctx, client, nil) | ||
| assert.NoError(t, err) | ||
| assert.True(t, count > 8) | ||
|
|
||
| count, err = allCollectionsCount(ctx, client, []string{"col0", "colx"}) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 6, count) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.