atatus 1.3.0 → 1.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +21 -0
  3. data/Gemfile +72 -22
  4. data/LICENSE +1 -1
  5. data/atatus.gemspec +2 -2
  6. data/lib/atatus/agent.rb +78 -29
  7. data/lib/atatus/central_config/cache_control.rb +18 -1
  8. data/lib/atatus/central_config.rb +72 -27
  9. data/lib/atatus/child_durations.rb +64 -0
  10. data/lib/atatus/collector/base.rb +102 -43
  11. data/lib/atatus/collector/builder.rb +134 -4
  12. data/lib/atatus/collector/hist.rb +54 -0
  13. data/lib/atatus/collector/layer.rb +1 -0
  14. data/lib/atatus/collector/transport.rb +41 -11
  15. data/lib/atatus/config/bytes.rb +17 -0
  16. data/lib/atatus/config/duration.rb +17 -0
  17. data/lib/atatus/config/options.rb +29 -9
  18. data/lib/atatus/config/regexp_list.rb +17 -0
  19. data/lib/atatus/config/wildcard_pattern_list.rb +64 -0
  20. data/lib/atatus/config.rb +129 -28
  21. data/lib/atatus/context/request/socket.rb +18 -1
  22. data/lib/atatus/context/request/url.rb +17 -0
  23. data/lib/atatus/context/request.rb +17 -0
  24. data/lib/atatus/context/response.rb +27 -2
  25. data/lib/atatus/context/user.rb +17 -0
  26. data/lib/atatus/context.rb +32 -1
  27. data/lib/atatus/context_builder.rb +19 -4
  28. data/lib/atatus/deprecations.rb +17 -0
  29. data/lib/atatus/error/exception.rb +24 -0
  30. data/lib/atatus/error/log.rb +17 -0
  31. data/lib/atatus/error.rb +27 -0
  32. data/lib/atatus/error_builder.rb +21 -2
  33. data/lib/atatus/grape.rb +62 -0
  34. data/lib/atatus/graphql.rb +91 -0
  35. data/lib/atatus/grpc.rb +99 -0
  36. data/lib/atatus/instrumenter.rb +135 -30
  37. data/lib/atatus/internal_error.rb +17 -0
  38. data/lib/atatus/logging.rb +17 -2
  39. data/lib/atatus/metadata/process_info.rb +17 -0
  40. data/lib/atatus/metadata/service_info.rb +21 -6
  41. data/lib/atatus/metadata/system_info/container_info.rb +49 -10
  42. data/lib/atatus/metadata/system_info/hw_info.rb +1 -1
  43. data/lib/atatus/metadata/system_info.rb +22 -3
  44. data/lib/atatus/metadata.rb +17 -0
  45. data/lib/atatus/metrics/breakdown_set.rb +31 -0
  46. data/lib/atatus/metrics/{cpu_mem.rb → cpu_mem_set.rb} +110 -63
  47. data/lib/atatus/metrics/metric.rb +140 -0
  48. data/lib/atatus/metrics/set.rb +123 -0
  49. data/lib/atatus/metrics/span_scoped_set.rb +56 -0
  50. data/lib/atatus/metrics/transaction_set.rb +26 -0
  51. data/lib/atatus/metrics/vm_set.rb +58 -0
  52. data/lib/atatus/metrics.rb +69 -27
  53. data/lib/atatus/metricset.rb +48 -4
  54. data/lib/atatus/middleware.rb +28 -8
  55. data/lib/atatus/naively_hashable.rb +17 -0
  56. data/lib/atatus/normalizers/grape/endpoint_run.rb +65 -0
  57. data/lib/atatus/normalizers/grape.rb +22 -0
  58. data/lib/atatus/normalizers/rails/action_controller.rb +44 -0
  59. data/lib/atatus/normalizers/rails/action_mailer.rb +43 -0
  60. data/lib/atatus/normalizers/{action_view.rb → rails/action_view.rb} +17 -0
  61. data/lib/atatus/normalizers/rails/active_record.rb +80 -0
  62. data/lib/atatus/normalizers/rails.rb +27 -0
  63. data/lib/atatus/normalizers.rb +23 -9
  64. data/lib/atatus/opentracing.rb +75 -42
  65. data/lib/atatus/rails.rb +29 -13
  66. data/lib/atatus/railtie.rb +19 -6
  67. data/lib/atatus/resque.rb +29 -0
  68. data/lib/atatus/sinatra.rb +53 -0
  69. data/lib/atatus/span/context/db.rb +43 -0
  70. data/lib/atatus/span/context/destination.rb +77 -0
  71. data/lib/atatus/span/context/http.rb +43 -0
  72. data/lib/atatus/span/context.rb +43 -28
  73. data/lib/atatus/span.rb +44 -15
  74. data/lib/atatus/span_helpers.rb +18 -1
  75. data/lib/atatus/spies/action_dispatch.rb +27 -6
  76. data/lib/atatus/spies/delayed_job.rb +26 -5
  77. data/lib/atatus/spies/dynamo_db.rb +62 -0
  78. data/lib/atatus/spies/elasticsearch.rb +53 -7
  79. data/lib/atatus/spies/faraday.rb +54 -20
  80. data/lib/atatus/spies/http.rb +36 -6
  81. data/lib/atatus/spies/json.rb +18 -0
  82. data/lib/atatus/spies/mongo.rb +41 -10
  83. data/lib/atatus/spies/net_http.rb +52 -11
  84. data/lib/atatus/spies/rake.rb +42 -23
  85. data/lib/atatus/spies/redis.rb +17 -0
  86. data/lib/atatus/spies/resque.rb +57 -0
  87. data/lib/atatus/spies/sequel.rb +54 -17
  88. data/lib/atatus/spies/shoryuken.rb +69 -0
  89. data/lib/atatus/spies/sidekiq.rb +46 -25
  90. data/lib/atatus/spies/sinatra.rb +20 -4
  91. data/lib/atatus/spies/sneakers.rb +74 -0
  92. data/lib/atatus/spies/sucker_punch.rb +58 -0
  93. data/lib/atatus/spies/tilt.rb +20 -1
  94. data/lib/atatus/spies.rb +33 -15
  95. data/lib/atatus/sql/signature.rb +169 -0
  96. data/lib/atatus/sql/tokenizer.rb +264 -0
  97. data/lib/atatus/sql/tokens.rb +63 -0
  98. data/lib/atatus/sql.rb +36 -0
  99. data/lib/atatus/sql_summarizer.rb +24 -6
  100. data/lib/atatus/stacktrace/frame.rb +17 -3
  101. data/lib/atatus/stacktrace.rb +17 -0
  102. data/lib/atatus/stacktrace_builder.rb +23 -3
  103. data/lib/atatus/subscriber.rb +23 -4
  104. data/lib/atatus/trace_context/traceparent.rb +111 -0
  105. data/lib/atatus/trace_context/tracestate.rb +148 -0
  106. data/lib/atatus/trace_context.rb +84 -51
  107. data/lib/atatus/transaction.rb +74 -18
  108. data/lib/atatus/transport/base.rb +44 -27
  109. data/lib/atatus/transport/connection/http.rb +58 -35
  110. data/lib/atatus/transport/connection/proxy_pipe.rb +24 -5
  111. data/lib/atatus/transport/connection.rb +28 -72
  112. data/lib/atatus/transport/filters/hash_sanitizer.rb +77 -0
  113. data/lib/atatus/transport/filters/secrets_filter.rb +30 -55
  114. data/lib/atatus/transport/filters.rb +18 -1
  115. data/lib/atatus/transport/headers.rb +83 -0
  116. data/lib/atatus/transport/serializers/context_serializer.rb +30 -3
  117. data/lib/atatus/transport/serializers/error_serializer.rb +17 -2
  118. data/lib/atatus/transport/serializers/metadata_serializer.rb +44 -22
  119. data/lib/atatus/transport/serializers/metricset_serializer.rb +34 -6
  120. data/lib/atatus/transport/serializers/span_serializer.rb +47 -12
  121. data/lib/atatus/transport/serializers/transaction_serializer.rb +18 -2
  122. data/lib/atatus/transport/serializers.rb +17 -5
  123. data/lib/atatus/transport/user_agent.rb +48 -0
  124. data/lib/atatus/transport/worker.rb +31 -7
  125. data/lib/atatus/util/inflector.rb +17 -0
  126. data/lib/atatus/util/lru_cache.rb +17 -0
  127. data/lib/atatus/util/throttle.rb +17 -0
  128. data/lib/atatus/util.rb +18 -1
  129. data/lib/atatus/version.rb +19 -1
  130. data/lib/atatus.rb +84 -16
  131. metadata +46 -26
  132. data/Rakefile +0 -19
  133. data/bench/.gitignore +0 -2
  134. data/bench/app.rb +0 -53
  135. data/bench/benchmark.rb +0 -36
  136. data/bench/report.rb +0 -55
  137. data/bench/rubyprof.rb +0 -39
  138. data/bench/stackprof.rb +0 -23
  139. data/bin/build_docs +0 -5
  140. data/bin/console +0 -15
  141. data/bin/setup +0 -8
  142. data/bin/with_framework +0 -7
  143. data/lib/atatus/metrics/vm.rb +0 -60
  144. data/lib/atatus/normalizers/action_controller.rb +0 -27
  145. data/lib/atatus/normalizers/action_mailer.rb +0 -26
  146. data/lib/atatus/normalizers/active_record.rb +0 -45
  147. data/lib/atatus/util/prefixed_logger.rb +0 -18
  148. data/vendor/.gitkeep +0 -0
@@ -1,3 +1,20 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
3
20
  require 'atatus'
@@ -22,8 +39,7 @@ module Atatus
22
39
  @span_context
23
40
  end
24
41
 
25
- # rubocop:disable Metrics/MethodLength
26
- def set_label(key, val)
42
+ def set_tag(key, val)
27
43
  if atatus_span.is_a?(Transaction)
28
44
  case key.to_s
29
45
  when 'type'
@@ -38,8 +54,9 @@ module Atatus
38
54
  else
39
55
  atatus_span.context.labels[key] = val
40
56
  end
57
+
58
+ self
41
59
  end
42
- # rubocop:enable Metrics/MethodLength
43
60
 
44
61
  def set_baggage_item(_key, _value)
45
62
  Atatus.agent.config.logger.warn(
@@ -65,28 +82,20 @@ module Atatus
65
82
  end
66
83
  # rubocop:enable Lint/UnusedMethodArgument
67
84
 
68
- # rubocop:disable Metrics/MethodLength
69
- def finish(clock_end: Util.monotonic_micros, end_time: nil)
70
- return unless (instrumenter = Atatus.agent&.instrumenter)
85
+ def finish(end_time: Time.now)
86
+ return unless (agent = Atatus.agent)
71
87
 
72
- if end_time
73
- warn '[Atatus] DEPRECATED: Setting a custom end time as a ' \
74
- '`Time` is deprecated. Use `clock_end:` and monotonic time instead.'
75
- clock_end = end_time
76
- end
77
-
78
- atatus_span.done clock_end: clock_end
88
+ atatus_span.done clock_end: Util.micros(end_time)
79
89
 
80
90
  case atatus_span
81
91
  when Atatus::Transaction
82
- instrumenter.current_transaction = nil
92
+ agent.instrumenter.current_transaction = nil
83
93
  when Atatus::Span
84
- instrumenter.current_spans.delete(atatus_span)
94
+ agent.instrumenter.current_spans.delete(atatus_span)
85
95
  end
86
96
 
87
- instrumenter.enqueue.call atatus_span
97
+ agent.enqueue atatus_span
88
98
  end
89
- # rubocop:enable Metrics/MethodLength
90
99
 
91
100
  private
92
101
 
@@ -101,6 +110,8 @@ module Atatus
101
110
 
102
111
  # @api private
103
112
  class SpanContext
113
+ extend Forwardable
114
+
104
115
  def initialize(trace_context:, baggage: nil)
105
116
  if baggage
106
117
  Atatus.agent.config.logger.warn(
@@ -112,10 +123,27 @@ module Atatus
112
123
  end
113
124
 
114
125
  attr_accessor :trace_context
126
+ def_delegators :trace_context, :trace_id, :id, :parent_id
127
+
128
+ def self.from_header(header)
129
+ return unless header
130
+
131
+ trace_context = Atatus::TraceContext.parse(header)
132
+ return unless trace_context
133
+
134
+ trace_context.traceparent.id = trace_context.parent_id
135
+ trace_context.traceparent.parent_id = nil
136
+
137
+ from_trace_context(trace_context)
138
+ end
115
139
 
116
140
  def self.from_trace_context(trace_context)
117
141
  new(trace_context: trace_context)
118
142
  end
143
+
144
+ def child
145
+ self.class.from_trace_context(trace_context.child)
146
+ end
119
147
  end
120
148
 
121
149
  # @api private
@@ -179,8 +207,6 @@ module Atatus
179
207
  @scope_stack.last
180
208
  end
181
209
  end
182
-
183
- # rubocop:disable Metrics/ClassLength
184
210
  # A custom tracer to use the OpenTracing API with Atatus
185
211
  class Tracer
186
212
  def initialize
@@ -193,13 +219,13 @@ module Atatus
193
219
  scope_manager.active&.span
194
220
  end
195
221
 
196
- # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
222
+ # rubocop:disable Metrics/ParameterLists
197
223
  def start_active_span(
198
224
  operation_name,
199
225
  child_of: nil,
200
226
  references: nil,
201
227
  start_time: Time.now,
202
- labels: {},
228
+ tags: {},
203
229
  ignore_active_scope: false,
204
230
  finish_on_close: true,
205
231
  **
@@ -209,14 +235,14 @@ module Atatus
209
235
  child_of: child_of,
210
236
  references: references,
211
237
  start_time: start_time,
212
- labels: labels,
238
+ tags: tags,
213
239
  ignore_active_scope: ignore_active_scope
214
240
  )
215
241
  scope = scope_manager.activate(span, finish_on_close: finish_on_close)
216
242
 
217
243
  if block_given?
218
244
  begin
219
- yield scope
245
+ return yield scope
220
246
  ensure
221
247
  scope.close
222
248
  end
@@ -224,16 +250,15 @@ module Atatus
224
250
 
225
251
  scope
226
252
  end
227
- # rubocop:enable Metrics/MethodLength, Metrics/ParameterLists
253
+ # rubocop:enable Metrics/ParameterLists
228
254
 
229
- # rubocop:disable Metrics/MethodLength, Metrics/ParameterLists
230
- # rubocop:disable Metrics/AbcSize
255
+ # rubocop:disable Metrics/ParameterLists
231
256
  def start_span(
232
257
  operation_name,
233
258
  child_of: nil,
234
259
  references: nil,
235
260
  start_time: Time.now,
236
- labels: {},
261
+ tags: {},
237
262
  ignore_active_scope: false,
238
263
  **
239
264
  )
@@ -245,7 +270,6 @@ module Atatus
245
270
 
246
271
  if span_context
247
272
  trace_context =
248
- span_context &&
249
273
  span_context.respond_to?(:trace_context) &&
250
274
  span_context.trace_context
251
275
  end
@@ -263,7 +287,7 @@ module Atatus
263
287
  )
264
288
  end
265
289
 
266
- # if no Atatus APM agent is running or transaction not sampled
290
+ # if no Atatus agent is running or transaction not sampled
267
291
  unless atatus_span
268
292
  return ::OpenTracing::Span::NOOP_INSTANCE
269
293
  end
@@ -271,7 +295,7 @@ module Atatus
271
295
  span_context ||=
272
296
  SpanContext.from_trace_context(atatus_span.trace_context)
273
297
 
274
- labels.each do |key, value|
298
+ tags.each do |key, value|
275
299
  atatus_span.context.labels[key] = value
276
300
  end
277
301
 
@@ -279,13 +303,14 @@ module Atatus
279
303
 
280
304
  Span.new(atatus_span, span_context)
281
305
  end
282
- # rubocop:enable Metrics/AbcSize
283
- # rubocop:enable Metrics/MethodLength, Metrics/ParameterLists
306
+
307
+ # rubocop:enable Metrics/ParameterLists
284
308
 
285
309
  def inject(span_context, format, carrier)
286
310
  case format
287
- when ::OpenTracing::FORMAT_RACK
288
- carrier['atatus-traceparent'] = span_context.to_header
311
+ when ::OpenTracing::FORMAT_RACK, ::OpenTracing::FORMAT_TEXT_MAP
312
+ carrier['atatus-traceparent'] =
313
+ span_context.traceparent.to_header
289
314
  else
290
315
  warn 'Only injection via HTTP headers and Rack is available'
291
316
  end
@@ -294,10 +319,16 @@ module Atatus
294
319
  def extract(format, carrier)
295
320
  case format
296
321
  when ::OpenTracing::FORMAT_RACK
297
- Atatus::TraceContext
298
- .parse(carrier['HTTP_ATATUS_APM_TRACEPARENT'])
322
+ SpanContext.from_header(
323
+ carrier['HTTP_ATATUS_TRACEPARENT']
324
+ )
325
+ when ::OpenTracing::FORMAT_TEXT_MAP
326
+ SpanContext.from_header(
327
+ carrier['atatus-traceparent']
328
+ )
299
329
  else
300
- warn 'Only extraction from HTTP headers via Rack is available'
330
+ warn 'Only extraction from HTTP headers via Rack or in ' \
331
+ 'text map format are available'
301
332
  nil
302
333
  end
303
334
  rescue Atatus::TraceContext::InvalidTraceparentHeader
@@ -311,9 +342,12 @@ module Atatus
311
342
  references:,
312
343
  ignore_active_scope:
313
344
  )
