-
Notifications
You must be signed in to change notification settings - Fork 1
Attempt to instrument mongodb queries #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# rubocop:todo all | ||
Mongoid.configure do | ||
target_version = "9.0" | ||
|
||
# Load Mongoid behavior defaults. This automatically sets | ||
# features flags (refer to documentation) | ||
config.load_defaults target_version | ||
|
||
# It is recommended to use config/mongoid.yml for most Mongoid-related | ||
# configuration, whenever possible, but if you prefer, you can set | ||
# configuration values here, instead: | ||
# | ||
# config.log_level = :debug | ||
# | ||
# Note that the settings in config/mongoid.yml always take precedence, | ||
# whatever else is set here. | ||
end | ||
|
||
# Enable Mongo driver query cache for Rack | ||
# Rails.application.config.middleware.use(Mongo::QueryCache::Middleware) | ||
|
||
# Enable Mongo driver query cache for ActiveJob | ||
# ActiveSupport.on_load(:active_job) do | ||
# include Mongo::QueryCache::Middleware::ActiveJob | ||
# end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# config/initializer/opentelemetry.rb | ||
|
||
require "opentelemetry/sdk" | ||
require "opentelemetry/exporter/otlp" | ||
require "opentelemetry/instrumentation/all" | ||
|
||
class BatchSpanProcessorWithDbStatementFlattening < OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor | ||
def on_start(span, context) | ||
# This is where we modify spans before they start. | ||
statement = span.attributes["db.statement"] | ||
if statement | ||
begin | ||
# Recursively convert nested JSON attributes to dotted format | ||
def flatten_json(obj, prefix = "") | ||
attributes = {} | ||
obj.each do |key, value| | ||
if value.is_a?(Hash) | ||
attributes.merge!(flatten_json(value, "#{prefix}#{key}.")) | ||
else | ||
attributes["#{prefix}#{key}"] = value | ||
end | ||
end | ||
attributes | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recursive function to build up |
||
|
||
parsed_statement = JSON.parse(statement) | ||
flatten_json(parsed_statement).each do |key, value| | ||
span.set_attribute("db.statement.#{key}", value) | ||
end | ||
rescue JSON::ParserError | ||
end | ||
end | ||
super(span, context) | ||
end | ||
end | ||
|
||
|
||
OpenTelemetry::SDK.configure do |c| | ||
c.use_all( | ||
"OpenTelemetry::Instrumentation::ActiveJob" => { | ||
propagation_style: :link, | ||
span_naming: :job_class | ||
}, | ||
"OpenTelemetry::Instrumentation::Mongo" => { | ||
db_statement: :include | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Turns out this is important. Without this, the instrumentation will substitute Just make sure you don't use this on PII information or secrets... |
||
} | ||
) | ||
c.resource = OpenTelemetry::SDK::Resources::Resource.create( | ||
"service.commit_sha" => `git rev-parse HEAD`.strip | ||
) | ||
c.add_span_processor( | ||
BatchSpanProcessorWithDbStatementFlattening.new( | ||
OpenTelemetry::Exporter::OTLP::Exporter.new | ||
) | ||
) | ||
end |
There was a problem hiding this comment.
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.