Skip to content

Conversation

johngallagher
Copy link
Contributor

@johngallagher johngallagher commented Mar 1, 2025

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
@@ -0,0 +1,6 @@
class LogEntry
Copy link
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
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
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
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