Skip to content

Commit

Permalink
feature: instrument sequel gem
Browse files Browse the repository at this point in the history
Signed-off-by: arjun-rajappa <[email protected]>
  • Loading branch information
arjun-rajappa committed Aug 21, 2024
1 parent 7a1b69c commit b9bf65b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
21 changes: 21 additions & 0 deletions lib/instana/activators/sequel.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/instana/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions lib/instana/instrumentation/sequel.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/instana/tracing/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b9bf65b

Please sign in to comment.