app_perf_rpm 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/lib/app_perf_rpm/backtrace.rb +7 -7
  3. data/lib/app_perf_rpm/configuration.rb +6 -4
  4. data/lib/app_perf_rpm/instruments/action_controller.rb +33 -16
  5. data/lib/app_perf_rpm/instruments/action_view.rb +93 -65
  6. data/lib/app_perf_rpm/instruments/active_record/adapters/mysql2.rb +24 -15
  7. data/lib/app_perf_rpm/instruments/active_record/adapters/postgresql.rb +95 -52
  8. data/lib/app_perf_rpm/instruments/active_record/adapters/sqlite3.rb +95 -54
  9. data/lib/app_perf_rpm/instruments/active_record.rb +1 -1
  10. data/lib/app_perf_rpm/instruments/activerecord_import.rb +22 -13
  11. data/lib/app_perf_rpm/instruments/emque_consuming.rb +16 -7
  12. data/lib/app_perf_rpm/instruments/faraday.rb +26 -16
  13. data/lib/app_perf_rpm/instruments/net_http.rb +16 -10
  14. data/lib/app_perf_rpm/instruments/rack.rb +75 -25
  15. data/lib/app_perf_rpm/instruments/redis.rb +49 -13
  16. data/lib/app_perf_rpm/instruments/sequel.rb +36 -28
  17. data/lib/app_perf_rpm/instruments/sidekiq.rb +65 -21
  18. data/lib/app_perf_rpm/instruments/sinatra.rb +34 -20
  19. data/lib/app_perf_rpm/instruments/typhoeus.rb +40 -21
  20. data/lib/app_perf_rpm/rails.rb +2 -1
  21. data/lib/app_perf_rpm/railtie.rb +4 -4
  22. data/lib/app_perf_rpm/reporters/json_client.rb +69 -0
  23. data/lib/app_perf_rpm/reporters/null_client.rb +14 -0
  24. data/lib/app_perf_rpm/tracer.rb +20 -89
  25. data/lib/app_perf_rpm/tracing/buffer.rb +25 -0
  26. data/lib/app_perf_rpm/tracing/carrier.rb +23 -0
  27. data/lib/app_perf_rpm/tracing/collector.rb +31 -0
  28. data/lib/app_perf_rpm/tracing/endpoint.rb +19 -0
  29. data/lib/app_perf_rpm/tracing/managed_span.rb +36 -0
  30. data/lib/app_perf_rpm/tracing/managed_tracer.rb +32 -0
  31. data/lib/app_perf_rpm/tracing/span.rb +67 -0
  32. data/lib/app_perf_rpm/tracing/span_context.rb +41 -0
  33. data/lib/app_perf_rpm/tracing/thread_span_stack.rb +32 -0
  34. data/lib/app_perf_rpm/tracing/trace_id.rb +11 -0
  35. data/lib/app_perf_rpm/tracing/tracer.rb +91 -0
  36. data/lib/app_perf_rpm/utils.rb +18 -0
  37. data/lib/app_perf_rpm.rb +59 -26
  38. metadata +90 -12
  39. data/lib/app_perf_rpm/aggregator.rb +0 -77
  40. data/lib/app_perf_rpm/dispatcher.rb +0 -85
  41. data/lib/app_perf_rpm/middleware.rb +0 -30
  42. data/lib/app_perf_rpm/span.rb +0 -103
  43. data/lib/app_perf_rpm/worker.rb +0 -46
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a066a37d5f3c2f7880ef434988ca092e512e741
4
- data.tar.gz: a781dcbc4d6c97fe97fd807d6eaa8859450a45a2
3
+ metadata.gz: 049ed9d70578f19385c19d57d2a9794be48518a0
4
+ data.tar.gz: 5ebeb333e89d38319a413d92f4a7e41f5bd11348
5
5
  SHA512:
6
- metadata.gz: d0c35afab0e3d636277c7b308f0c81186ee5a6cfe3f01997c0e559e63b327e5e09103063ee5e6d026f69357c00387e326e013affa98d9de64d0fcf92ea29989c
7
- data.tar.gz: 1bed1af8e426808af7702eedfc810f7508f8b510acb0c49f621dbbf17c343e5a1bc6a3f47571ba18b4ba3cb3614e4ef1f2c2cd629ee5fb7348310ed0d4f2b6c8
6
+ metadata.gz: 50cfe054972dcb5a8ba179383c4ad7433a4a84c13d2c70f73afe6dd1401715e82d8bcecab2311b8e2a737de84507376eb5794b6e4e624ecbfda9c5a3a86ae3f3
7
+ data.tar.gz: 9ac2e437821c812c0c345dc61c32067ad35d7dc45c7614ae5f5bf6e1e85a89ee10b13f2c99ec83ecbe58ce4a3dda8c40211bf7901b23e41284d8903b146820f5
@@ -8,7 +8,7 @@ module AppPerfRpm
8
8
  end
9
9
 
10
10
  def clean(backtrace)
11
- backtrace
11
+ Array(backtrace)
12
12
  .map {|b| clean_line(b) }
13
13
  .select {|b| b !~ %r{lib/app_perf_rpm} }
14
14
  end
@@ -27,7 +27,7 @@ module AppPerfRpm
27
27
  #end
28
28
 
29
29
  def source_extract(_backtrace = Kernel.caller(0))
30
- Array(_backtrace).select {|bt| bt[/^#{::AppPerfRpm.configuration.app_root.to_s}\//] }.map do |trace|
30
+ Array(_backtrace).select {|bt| bt[/^#{::AppPerfRpm.config.app_root.to_s}\//] }.map do |trace|
31
31
  file, line_number = extract_file_and_line_number(trace)
32
32
  source_to_hash(file, line_number)
33
33
  end
@@ -41,14 +41,14 @@ module AppPerfRpm
41
41
  }
42
42
  end
43
43
 
44
- private
45
-
46
44
  def clean_line(line)
47
45
  line
48
- .sub(/#{::AppPerfRpm.configuration.app_root.to_s}\//, "[APP_PATH]/")
46
+ .sub(/#{::AppPerfRpm.config.app_root.to_s}\//, "[APP_PATH]/")
49
47
  .sub(gems_regexp, '\2 (\3) [GEM_PATH]/\4')
50
48
  end
51
49
 
50
+ private
51
+
52
52
  def gems_regexp
53
53
  gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) }
54
54
  if gems_paths
@@ -59,8 +59,8 @@ module AppPerfRpm
59
59
  end
60
60
 
61
61
  def source_fragment(path, line)
62
- return unless AppPerfRpm.configuration.app_root
63
- full_path = AppPerfRpm.configuration.app_root.join(path)
62
+ return unless AppPerfRpm.config.app_root
63
+ full_path = AppPerfRpm.config.app_root.join(path)
64
64
  if File.exist?(full_path)
65
65
  File.open(full_path, "r") do |file|
66
66
  start = [line - 3, 0].max
@@ -9,10 +9,11 @@ module AppPerfRpm
9
9
  :ssl,
10
10
  :sample_rate,
11
11
  :sample_threshold,
12
- :dispatch_interval,
12
+ :flush_interval,
13
13
  :application_name,
14
14
  :instrumentation,
15
- :agent_disabled
15
+ :agent_disabled,
16
+ :ignore_paths
16
17
 
17
18
  def initialize
18
19
  reload
@@ -27,10 +28,11 @@ module AppPerfRpm
27
28
  self.application_name ||= "Default"
28
29
  self.sample_rate ||= 10 # Percentage of request to sample
29
30
  self.sample_threshold ||= 0 # Minimum amount of duration to sample
30
- self.dispatch_interval ||= 60 # In seconds
31
+ self.flush_interval ||= 60 # In seconds
31
32
  self.agent_disabled ||= default_if_blank(ENV["APP_PERF_AGENT_DISABLED"], false)
33
+ self.ignore_paths ||= /\/assets/
32
34
  self.instrumentation = {
33
- :rack => { :enabled => true, :backtrace => false, :trace_middleware => true },
35
+ :rack => { :enabled => true, :backtrace => false, :trace_middleware => false },
34
36
  :active_record => { :enabled => true, :backtrace => false },
35
37
  :active_record_import => { :enabled => true, :backtrace => false },
36
38
  :action_view => { :enabled => true, :backtrace => false },
@@ -3,34 +3,51 @@ module AppPerfRpm
3
3
  module ActionController
4
4
  def process_action_with_trace(method_name, *args)
5
5
  if ::AppPerfRpm::Tracer.tracing?
6
- AppPerfRpm::Tracer.trace('actioncontroller') do |span|
7
- span.controller = self.class.name
8
- span.action = self.action_name
6
+ operation = "#{self.class.name}##{self.action_name}"
7
+ span = AppPerfRpm.tracer.start_span(operation, tags: {
8
+ "component" => "ActionController",
9
+ "span.kind" => "client"
10
+ })
11
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
12
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
13
+ end
9
14
 
10
- process_action_without_trace(method_name, *args)
11
- end
12
- else
13
- process_action_without_trace(method_name, *args)
15
+ process_action_without_trace(method_name, *args)
16
+ rescue Exception => e
17
+ if span
18
+ span.set_tag('error', true)
19
+ span.log_error(e)
14
20
  end
21
+ raise
22
+ ensure
23
+ span.finish if span
15
24
  end
16
25
 
17
26
  def perform_action_with_trace(*arguments)
18
27
  if ::AppPerfRpm::Tracer.tracing?
19
- AppPerfRpm::Tracer.trace('actioncontroller') do |span|
20
- span.controller = @_request.path_parameters['controller']
21
- span.action = @_request.path_parameters['action']
22
-
23
- perform_action_without_trace(*arguments)
24
- end
25
- else
26
- perform_action_without_trace(*arguments)
28
+ operation = "#{@_request.path_parameters['controller']}##{@_request.path_parameters['action']}"
29
+ span = AppPerfRpm.tracer.start_span(operation, tags: {
30
+ "component" => "ActionController",
31
+ "span.kind" => "client"
32
+ })
33
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
34
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
35
+ end
36
+ perform_action_without_trace(*arguments)
37
+ rescue Exception => e
38
+ if span
39
+ span.set_tag('error', true)
40
+ span.log_error(e)
27
41
  end
42
+ raise
43
+ ensure
44
+ span.finish if span
28
45
  end
29
46
  end
30
47
  end
31
48
  end
32
49
 
33
- if ::AppPerfRpm.configuration.instrumentation[:action_controller][:enabled] &&
50
+ if ::AppPerfRpm.config.instrumentation[:action_controller][:enabled] &&
34
51
  defined?(::ActionController)
35
52
  AppPerfRpm.logger.info "Initializing actioncontroller tracer."
36
53
 
@@ -1,46 +1,54 @@
1
- if ::AppPerfRpm.configuration.instrumentation[:action_view][:enabled] && defined?(::ActionView)
1
+ if ::AppPerfRpm.config.instrumentation[:action_view][:enabled] && defined?(::ActionView)
2
2
  if defined?(Rails) && Rails::VERSION::MAJOR == 2
3
3
  ActionView::Partials.module_eval do
4
4
  alias :render_partial_without_trace :render_partial
5
5
  def render_partial(options = {})
6
6
  if ::AppPerfRpm::Tracer.tracing? && options.key?(:partial) && options[:partial].is_a?(String)
7
- opts = {
8
- "method" => "render_partial",
9
- "name" => options[:partial]
10
- }
11
-
12
- opts["backtrace"] = ::AppPerfRpm::Backtrace.backtrace
13
- opts["source"] = ::AppPerfRpm::Backtrace.source_extract
14
-
15
- AppPerfRpm::Tracer.trace("actionview", opts) do |span|
16
- span.controller = @_request.path_parameters['controller']
17
- span.action = @_request.path_parameters['action']
18
- span.backtrace = ::AppPerfRpm::Backtrace.backtrace
19
- span.source = ::AppPerfRpm::Backtrace.source_extract
7
+ span = AppPerfRpm.tracer.start_span("render_partial", tags: {
8
+ "component" => "ActionView",
9
+ "span.kind" => "client",
10
+ "view.controller" => @_request.path_parameters['controller'],
11
+ "view.action" => @_request.path_parameters['action'],
12
+ "view.template" => options[:partial]
13
+ })
14
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
15
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
16
+ end
20
17
 
21
- render_partial_without_trace(options)
22
- end
23
- else
24
- render_partial_without_trace(options)
18
+ render_partial_without_trace(options)
19
+ rescue Exception => e
20
+ if span
21
+ span.set_tag('error', true)
22
+ span.log_error(e)
25
23
  end
24
+ raise
25
+ ensure
26
+ span.finish if span
26
27
  end
27
28
 
28
29
  alias :render_partial_collection_without_trace :render_partial_collection
29
30
  def render_partial_collection(options = {})
30
31
  if ::AppPerfRpm::Tracer.tracing?
31
- AppPerfRpm::Tracer.trace("actionview") do |span|
32
- span.backtrace = ::AppPerfRpm::Backtrace.backtrace
33
- span.source = ::AppPerfRpm::Backtrace.source_extract
34
- span.options = {
35
- "method" => "render_partial_collection",
36
- "name" => @path
37
- }
32
+ span = AppPerfRpm.tracer.start_span("render_partial_collection", tags: {
33
+ "component" => "ActionView",
34
+ "span.kind" => "client",
35
+ "view.controller" => @_request.path_parameters['controller'],
36
+ "view.action" => @_request.path_parameters['action'],
37
+ "view.template" => @path
38
+ })
39
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
40
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
41
+ end
38
42
 
39
- render_partial_collection_without_trace(options)
40
- end
41
- else
42
- render_partial_collection_without_trace(options)
43
+ render_partial_collection_without_trace(options)
44
+ rescue Exception => e
45
+ if span
46
+ span.set_tag('error', true)
47
+ span.log_error(e)
43
48
  end
49
+ raise
50
+ ensure
51
+ span.finish if span
44
52
  end
45
53
  end
46
54
  else
@@ -48,35 +56,49 @@ if ::AppPerfRpm.configuration.instrumentation[:action_view][:enabled] && defined
48
56
  alias :render_partial_without_trace :render_partial
49
57
  def render_partial
50
58
  if ::AppPerfRpm::Tracer.tracing?
51
- AppPerfRpm::Tracer.trace("actionview") do |span|
52
- span.backtrace = ::AppPerfRpm::Backtrace.backtrace
53
- span.source = ::AppPerfRpm::Backtrace.source_extract
54
- span.options = {
55
- "method" => "render_partial",
56
- "name" => @options[:partial]
57
- }
59
+ span = AppPerfRpm.tracer.start_span("render_partial", tags: {
60
+ "component" => "ActionView",
61
+ "span.kind" => "client",
62
+ "view.template" => @options[:partial]
63
+ })
64
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
65
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
66
+ end
58
67
 
59
- render_partial_without_trace
60
- end
61
- else
62
- render_partial_without_trace
68
+ render_partial_without_trace
69
+ rescue Exception => e
70
+ if span
71
+ span.set_tag('error', true)
72
+ span.log_error(e)
63
73
  end
74
+ raise
75
+ ensure
76
+ span.finish if span
64
77
  end
65
78
 
66
79
  alias :render_collection_without_trace :render_collection
67
80
  def render_collection
68
81
  if ::AppPerfRpm::Tracer.tracing?
69
- AppPerfRpm::Tracer.trace("actionview") do |span|
70
- span.options = {
71
- "method" => "render_collection",
72
- "name" => @path
73
- }
82
+ span = AppPerfRpm.tracer.start_span("render_collection", tags: {
83
+ "component" => "ActionView",
84
+ "span.kind" => "client",
85
+ "view.controller" => @_request.path_parameters['controller'],
86
+ "view.action" => @_request.path_parameters['action'],
87
+ "view.template" => @path
88
+ })
89
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
90
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
91
+ end
74
92
 
75
- render_collection_without_trace
76
- end
77
- else
78
- render_collection_without_trace
93
+ render_collection_without_trace
94
+ rescue Exception => e
95
+ if span
96
+ span.set_tag('error', true)
97
+ span.log_error(e)
79
98
  end
99
+ raise
100
+ ensure
101
+ span.finish if span
80
102
  end
81
103
  end
82
104
 
@@ -99,25 +121,31 @@ if ::AppPerfRpm.configuration.instrumentation[:action_view][:enabled] && defined
99
121
  @path = path
100
122
  end
101
123
 
102
- AppPerfRpm::Tracer.trace("actionview") do |span|
103
- if layout
104
- span.options = {
105
- "method" => "render_with_layout",
106
- "name" => layout.identifier,
107
- "path" => @path,
108
- "layout" => layout
109
- }
110
- else
111
- span.options = {
112
- "method" => "render_without_layout",
113
- "path" => @path
114
- }
124
+ if layout
125
+ span = AppPerfRpm.tracer.start_span("render_with_layout")
126
+ # span.set_tag "view.layout", layout
127
+ span.set_tag "view.template", layout.identifier
128
+ else
129
+ span = AppPerfRpm.tracer.start_span("render_without_layout")
130
+ if @path
131
+ span.set_tag "view.template", @path.call
115
132
  end
116
- render_with_layout_without_trace(path, locals, *args, &block)
117
133
  end
118
- else
119
- render_with_layout_without_trace(path, locals, *args, &block)
134
+ span.set_tag "component", "ActionView"
135
+ span.set_tag "span.kind", "client"
136
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
137
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
138
+ end
139
+
140
+ render_with_layout_without_trace(path, locals, *args, &block)
141
+ rescue Exception => e
142
+ if span
143
+ span.set_tag('error', true)
144
+ span.log_error(e)
120
145
  end
146
+ raise
147
+ ensure
148
+ span.finish if span
121
149
  end
122
150
  end
123
151
  end
@@ -19,24 +19,33 @@ module AppPerfRpm
19
19
 
20
20
  def execute_with_trace(sql, name = nil)
21
21
  if ::AppPerfRpm::Tracer.tracing?
22
- if ignore_trace?(name)
23
- execute_without_trace(sql, name)
24
- else
25
- sanitized_sql = sanitize_sql(sql, :mysql2)
22
+ unless ignore_trace?(name)
23
+ adapter = connection_config.fetch(:adapter)
24
+ sanitized_sql = sanitize_sql(sql, adapter)
26
25
 
27
- AppPerfRpm::Tracer.trace('activerecord') do |span|
28
- span.options ={
29
- "adapter" => "mysql2",
30
- "query" => sanitized_sql,
31
- "name" => name
32
- }
33
-
34
- execute_without_trace(sql, name)
35
- end
26
+ span = AppPerfRpm.tracer.start_span(name || 'sql.query', tags: {
27
+ "component" => "ActiveRecord",
28
+ "span.kind" => "client",
29
+ "db.statement" => sanitized_sql,
30
+ "db.user" => connection_config.fetch(:username, 'unknown'),
31
+ "db.instance" => connection_config.fetch(:database),
32
+ "db.vendor" => adapter,
33
+ "db.type" => "sql"
34
+ })
35
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
36
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
36
37
  end
37
- else
38
- execute_without_trace(sql, name)
39
38
  end
39
+
40
+ execute_without_trace(sql, name)
41
+ rescue Exception => e
42
+ if span
43
+ span.set_tag('error', true)
44
+ span.log_error(e)
45
+ end
46
+ raise
47
+ ensure
48
+ span.finish if span
40
49
  end
41
50
  end
42
51
  end
@@ -19,81 +19,124 @@ module AppPerfRpm
19
19
 
20
20
  def exec_query_with_trace(sql, name = nil, binds = [])
21
21
  if ::AppPerfRpm::Tracer.tracing?
22
- if ignore_trace?(name)
23
- exec_query_without_trace(sql, name, binds)
24
- else
25
- sanitized_sql = sanitize_sql(sql, :postgres)
22
+ unless ignore_trace?(name)
23
+ adapter = connection_config.fetch(:adapter)
24
+ sanitized_sql = sanitize_sql(sql, adapter)
26
25
 
27
- AppPerfRpm::Tracer.trace('activerecord') do |span|
28
- span.options = {
29
- "adapter" => "postgresql",
30
- "query" => sanitized_sql,
31
- "name" => name
32
- }
33
- exec_query_without_trace(sql, name, binds)
34
- end
26
+ span = AppPerfRpm.tracer.start_span(name || 'sql.query', tags: {
27
+ "component" => "ActiveRecord",
28
+ "span.kind" => "client",
29
+ "db.statement" => sanitized_sql,
30
+ "db.user" => connection_config.fetch(:username, 'unknown'),
31
+ "db.instance" => connection_config.fetch(:database),
32
+ "db.vendor" => adapter,
33
+ "db.type" => "sql"
34
+ })
35
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
36
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
35
37
  end
36
- else
37
- exec_query_without_trace(sql, name, binds)
38
38
  end
39
+
40
+ exec_query_without_trace(sql, name, binds)
41
+ rescue Exception => e
42
+ if span
43
+ span.set_tag('error', true)
44
+ span.log_error(e)
45
+ end
46
+ raise
47
+ ensure
48
+ span.finish if span
39
49
  end
40
50
 
41
51
  def exec_delete_with_trace(sql, name = nil, binds = [])
42
52
  if ::AppPerfRpm::Tracer.tracing?
43
- if ignore_trace?(name)
44
- exec_delete_without_trace(sql, name, binds)
45
- else
46
- sanitized_sql = sanitize_sql(sql)
53
+ unless ignore_trace?(name)
54
+ adapter = connection_config.fetch(:adapter)
55
+ sanitized_sql = sanitize_sql(sql, adapter)
47
56
 
48
- AppPerfRpm::Tracer.trace('activerecord') do |span|
49
- span.options = {
50
- "adapter" => "postgresql",
51
- "query" => sanitized_sql,
52
- "name" => name
53
- }
54
- exec_delete_without_trace(sql, name, binds)
55
- end
57
+ span = AppPerfRpm.tracer.start_span(name || 'sql.query', tags: {
58
+ "component" => "ActiveRecord",
59
+ "span.kind" => "client",
60
+ "db.statement" => sanitized_sql,
61
+ "db.user" => connection_config.fetch(:username, 'unknown'),
62
+ "db.instance" => connection_config.fetch(:database),
63
+ "db.vendor" => adapter,
64
+ "db.type" => "sql"
65
+ })
66
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
67
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
56
68
  end
57
- else
58
- exec_delete_without_trace(sql, name, binds)
59
69
  end
70
+
71
+ exec_delete_without_trace(sql, name, binds)
72
+ rescue Exception => e
73
+ if span
74
+ span.set_tag('error', true)
75
+ span.log_error(e)
76
+ end
77
+ raise
78
+ ensure
79
+ span.finish if span
60
80
  end
61
81
 
62
82
  def exec_insert_with_trace(sql, name = nil, binds = [], *args)
63
83
  if ::AppPerfRpm::Tracer.tracing?
64
- if ignore_trace?(name)
65
- exec_insert_without_trace(sql, name, binds, *args)
66
- else
67
- sanitized_sql = sanitize_sql(sql, :postgres)
68
-
69
- AppPerfRpm::Tracer.trace('activerecord') do |span|
70
- span.options = {
71
- "adapter" => "postgresql",
72
- "query" => sanitized_sql,
73
- "name" => name
74
- }
84
+ unless ignore_trace?(name)
85
+ adapter = connection_config.fetch(:adapter)
86
+ sanitized_sql = sanitize_sql(sql, adapter)
75
87
 
76
- exec_insert_without_trace(sql, name, binds, *args)
77
- end
88
+ span = AppPerfRpm.tracer.start_span(name || 'sql.query', tags: {
89
+ "component" => "ActiveRecord",
90
+ "span.kind" => "client",
91
+ "db.statement" => sanitized_sql,
92
+ "db.user" => connection_config.fetch(:username, 'unknown'),
93
+ "db.instance" => connection_config.fetch(:database),
94
+ "db.vendor" => adapter,
95
+ "db.type" => "sql"
96
+ })
97
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
98
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
78
99
  end
79
- else
80
- exec_insert_without_trace(sql, name, binds, *args)
81
100
  end
101
+
102
+ exec_insert_without_trace(sql, name, binds, *args)
103
+ rescue Exception => e
104
+ if span
105
+ span.set_tag('error', true)
106
+ span.log_error(e)
107
+ end
108
+ raise
109
+ ensure
110
+ span.finish if span
82
111
  end
83
112
 
84
113
  def begin_db_transaction_with_trace
85
114
  if ::AppPerfRpm::Tracer.tracing?
86
- AppPerfRpm::Tracer.trace('activerecord') do |span|
87
- span.options = {
88
- "adapter" => "postgresql",
89
- "query" => "BEGIN"
90
- }
115
+ adapter = connection_config.fetch(:adapter)
116
+ sanitized_sql = sanitize_sql(sql, adapter)
91
117
 
92
- begin_db_transaction_without_trace
93
- end
94
- else
95
- begin_db_transaction_without_trace
118
+ span = AppPerfRpm.tracer.start_span('sql.query', tags: {
119
+ "component" => "ActiveRecord",
120
+ "span.kind" => "client",
121
+ "db.statement" => "BEGIN",
122
+ "db.user" => connection_config.fetch(:username, 'unknown'),
123
+ "db.instance" => connection_config.fetch(:database),
124
+ "db.vendor" => adapter,
125
+ "db.type" => "sql"
126
+ })
127
+ span.log(event: "backtrace", stack: ::AppPerfRpm::Backtrace.backtrace)
128
+ span.log(event: "source", stack: ::AppPerfRpm::Backtrace.source_extract)
129
+ end
130
+
131
+ begin_db_transaction_without_trace
132
+ rescue Exception => e
133
+ if span
134
+ span.set_tag('error', true)
135
+ span.log_error(e)
96
136
  end
137
+ raise
138
+ ensure
139
+ span.finish if span
97
140
  end
98
141
  end
99
142
  end