314
- context_from_child_of(child_of) ||
315
- context_from_references(references) ||
316
- context_from_active_scope(ignore_active_scope)
345
+ context = context_from_child_of(child_of) ||
346
+ context_from_references(references) ||
347
+ context_from_active_scope(ignore_active_scope)
348
+ return context.child if context&.respond_to?(:child)
349
+
350
+ context
317
351
  end
318
352
 
319
353
  def context_from_child_of(child_of)
@@ -334,13 +368,12 @@ module Atatus
334
368
  def context_from_active_scope(ignore_active_scope)
335
369
  if ignore_active_scope
336
370
  Atatus.agent&.config&.logger&.warn(
337
- 'ignore_active_scope might lead to unexpeced results'
371
+ 'ignore_active_scope might lead to unexpected results'
338
372
  )
339
373
  return
340
374
  end
341
375
  @scope_manager.active&.span&.context
342
376
  end
343
377
  end
344
- # rubocop:enable Metrics/ClassLength
345
378
  end
346
379
  end
data/lib/atatus/rails.rb CHANGED
@@ -1,14 +1,31 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
1
18
  # frozen_string_literal: true
2
19
 
20
+ require 'atatus/railtie'
3
21
  require 'atatus/subscriber'
22
+ require 'atatus/normalizers/rails'
4
23
 
