Skip to content

Commit

Permalink
PMM-13374 Improve fingerprinter generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
BupycHuk committed Oct 28, 2024
1 parent 0971d2e commit 33434f4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (pf *ProfilerFingerprinter) Fingerprint(doc proto.SystemProfile) (fingerpri

// Helper for find operations with optional parameters.
func (pf *ProfilerFingerprinter) fingerprintFind(fp fingerprinter.Fingerprint, doc proto.SystemProfile) (fingerprinter.Fingerprint, error) {
filter := "{}"
filter := ""
command := doc.Command.Map()
if f, ok := command["filter"]; ok {
values := maskValues(f, make(map[string]maskOption))
Expand Down Expand Up @@ -141,23 +141,28 @@ func (pf *ProfilerFingerprinter) fingerprintDelete(fp fingerprinter.Fingerprint,
// Helper for general command operations, including support for "aggregate" commands
func (pf *ProfilerFingerprinter) fingerprintCommand(fp fingerprinter.Fingerprint, doc proto.SystemProfile) (fingerprinter.Fingerprint, error) {
// Unmarshal the command into a map for easy access and manipulation
commandMap := doc.Command.Map()

maskOptions := make(map[string]maskOption)
maskOptions["$db"] = maskOption{remove: true}
maskOptions["$readPreference"] = maskOption{remove: true}
maskOptions["lsid"] = maskOption{remove: true}
maskOptions["findAndModify"] = maskOption{skipMask: true}
maskOptions["remove"] = maskOption{skipMask: true}
if cmd, exists := commandMap["aggregate"]; exists {
command := doc.Command.Map()

maskOptions := map[string]maskOption{
"$db": {remove: true},
"$readPreference": {remove: true},
"$readConcern": {remove: true},
"$writeConcern": {remove: true},
"$clusterTime": {remove: true},
"$oplogQueryData": {remove: true},
"$replData": {remove: true},
"lastKnownCommittedOpTime": {remove: true},
"lsid": {remove: true},
"findAndModify": {skipMask: true},
"remove": {skipMask: true},
}
if _, exists := command["aggregate"]; exists {
// Set collection and initialize aggregation structure
collectionName, _ := cmd.(string)
fp.Collection = collectionName
fp.Fingerprint = fmt.Sprintf(`db.%s.aggregate([`, collectionName)
fp.Fingerprint = fmt.Sprintf(`db.%s.aggregate([`, fp.Collection)
stageStrings := []string{}

// Process pipeline stages, replacing all values with "?"
if pipeline, exists := commandMap["pipeline"]; exists {
if pipeline, exists := command["pipeline"]; exists {
pipelineStages, _ := pipeline.(bson.A)

for _, stage := range pipelineStages {
Expand All @@ -166,9 +171,7 @@ func (pf *ProfilerFingerprinter) fingerprintCommand(fp fingerprinter.Fingerprint
switch {
case stageMap["$match"] != nil:
stageJSON, _ = json.Marshal(maskValues(stageMap, maskOptions))
case stageMap["$group"] != nil:
stageJSON, _ = bson.MarshalExtJSON(stageMap, false, false)
case stageMap["$sort"] != nil:
default:
stageJSON, _ = bson.MarshalExtJSON(stageMap, false, false)
}

Expand All @@ -178,7 +181,7 @@ func (pf *ProfilerFingerprinter) fingerprintCommand(fp fingerprinter.Fingerprint
fp.Fingerprint += strings.Join(stageStrings, ", ")
}
fp.Fingerprint += "])"
if collation, exists := commandMap["collation"]; exists {
if collation, exists := command["collation"]; exists {
collationMasked, _ := json.Marshal(maskValues(collation, maskOptions))
fp.Fingerprint += fmt.Sprintf(`, collation: %s`, collationMasked)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,35 @@ func TestProfilerFingerprinter(t *testing.T) {
database.Collection("test").FindOneAndDelete(ctx, bson.M{"id": 1})
database.Collection("secondcollection").Find(ctx, bson.M{"name": "sec"}, options.Find().SetLimit(1).SetSort(bson.M{"id": -1}))
database.Collection("test").Aggregate(ctx,
bson.A{
bson.M{
[]bson.M{
{
"$match": bson.M{"id": 0, "time": bson.M{"$gt": time.Now().Add(-time.Hour)}},
},
bson.M{
{
"$group": bson.M{"_id": "$id", "count": bson.M{"$sum": 1}},
},
bson.M{
{
"$sort": bson.M{"_id": 1},
},
},
)
database.Collection("secondcollection").Aggregate(ctx, mongo.Pipeline{
bson.D{
{
Key: "$collStats", Value: bson.M{
// TODO: PMM-9568 : Add support to handle histogram metrics
"latencyStats": bson.M{"histograms": false},
"storageStats": bson.M{"scale": 1},
},
},
}, bson.D{
{
Key: "$project", Value: bson.M{
"storageStats.wiredTiger": 0,
"storageStats.indexDetails": 0,
},
},
}})
database.Collection("secondcollection").DeleteOne(ctx, bson.M{"id": 0})
database.Collection("test").DeleteMany(ctx, bson.M{"name": "test"})
profilerCollection := database.Collection("system.profile")
Expand Down Expand Up @@ -160,6 +177,7 @@ func TestProfilerFingerprinter(t *testing.T) {
`db.runCommand({"findAndModify":"test","query":{"id":"?"},"remove":true})`,
`db.secondcollection.find({"name":"?"}).sort({"id":-1}).limit(?)`,
`db.test.aggregate([{"$match":{"id":"?","time":{"$gt":"?"}}}, {"$group":{"_id":"$id","count":{"$sum":1}}}, {"$sort":{"_id":1}}])`,
`db.secondcollection.aggregate([{"$collStats":{"latencyStats":{"histograms":false},"storageStats":{"scale":1}}}, {"$project":{"storageStats.wiredTiger":0,"storageStats.indexDetails":0}}])`,
`db.secondcollection.deleteOne({"id":"?"})`,
`db.test.deleteMany({"name":"?"})`,
}
Expand Down

0 comments on commit 33434f4

Please sign in to comment.