Skip to content

Attempt to instrument mongodb queries#2

Open
johngallagher wants to merge 2 commits into
mainfrom
experiment-with-mongo
Open

Attempt to instrument mongodb queries#2
johngallagher wants to merge 2 commits into
mainfrom
experiment-with-mongo

Conversation

@johngallagher

@johngallagher johngallagher commented Mar 1, 2025

Copy link
Copy Markdown
Contributor

Why

I'm interested in parsing out MongoDB queries to see if we can correlate performance to certain types of query.

How

Add a custom span processor before the span gets to OpenTelemetry.

Subclass the batch span processor but customise it so if the db.statement attribute is there, it assumes it's in JSON format and parses out all the attributes.

Test

In Honeycomb if we hit /patterns/new we see:

image

It works!

@johngallagher johngallagher changed the title Experiment with mongo Attempt to instrument mongodb queries Mar 1, 2025
Comment thread lib/log_entry.rb
@@ -0,0 +1,6 @@
class LogEntry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogEntry is completely arbitrary - just the simplest example I could come up with.

span_naming: :job_class
},
"OpenTelemetry::Instrumentation::Mongo" => {
db_statement: :include

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this is important. Without this, the instrumentation will substitute ? for attributes.

Just make sure you don't use this on PII information or secrets...

end
end
attributes
end

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursive function to build up filter.message etc

require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"

class BatchSpanProcessorWithDbStatementFlattening < OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like to subclass generally, but this works. Probably a better way to do this with delegation, but it's just a proof of concept.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant