From e2a033780d5cb168b85967e7fc5fe7f355368280 Mon Sep 17 00:00:00 2001 From: arjun-rajappa Date: Wed, 21 Aug 2024 13:00:29 +0530 Subject: [PATCH] feature: instrument sequel gem Signed-off-by: arjun-rajappa --- lib/instana/activators/sequel.rb | 21 ++++++++++++++ lib/instana/config.rb | 1 + lib/instana/instrumentation/sequel.rb | 41 +++++++++++++++++++++++++++ lib/instana/tracing/span.rb | 4 +-- 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 lib/instana/activators/sequel.rb create mode 100644 lib/instana/instrumentation/sequel.rb diff --git a/lib/instana/activators/sequel.rb b/lib/instana/activators/sequel.rb new file mode 100644 index 00000000..558f3140 --- /dev/null +++ b/lib/instana/activators/sequel.rb @@ -0,0 +1,21 @@ +# (c) Copyright IBM Corp. 2021 +# (c) Copyright Instana Inc. 2021 + +module Instana + module Activators + class Sequel < Activator + def can_instrument? + defined?(::Sequel::Database) + end + + def instrument + require 'instana/instrumentation/sequel' + + ::Sequel::Database + .prepend(Instana::Instrumentation::Sequel) + + true + end + end + end +end diff --git a/lib/instana/config.rb b/lib/instana/config.rb index c44937ef..539b28c8 100644 --- a/lib/instana/config.rb +++ b/lib/instana/config.rb @@ -73,6 +73,7 @@ def initialize(logger: ::Instana.logger, agent_host: ENV['INSTANA_AGENT_HOST'], @config[:'resque-client'] = { :enabled => true, :propagate => true } @config[:'resque-worker'] = { :enabled => true, :'setup-fork' => true } @config[:'rest-client'] = { :enabled => true } + @config[:sequel] = { :enabled => true } @config[:'sidekiq-client'] = { :enabled => true } @config[:'sidekiq-worker'] = { :enabled => true } end diff --git a/lib/instana/instrumentation/sequel.rb b/lib/instana/instrumentation/sequel.rb new file mode 100644 index 00000000..a50b2852 --- /dev/null +++ b/lib/instana/instrumentation/sequel.rb @@ -0,0 +1,41 @@ +# (c) Copyright IBM Corp. 2024 + +module Instana + module Instrumentation + module Sequel + IGNORED_SQL = %w[BEGIN COMMIT SET].freeze + SANITIZE_REGEXP = /('[\s\S][^']*'|\d*\.\d+|\d+|NULL)/i.freeze + + def log_connection_yield(sql, conn, *args) + call_payload = { + sequel: { + adapter: opts[:adapter], + host: opts[:host], + username: opts[:user], + db: opts[:database], + sql: maybe_sanitize(sql) + } + } + maybe_trace(call_payload) { super(sql, conn, *args) } + end + + private + + def maybe_sanitize(sql) + ::Instana.config[:sanitize_sql] ? sql.gsub(SANITIZE_REGEXP, '?') : sql + end + + def maybe_trace(call_payload, &blk) + if ::Instana.tracer.tracing? && !ignored?(call_payload) + ::Instana.tracer.trace(:sequel, call_payload, &blk) + else + yield + end + end + + def ignored?(call_payload) + IGNORED_SQL.any? { |s| call_payload[:sequel][:sql].upcase.start_with?(s) } + end + end + end +end diff --git a/lib/instana/tracing/span.rb b/lib/instana/tracing/span.rb index 6762bdf0..9847c3a5 100644 --- a/lib/instana/tracing/span.rb +++ b/lib/instana/tracing/span.rb @@ -7,12 +7,12 @@ class Span :memcache, :'net-http', :rack, :render, :'rpc-client', :'rpc-server', :'sidekiq-client', :'sidekiq-worker', :redis, :'resque-client', :'resque-worker', :'graphql.server', :dynamodb, :s3, :sns, :sqs, :'aws.lambda.entry', :activejob, :log, :"mail.actionmailer", - :"aws.lambda.invoke", :mongo ].freeze + :"aws.lambda.invoke", :mongo, :sequel ].freeze ENTRY_SPANS = [ :rack, :'resque-worker', :'rpc-server', :'sidekiq-worker', :'graphql.server', :sqs, :'aws.lambda.entry' ].freeze EXIT_SPANS = [ :activerecord, :excon, :'net-http', :'resque-client', :'rpc-client', :'sidekiq-client', :redis, :dynamodb, :s3, :sns, :sqs, :log, :"mail.actionmailer", - :"aws.lambda.invoke", :mongo ].freeze + :"aws.lambda.invoke", :mongo, :sequel ].freeze HTTP_SPANS = [ :rack, :excon, :'net-http' ].freeze attr_accessor :parent