Skip to content

Conversation

@timvaillancourt
Copy link

  1. Added query comments to .Find() queries in oplog_status.go (only .Find() queries).
  2. Cleaned up (terrible) duplicated logic/code for getting oplog timestamps that I made in oplog_status.go long ago.
  3. Added a function to shared/utils.go to add code comments (File+LineNum) to MongoDB queries.

Query comment output example in Profiler:

test1:PRIMARY> db.system.profile.find({op:"query",ns:{$ne:"local.system.profile"}},{op:1,ns:1,query:1,command:1}).pretty()
{
	"op" : "query",
	"ns" : "local.oplog.rs",
	"query" : {
		"find" : "oplog.rs",
		"sort" : {
			"$natural" : -1
		},
		"skip" : 0,
		"limit" : 1,
		"batchSize" : 1,
		"singleBatch" : true,
		"comment" : "/home/tim/go/src/github.com/percona/mongodb_exporter/collector/mongod/oplog_status.go:90"
	}
}
{
	"op" : "query",
	"ns" : "local.oplog.rs",
	"query" : {
		"find" : "oplog.rs",
		"sort" : {
			"$natural" : 1
		},
		"skip" : 0,
		"limit" : 1,
		"batchSize" : 1,
		"singleBatch" : true,
		"comment" : "/home/tim/go/src/github.com/percona/mongodb_exporter/collector/mongod/oplog_status.go:90"
	}
}

…hared/utils.go function to add comments, cleaned up duplicated code I made long ago
@AlekSi AlekSi self-assigned this Aug 20, 2017
@AlekSi
Copy link
Contributor

AlekSi commented Aug 21, 2017

Why do we need those comments?

@timvaillancourt
Copy link
Author

@AlekSi query comments show us the source of queries in the MongoDB profiler and .currentOp() outputs. This is very useful for investigation on the server as there is no context logged for operations. The plan is to add this to most mongo-related tools to aid in investigations and isolate user/application operations in profiler/server metrics

@AlekSi
Copy link
Contributor

AlekSi commented Aug 22, 2017

Is that approach documented somewhere?

@timvaillancourt
Copy link
Author

timvaillancourt commented Aug 22, 2017

Yes, this is documented here: https://docs.mongodb.com/v3.2/reference/method/cursor.comment/.

It is as simple as adding a .Comment() function to any mongo query object. In pymongo (python) it is lower-case .comment().

EDIT: adding blog on this https://www.percona.com/blog/2017/06/21/tracing-mongodb-queries-to-code-with-cursor-comments/

@AlekSi AlekSi closed this Oct 17, 2017
@AlekSi AlekSi changed the base branch from develop to master October 17, 2017 08:13
@AlekSi AlekSi reopened this Oct 17, 2017
@AlekSi AlekSi changed the base branch from master to PMM-1764-small-improvements January 4, 2018 11:51
@percona percona deleted a comment from codecov bot Jan 4, 2018
@AlekSi AlekSi merged commit f08e2e2 into percona:PMM-1764-small-improvements Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants