Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,5 @@ gem "faraday-follow_redirects", "~> 0.3.0"
gem "opentelemetry-sdk", "~> 1.7"
gem "opentelemetry-exporter-otlp", "~> 0.29.1"
gem "opentelemetry-instrumentation-all", "~> 0.74.0"

gem "mongoid", "~> 9.0"
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ GEM
bindex (0.8.1)
bootsnap (1.18.4)
msgpack (~> 1.2)
bson (5.0.2)
builder (3.3.0)
bump (0.10.0)
capybara (3.40.0)
Expand Down Expand Up @@ -243,6 +244,12 @@ GEM
railties (>= 7.1)
stimulus-rails
turbo-rails
mongo (2.21.0)
bson (>= 4.14.1, < 6.0.0)
mongoid (9.0.6)
activemodel (>= 5.1, < 8.1, != 7.0.0)
concurrent-ruby (>= 1.0.5, < 2.0)
mongo (>= 2.18.0, < 3.0.0)
msgpack (1.7.3)
multi_json (1.15.0)
multi_xml (0.7.1)
Expand Down Expand Up @@ -743,6 +750,7 @@ DEPENDENCIES
memory_profiler (~> 1.1)
mini_magick (~> 5.0)
mission_control-jobs (~> 0.6.0)
mongoid (~> 9.0)
opentelemetry-exporter-otlp (~> 0.29.1)
opentelemetry-instrumentation-all (~> 0.74.0)
opentelemetry-sdk (~> 1.7)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/patterns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class PatternsController < ApplicationController
skip_before_action :verify_authenticity_token

def new
LogEntry.where(message: "Pattern created").first
render :new
end

Expand Down
25 changes: 25 additions & 0 deletions config/initializers/mongoid.rb
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
56 changes: 56 additions & 0 deletions config/initializers/opentelemetry.rb
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
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.

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
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


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
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...

}
)
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
Loading