5
24
  module Atatus
6
25
  # Module for explicitly starting the Atatus agent and hooking into Rails.
7
26
  # It is recommended to use the Railtie instead.
8
27
  module Rails
9
28
  extend self
10
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
11
- # rubocop:disable Metrics/CyclomaticComplexity
12
29
  # Start the Atatus agent and hook into Rails.
13
30
  # Note that the agent won't be started if the Rails console is being used.
14
31
  #
@@ -16,11 +33,13 @@ module Atatus
16
33
  # @return [true, nil] true if the agent was started, nil otherwise.
17
34
  def start(config)
18
35
  config = Config.new(config) unless config.is_a?(Config)
36
+
19
37
  if (reason = should_skip?(config))
20
38
  unless config.disable_start_message?
21
39
  config.logger.info "Skipping because: #{reason}. " \
22
40
  "Start manually with `Atatus.start'"
23
41
  end
42
+
24
43
  return
25
44
  end
26
45
 
@@ -28,24 +47,21 @@ module Atatus
28
47
  attach_subscriber(agent)
29
48
  end
30
49
 
31
- if Atatus.running? &&
32
- !Atatus.agent.config.disabled_instrumentations.include?(
33
- 'action_dispatch'
34
- )
35
- require 'atatus/spies/action_dispatch'
36
- end
37
50
  Atatus.running?
38
51
  rescue StandardError => e
39
- config.logger.error format('Failed to start: %s', e.message)
40
- config.logger.error "Backtrace:\n" + e.backtrace.join("\n")
52
+ if config.disable_start_message?
53
+ config.logger.error format('Failed to start: %s', e.message)
54
+ config.logger.error "Backtrace:\n" + e.backtrace.join("\n")
55
+ else
56
+ puts format('Failed to start: %s', e.message)
57
+ puts "Backtrace:\n" + e.backtrace.join("\n")
58
+ end
41
59
  end
42
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
43
- # rubocop:enable Metrics/CyclomaticComplexity
44
60
 
45
61
  private
46
62
 
47
63
  def should_skip?(_config)
48
- if ::Rails.const_defined? 'Rails::Console'
64
+ if ::Rails.const_defined?('Console', false)
49
65
  return 'Rails console'
50
66
  end
51
67
 
@@ -1,6 +1,21 @@
1
- # frozen_string_literal: true
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
2
17
 
3
- require 'atatus/rails'
18
+ # frozen_string_literal: true
4
19
 
5
20
  module Atatus
6
21
  # @api private
@@ -9,13 +24,11 @@ module Atatus
9
24
 
10
25
  Config.schema.each do |key, args|
11
26
  next unless args.length > 1
12
- config.atatus[key] = args.last[:default]
27
+ config.atatus[key] = args[:default]
13
28
  end
14
29
 
15
30
  initializer 'atatus.initialize' do |app|
16
- config = Config.new(app.config.atatus).tap do |c|
17
- c.app = app
18
-
31
+ config = Config.new(app.config.atatus.merge(app: app)).tap do |c|
19
32
  # Prepend Rails.root to log_path if present
20
33
  if c.log_path && !c.log_path.start_with?('/')
21
34
  c.log_path = ::Rails.root.join(c.log_path)
@@ -0,0 +1,29 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
20
+ module Atatus
21
+ # Defines a before_first_fork hook for starting the Atatus agent
22
+ # with Resque.
23
+ module Resque
24
+ ::Resque.before_first_fork do
25
+ ::Resque.logger.debug('Starting Atatus agent')
26
+ Atatus.start
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
20
+ module Atatus
21
+ # Module for starting the Atatus agent and hooking into Sinatra.
22
+ module Sinatra
23
+ extend self
24
+ # Start the Atatus agent and hook into Sinatra.
25
+ #
26
+ # @param app [Sinatra::Base] A Sinatra app.
27
+ # @param config [Config, Hash] An instance of Config or a Hash config.
28
+ # @return [true, nil] true if the agent was started, nil otherwise.
29
+ def start(app, config = {})
30
+ config = Config.new(config) unless config.is_a?(Config)
31
+ configure_app(app, config)
32
+
33
+ Atatus.start(config)
34
+ Atatus.running?
35
+ rescue StandardError => e
36
+ config.logger.error format('Failed to start: %s', e.message)
37
+ config.logger.debug "Backtrace:\n" + e.backtrace.join("\n")
38
+ end
39
+
40
+ private
41
+
42
+ def configure_app(app, config)
43
+ config.service_name ||= format_name(app.to_s)
44
+ config.framework_name ||= 'Sinatra'
45
+ config.framework_version ||= ::Sinatra::VERSION
46
+ config.__root_path ||= Dir.pwd
47
+ end
48
+
49
+ def format_name(str)
50
+ str&.gsub('::', '_')
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
20
+ module Atatus
21
+ class Span
22
+ class Context
23
+ # @api private
24
+ class Db
25
+ def initialize(
26
+ instance: nil,
27
+ statement: nil,
28
+ type: nil,
29
+ user: nil,
30
+ rows_affected: nil
31
+ )
32
+ @instance = instance
33
+ @statement = statement
34
+ @type = type
35
+ @user = user
36
+ @rows_affected = rows_affected
37
+ end
38
+
39
+ attr_accessor :instance, :statement, :type, :user, :rows_affected
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,77 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
20
+ module Atatus
21
+ class Span
22
+ class Context
23
+ # @api private
24
+ class Destination
25
+ def initialize(
26
+ name: nil,
27
+ resource: nil,
28
+ type: nil,
29
+ address: nil,
30
+ port: nil
31
+ )
32
+ @name = name
33
+ @resource = resource
34
+ @type = type
35
+ @address = address
36
+ @port = port
37
+ end
38
+
39
+ attr_reader(
40
+ :name,
41
+ :resource,
42
+ :type,
43
+ :address,
44
+ :port
45
+ )
46
+
47
+ def self.from_uri(uri_or_str, type: 'external', port: nil)
48
+ uri = normalize(uri_or_str)
49
+
50
+ new(
51
+ name: only_scheme_and_host(uri),
52
+ resource: "#{uri.host}:#{uri.port}",
53
+ type: type,
54
+ address: uri.hostname,
55
+ port: port || uri.port
56
+ )
57
+ end
58
+
59
+ def self.only_scheme_and_host(uri_or_str)
60
+ uri = normalize(uri_or_str)
61
+ uri.path = ''
62
+ uri.password = uri.query = uri.fragment = nil
63
+ uri.to_s
64
+ end
65
+
66
+ class << self
67
+ private
68
+
69
+ def normalize(uri_or_str)
70
+ return uri_or_str.dup if uri_or_str.is_a?(URI)
71
+ URI(uri_or_str)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,43 @@
1
+ # Licensed to Elasticsearch B.V. under one or more contributor
2
+ # license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright
4
+ # ownership. Elasticsearch B.V. licenses this file to you under
5
+ # the Apache License, Version 2.0 (the "License"); you may
6
+ # not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing,
12
+ # software distributed under the License is distributed on an
13
+ # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ # KIND, either express or implied. See the License for the
15
+ # specific language governing permissions and limitations
16
+ # under the License.
17
+
18
+ # frozen_string_literal: true
19
+
20
+ module Atatus
21
+ class Span
22
+ class Context
23
+ # @api private
24
+ class Http
25
+ def initialize(url: nil, status_code: nil, method: nil)
26
+ @url = sanitize_url(url)
27
+ @status_code = status_code
28
+ @method = method
29
+ end
30
+
31
+ attr_accessor :url, :status_code, :method
32
+
33
+ private
34
+
35
+ def sanitize_url(uri_or_str)
36
+ uri = uri_or_str.is_a?(URI) ? uri_or_str.dup : URI(uri_or_str)
37
+ uri.password = nil
38
+ uri.to_s
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end