baseline_red_rpm 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/lib/baseline_red_rpm.rb +164 -0
  3. data/lib/baseline_red_rpm/backtrace.rb +117 -0
  4. data/lib/baseline_red_rpm/configuration.rb +63 -0
  5. data/lib/baseline_red_rpm/instrumentation.rb +23 -0
  6. data/lib/baseline_red_rpm/instruments/action_controller.rb +70 -0
  7. data/lib/baseline_red_rpm/instruments/action_view.rb +222 -0
  8. data/lib/baseline_red_rpm/instruments/active_model_serializer.rb +37 -0
  9. data/lib/baseline_red_rpm/instruments/active_record.rb +66 -0
  10. data/lib/baseline_red_rpm/instruments/active_record/adapters/mysql2.rb +55 -0
  11. data/lib/baseline_red_rpm/instruments/active_record/adapters/postgresql.rb +143 -0
  12. data/lib/baseline_red_rpm/instruments/active_record/adapters/sqlite3.rb +142 -0
  13. data/lib/baseline_red_rpm/instruments/activerecord_import.rb +57 -0
  14. data/lib/baseline_red_rpm/instruments/emque_consuming.rb +41 -0
  15. data/lib/baseline_red_rpm/instruments/faraday.rb +48 -0
  16. data/lib/baseline_red_rpm/instruments/grape.rb +63 -0
  17. data/lib/baseline_red_rpm/instruments/net_http.rb +43 -0
  18. data/lib/baseline_red_rpm/instruments/rack.rb +129 -0
  19. data/lib/baseline_red_rpm/instruments/redis.rb +75 -0
  20. data/lib/baseline_red_rpm/instruments/roda.rb +48 -0
  21. data/lib/baseline_red_rpm/instruments/sequel.rb +100 -0
  22. data/lib/baseline_red_rpm/instruments/sidekiq.rb +100 -0
  23. data/lib/baseline_red_rpm/instruments/sinatra.rb +82 -0
  24. data/lib/baseline_red_rpm/instruments/typhoeus.rb +74 -0
  25. data/lib/baseline_red_rpm/introspector.rb +53 -0
  26. data/lib/baseline_red_rpm/logger.rb +34 -0
  27. data/lib/baseline_red_rpm/rails.rb +15 -0
  28. data/lib/baseline_red_rpm/railtie.rb +19 -0
  29. data/lib/baseline_red_rpm/reporters/json_client.rb +69 -0
  30. data/lib/baseline_red_rpm/reporters/null_client.rb +16 -0
  31. data/lib/baseline_red_rpm/tracer.rb +75 -0
  32. data/lib/baseline_red_rpm/tracing/buffer.rb +27 -0
  33. data/lib/baseline_red_rpm/tracing/carrier.rb +25 -0
  34. data/lib/baseline_red_rpm/tracing/collector.rb +33 -0
  35. data/lib/baseline_red_rpm/tracing/endpoint.rb +21 -0
  36. data/lib/baseline_red_rpm/tracing/managed_span.rb +40 -0
  37. data/lib/baseline_red_rpm/tracing/managed_tracer.rb +36 -0
  38. data/lib/baseline_red_rpm/tracing/span.rb +72 -0
  39. data/lib/baseline_red_rpm/tracing/span_context.rb +43 -0
  40. data/lib/baseline_red_rpm/tracing/thread_span_stack.rb +34 -0
  41. data/lib/baseline_red_rpm/tracing/trace_id.rb +13 -0
  42. data/lib/baseline_red_rpm/tracing/tracer.rb +100 -0
  43. data/lib/baseline_red_rpm/utils.rb +45 -0
  44. data/lib/tasks/install.rake +6 -0
  45. metadata +212 -0
@@ -0,0 +1,222 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ::BaselineRedRpm.config.instrumentation[:action_view][:enabled] && defined?(::ActionView)
4
+ if defined?(Rails) && Rails::VERSION::MAJOR == 2
5
+ ActionView::Partials.module_eval do
6
+ alias :render_partial_without_trace :render_partial
7
+ def render_partial(options = {})
8
+ if ::BaselineRedRpm::Tracer.tracing? && options.key?(:partial) && options[:partial].is_a?(String)
9
+ span = BaselineRedRpm.tracer.start_span("render_partial", tags: {
10
+ "component" => "ActionView",
11
+ "span.kind" => "client",
12
+ "view.controller" => @_request.path_parameters['controller'],
13
+ "view.action" => @_request.path_parameters['action'],
14
+ "view.template" => options[:partial]
15
+ })
16
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
17
+ end
18
+
19
+ render_partial_without_trace(options)
20
+ rescue Exception => e
21
+ if span
22
+ span.set_tag('error', true)
23
+ span.log_error(e)
24
+ end
25
+ raise
26
+ ensure
27
+ span.finish if span
28
+ end
29
+
30
+ alias :render_partial_collection_without_trace :render_partial_collection
31
+ def render_partial_collection(options = {})
32
+ if ::BaselineRedRpm::Tracer.tracing?
33
+ span = BaselineRedRpm.tracer.start_span("render_partial_collection", tags: {
34
+ "component" => "ActionView",
35
+ "span.kind" => "client",
36
+ "view.controller" => @_request.path_parameters['controller'],
37
+ "view.action" => @_request.path_parameters['action'],
38
+ "view.template" => @path
39
+ })
40
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
41
+ end
42
+
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)
48
+ end
49
+ raise
50
+ ensure
51
+ span.finish if span
52
+ end
53
+ end
54
+ end
55
+
56
+ if defined?(Rails) && Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR == 0
57
+ ::ActionView::Partials::PartialRenderer.class_eval do
58
+ alias :render_partial_without_trace :render_partial
59
+ def render_partial
60
+ if ::BaselineRedRpm::Tracer.tracing?
61
+ span = BaselineRedRpm.tracer.start_span("render_partial", tags: {
62
+ "component" => "ActionView",
63
+ "span.kind" => "client",
64
+ "view.template" => @options[:partial]
65
+ })
66
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
67
+ end
68
+
69
+ render_partial_without_trace
70
+ rescue Exception => e
71
+ if span
72
+ span.set_tag('error', true)
73
+ span.log_error(e)
74
+ end
75
+ raise
76
+ ensure
77
+ span.finish if span
78
+ end
79
+
80
+ alias :render_collection_without_trace :render_collection
81
+ def render_collection
82
+ if ::BaselineRedRpm::Tracer.tracing?
83
+ span = BaselineRedRpm.tracer.start_span("render_collection", tags: {
84
+ "component" => "ActionView",
85
+ "span.kind" => "client",
86
+ "view.template" => @path
87
+ })
88
+ if @_request
89
+ span.set_tag('view.controller', @_request.path_parameters['controller'])
90
+ span.set_tag('view.action', @_request.path_parameters['action'])
91
+ end
92
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
93
+ end
94
+
95
+ render_collection_without_trace
96
+ rescue Exception => e
97
+ if span
98
+ span.set_tag('error', true)
99
+ span.log_error(e)
100
+ end
101
+ raise
102
+ ensure
103
+ span.finish if span
104
+ end
105
+ end
106
+
107
+ ::ActionView::Rendering.class_eval do
108
+ alias :_render_template_without_trace _render_template
109
+
110
+ def _render_template(template, layout = nil, options = {})
111
+ if ::BaselineRedRpm::Tracer.tracing?
112
+ span = BaselineRedRpm.tracer.start_span("render_template")
113
+ span.set_tag "view.template", template
114
+ span.set_tag "view.layout", layout
115
+ span.set_tag "component", "ActionView"
116
+ span.set_tag "span.kind", "client"
117
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
118
+ end
119
+
120
+ _render_template_without_trace(template, layout, options)
121
+ rescue Exception => e
122
+ if span
123
+ span.set_tag('error', true)
124
+ span.log_error(e)
125
+ end
126
+ raise
127
+ ensure
128
+ span.finish if span
129
+ end
130
+ end
131
+ end
132
+
133
+ if defined?(Rails) && Rails.version >= '3.1.0'
134
+ ActionView::PartialRenderer.class_eval do
135
+ alias :render_partial_without_trace :render_partial
136
+ def render_partial
137
+ if ::BaselineRedRpm::Tracer.tracing?
138
+ span = BaselineRedRpm.tracer.start_span("render_partial", tags: {
139
+ "component" => "ActionView",
140
+ "span.kind" => "client",
141
+ "view.template" => @options[:partial]
142
+ })
143
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
144
+ end
145
+
146
+ render_partial_without_trace
147
+ rescue Exception => e
148
+ if span
149
+ span.set_tag('error', true)
150
+ span.log_error(e)
151
+ end
152
+ raise
153
+ ensure
154
+ span.finish if span
155
+ end
156
+
157
+ alias :render_collection_without_trace :render_collection
158
+ def render_collection
159
+ if ::BaselineRedRpm::Tracer.tracing?
160
+ span = BaselineRedRpm.tracer.start_span("render_collection", tags: {
161
+ "component" => "ActionView",
162
+ "span.kind" => "client",
163
+ "view.template" => @path
164
+ })
165
+ if @_request
166
+ span.set_tag('view.controller', @_request.path_parameters['controller'])
167
+ span.set_tag('view.action', @_request.path_parameters['action'])
168
+ end
169
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
170
+ end
171
+
172
+ render_collection_without_trace
173
+ rescue Exception => e
174
+ if span
175
+ span.set_tag('error', true)
176
+ span.log_error(e)
177
+ end
178
+ raise
179
+ ensure
180
+ span.finish if span
181
+ end
182
+ end
183
+
184
+ ::ActionView::TemplateRenderer.class_eval do
185
+ alias :render_template_without_trace :render_template
186
+
187
+ def render_template(template, layout_name = nil, locals = {})
188
+ if ::BaselineRedRpm::Tracer.tracing?
189
+ layout = if layout_name
190
+ if layout_name.is_a?(String)
191
+ layout_name
192
+ elsif layout_name.is_a?(Proc)
193
+ layout_name.call
194
+ elsif method(:find_layout).arity == 3
195
+ find_layout(layout_name, locals, [formats.first])
196
+ elsif locals
197
+ find_layout(layout_name, locals)
198
+ end
199
+ end
200
+ span = BaselineRedRpm.tracer.start_span("render_template")
201
+ span.set_tag "view.layout", layout ? layout.inspect : ""
202
+ span.set_tag "view.template", template.inspect
203
+ span.set_tag "component", "ActionView"
204
+ span.set_tag "span.kind", "client"
205
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :action_view)
206
+ end
207
+
208
+ render_template_without_trace(template, layout_name, locals)
209
+ rescue Exception => e
210
+ if span
211
+ span.set_tag('error', true)
212
+ span.log_error(e)
213
+ end
214
+ raise
215
+ ensure
216
+ span.finish if span
217
+ end
218
+ end
219
+ end
220
+
221
+ BaselineRedRpm.logger.info "Initializing actionview tracer."
222
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ::BaselineRedRpm.config.instrumentation[:action_view][:enabled] &&
4
+ defined?(::ActiveModel) &&
5
+ defined?(::ActiveModel::Serializer) &&
6
+ defined?(::ActiveModel::Serializer::CollectionSerializer) &&
7
+ defined?(::ActiveModel::Serializer::ArraySerializer)
8
+ [
9
+ ::ActiveModel::Serializer,
10
+ ::ActiveModel::Serializer::CollectionSerializer,
11
+ ::ActiveModel::Serializer::ArraySerializer
12
+ ].each do |klass|
13
+ klass.class_eval do
14
+ alias :as_json_without_trace :as_json
15
+ def as_json(*args)
16
+ if ::BaselineRedRpm::Tracer.tracing?
17
+ span = BaselineRedRpm.tracer.start_span("ActiveModel::Serializer", tags: {
18
+ "serializer" => self.class.to_s
19
+ })
20
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_model_serializer)
21
+ end
22
+
23
+ as_json_without_trace(*args)
24
+ rescue Exception => e
25
+ if span
26
+ span.set_tag('error', true)
27
+ span.log_error(e)
28
+ end
29
+ raise
30
+ ensure
31
+ span.finish if span
32
+ end
33
+ end
34
+ end
35
+
36
+ BaselineRedRpm.logger.info "Initializing activemodel/serializer tracer."
37
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BaselineRedRpm
4
+ module Instruments
5
+ module ActiveRecord
6
+ module Adapters
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ if ::BaselineRedRpm.config.instrumentation[:active_record][:enabled] &&
13
+ defined?(::ActiveRecord)
14
+ BaselineRedRpm.logger.info "Initializing activerecord tracer."
15
+
16
+ if defined?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter)
17
+ require 'baseline_red_rpm/instruments/active_record/adapters/sqlite3'
18
+ ::ActiveRecord::ConnectionAdapters::SQLite3Adapter.send(:include,
19
+ ::BaselineRedRpm::Instruments::ActiveRecord::Adapters::Sqlite3
20
+ )
21
+ ::ActiveRecord::ConnectionAdapters::SQLite3Adapter.class_eval do
22
+ if Rails.version < "3.1"
23
+ alias_method :exec_query_without_trace, :execute
24
+ alias_method :execute, :exec_query_with_trace
25
+ else
26
+ alias_method :exec_query_without_trace, :exec_query
27
+ alias_method :exec_query, :exec_query_with_trace
28
+
29
+ alias_method :exec_delete_without_trace, :exec_delete
30
+ alias_method :exec_delete, :exec_delete_with_trace
31
+ end
32
+ end
33
+ end
34
+
35
+ if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
36
+ require 'baseline_red_rpm/instruments/active_record/adapters/postgresql'
37
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include,
38
+ ::BaselineRedRpm::Instruments::ActiveRecord::Adapters::Postgresql
39
+ )
40
+
41
+ ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
42
+ if Rails.version < "3.1"
43
+ alias_method :exec_query_without_trace, :execute
44
+ alias_method :execute, :exec_query_with_trace
45
+ else
46
+ alias_method :exec_query_without_trace, :exec_query
47
+ alias_method :exec_query, :exec_query_with_trace
48
+
49
+ alias_method :exec_delete_without_trace, :exec_delete
50
+ alias_method :exec_delete, :exec_delete_with_trace
51
+ end
52
+ end
53
+ end
54
+
55
+ if defined?(::ActiveRecord::ConnectionAdapters::Mysql2Adapter)
56
+ require 'baseline_red_rpm/instruments/active_record/adapters/mysql2'
57
+ ::ActiveRecord::ConnectionAdapters::Mysql2Adapter.send(:include,
58
+ ::BaselineRedRpm::Instruments::ActiveRecord::Adapters::Mysql2
59
+ )
60
+
61
+ ::ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
62
+ alias_method :execute_without_trace, :execute
63
+ alias_method :execute, :execute_with_trace
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BaselineRedRpm
4
+ module Instruments
5
+ module ActiveRecord
6
+ module Adapters
7
+ module Mysql2
8
+ include BaselineRedRpm::Utils
9
+
10
+ IGNORE_STATEMENTS = {
11
+ "SCHEMA" => true,
12
+ "EXPLAIN" => true,
13
+ "CACHE" => true
14
+ }
15
+
16
+ def ignore_trace?(name)
17
+ IGNORE_STATEMENTS[name.to_s] ||
18
+ (name && name.to_sym == :skip_logging) ||
19
+ name == 'ActiveRecord::SchemaMigration Load'
20
+ end
21
+
22
+ def execute_with_trace(sql, name = nil)
23
+ if ::BaselineRedRpm::Tracer.tracing?
24
+ unless ignore_trace?(name)
25
+ adapter = connection_config.fetch(:adapter)
26
+ sanitized_sql = sanitize_sql(sql, adapter)
27
+
28
+ span = BaselineRedRpm.tracer.start_span(name || 'SQL', tags: {
29
+ "component" => "ActiveRecord",
30
+ "span.kind" => "client",
31
+ "db.statement" => sanitized_sql,
32
+ "db.user" => connection_config.fetch(:username, 'unknown'),
33
+ "db.instance" => connection_config.fetch(:database),
34
+ "db.vendor" => adapter,
35
+ "db.type" => "sql"
36
+ })
37
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_record)
38
+ end
39
+ end
40
+
41
+ execute_without_trace(sql, name)
42
+ rescue Exception => e
43
+ if span
44
+ span.set_tag('error', true)
45
+ span.log_error(e)
46
+ end
47
+ raise
48
+ ensure
49
+ span.finish if span
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,143 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BaselineRedRpm
4
+ module Instruments
5
+ module ActiveRecord
6
+ module Adapters
7
+ module Postgresql
8
+ include BaselineRedRpm::Utils
9
+
10
+ IGNORE_STATEMENTS = {
11
+ "SCHEMA" => true,
12
+ "EXPLAIN" => true,
13
+ "CACHE" => true
14
+ }
15
+
16
+ def ignore_trace?(name)
17
+ IGNORE_STATEMENTS[name.to_s] ||
18
+ (name && name.to_sym == :skip_logging) ||
19
+ name == 'ActiveRecord::SchemaMigration Load'
20
+ end
21
+
22
+ def exec_query_with_trace(sql, name = nil, *args)
23
+ if ::BaselineRedRpm::Tracer.tracing?
24
+ unless ignore_trace?(name)
25
+ adapter = connection_config.fetch(:adapter)
26
+ sanitized_sql = sanitize_sql(sql, adapter)
27
+
28
+ span = BaselineRedRpm.tracer.start_span(name || 'SQL', tags: {
29
+ "component" => "ActiveRecord",
30
+ "span.kind" => "client",
31
+ "db.statement" => sanitized_sql,
32
+ "db.user" => connection_config.fetch(:username, 'unknown'),
33
+ "db.instance" => connection_config.fetch(:database),
34
+ "db.vendor" => adapter,
35
+ "db.type" => "sql"
36
+ })
37
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_record)
38
+ end
39
+ end
40
+
41
+ exec_query_without_trace(sql, name, *args)
42
+ rescue Exception => e
43
+ if span
44
+ span.set_tag('error', true)
45
+ span.log_error(e)
46
+ end
47
+ raise
48
+ ensure
49
+ span.finish if span
50
+ end
51
+
52
+ def exec_delete_with_trace(sql, name = nil, *args)
53
+ if ::BaselineRedRpm::Tracer.tracing?
54
+ unless ignore_trace?(name)
55
+ adapter = connection_config.fetch(:adapter)
56
+ sanitized_sql = sanitize_sql(sql, adapter)
57
+
58
+ span = BaselineRedRpm.tracer.start_span(name || 'SQL', tags: {
59
+ "component" => "ActiveRecord",
60
+ "span.kind" => "client",
61
+ "db.statement" => sanitized_sql,
62
+ "db.user" => connection_config.fetch(:username, 'unknown'),
63
+ "db.instance" => connection_config.fetch(:database),
64
+ "db.vendor" => adapter,
65
+ "db.type" => "sql"
66
+ })
67
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_record)
68
+ end
69
+ end
70
+
71
+ exec_delete_without_trace(sql, name, *args)
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
80
+ end
81
+
82
+ def exec_insert_with_trace(sql, name = nil, *args)
83
+ if ::BaselineRedRpm::Tracer.tracing?
84
+ unless ignore_trace?(name)
85
+ adapter = connection_config.fetch(:adapter)
86
+ sanitized_sql = sanitize_sql(sql, adapter)
87
+
88
+ span = BaselineRedRpm.tracer.start_span(name || 'SQL', 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
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_record)
98
+ end
99
+ end
100
+
101
+ exec_insert_without_trace(sql, name, *args)
102
+ rescue Exception => e
103
+ if span
104
+ span.set_tag('error', true)
105
+ span.log_error(e)
106
+ end
107
+ raise
108
+ ensure
109
+ span.finish if span
110
+ end
111
+
112
+ def begin_db_transaction_with_trace
113
+ if ::BaselineRedRpm::Tracer.tracing?
114
+ adapter = connection_config.fetch(:adapter)
115
+ sanitized_sql = sanitize_sql(sql, adapter)
116
+
117
+ span = BaselineRedRpm.tracer.start_span('SQL', tags: {
118
+ "component" => "ActiveRecord",
119
+ "span.kind" => "client",
120
+ "db.statement" => "BEGIN",
121
+ "db.user" => connection_config.fetch(:username, 'unknown'),
122
+ "db.instance" => connection_config.fetch(:database),
123
+ "db.vendor" => adapter,
124
+ "db.type" => "sql"
125
+ })
126
+ BaselineRedRpm::Utils.log_source_and_backtrace(span, :active_record)
127
+ end
128
+
129
+ begin_db_transaction_without_trace
130
+ rescue Exception => e
131
+ if span
132
+ span.set_tag('error', true)
133
+ span.log_error(e)
134
+ end
135
+ raise
136
+ ensure
137
+ span.finish if span
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end