-
Notifications
You must be signed in to change notification settings - Fork 369
/
database.rb
61 lines (53 loc) · 1.78 KB
/
database.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'ddtrace/ext/sql'
require 'ddtrace/ext/app_types'
require 'ddtrace/contrib/analytics'
require 'ddtrace/contrib/sequel/ext'
require 'ddtrace/contrib/sequel/utils'
module Datadog
module Contrib
module Sequel
# Adds instrumentation to Sequel::Database
module Database
def self.included(base)
base.send(:prepend, InstanceMethods)
end
# Instance methods for instrumenting Sequel::Database
module InstanceMethods
def run(sql, options = ::Sequel::OPTS)
opts = parse_opts(sql, options)
response = nil
datadog_pin.tracer.trace(Ext::SPAN_QUERY) do |span|
span.service = datadog_pin.service
span.resource = opts[:query]
span.span_type = Datadog::Ext::SQL::TYPE
Utils.set_common_tags(span)
span.set_tag(Ext::TAG_DB_VENDOR, adapter_name)
response = super(sql, options)
end
response
end
def datadog_pin
@pin ||= Datadog::Pin.new(
Datadog.configuration[:sequel][:service_name] || adapter_name,
app: Ext::APP,
app_type: Datadog::Ext::AppTypes::DB,
tracer: -> { Datadog.configuration[:sequel][:tracer] }
)
end
private
def adapter_name
Utils.adapter_name(self)
end
def parse_opts(sql, opts)
db_opts = if ::Sequel::VERSION < '3.41.0' && self.class.to_s !~ /Dataset$/
@opts
elsif instance_variable_defined?(:@pool) && @pool
@pool.db.opts
end
Utils.parse_opts(sql, opts, db_opts)
end
end
end
end
end
end