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,40 +1,55 @@
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
  module Atatus
4
21
  class Span
5
22
  # @api private
6
23
  class Context
7
- def initialize(db: nil, http: nil, labels: {})
8
- @sync = true
9
- @db = db && Db.new(db)
10
- @http = http && Http.new(http)
24
+ def initialize(
25
+ db: nil,
26
+ destination: nil,
27
+ http: nil,
28
+ labels: {},
29
+ sync: nil
30
+ )
31
+ @sync = sync
32
+ @db = db && Db.new(**db)
33
+ @http = http && Http.new(**http)
34
+ @destination =
35
+ case destination
36
+ when Destination then destination
37
+ when Hash then Destination.new(**destination)
38
+ end
11
39
  @labels = labels
12
40
  end
13
41
 
14
- attr_accessor :sync, :db, :http, :labels
15
-
16
- # @api private
17
- class Db
18
- def initialize(instance: nil, statement: nil, type: nil, user: nil)
19
- @instance = instance
20
- @statement = statement
21
- @type = type
22
- @user = user
23
- end
24
-
25
- attr_accessor :instance, :statement, :type, :user
26
- end
27
-
28
- # @api private
29
- class Http
30
- def initialize(url: nil, status_code: nil, method: nil)
31
- @url = url
32
- @status_code = status_code
33
- @method = method
34
- end
35
-
36
- attr_accessor :url, :status_code, :method
37
- end
42
+ attr_reader(
43
+ :db,
44
+ :destination,
45
+ :http,
46
+ :labels,
47
+ :sync
48
+ )
38
49
  end
39
50
  end
40
51
  end
52
+
53
+ require 'atatus/span/context/db'
54
+ require 'atatus/span/context/http'
55
+ require 'atatus/span/context/destination'
data/lib/atatus/span.rb CHANGED
@@ -1,7 +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 'securerandom'
4
- require 'forwardable'
18
+ # frozen_string_literal: true
5
19
 
6
20
  require 'atatus/span/context'
7
21
 
@@ -9,22 +23,24 @@ module Atatus
9
23
  # @api private
10
24
  class Span
11
25
  extend Forwardable
12
-
13
- def_delegators :@trace_context, :trace_id, :parent_id, :id
26
+ include ChildDurations::Methods
14
27
 
15
28
  DEFAULT_TYPE = 'Custom'
16
29
  DEFAULT_SUBTYPE = 'Ruby'
17
30
 
18
- # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
31
+ # rubocop:disable Metrics/ParameterLists
19
32
  def initialize(
20
33
  name:,
21
- transaction_id:,
34
+ transaction:,
22
35
  trace_context:,
36
+ parent:,
23
37
  type: nil,
24
38
  subtype: nil,
25
39
  action: nil,
26
40
  context: nil,
27
- stacktrace_builder: nil
41
+ stacktrace_builder: nil,
42
+ sync: nil,
43
+ sample_rate: nil
28
44
  )
29
45
  @name = name
30
46
 
@@ -36,13 +52,18 @@ module Atatus
36
52
  @action = action
37
53
  end
38
54
 
39
- @transaction_id = transaction_id
40
- @trace_context = trace_context
55
+ @transaction = transaction
56
+ @transaction_id = transaction.id
57
+ @parent = parent
58
+ @trace_context = trace_context || parent.trace_context.child
59
+ @sample_rate = transaction.sample_rate
41
60
 
42
- @context = context || Span::Context.new
61
+ @context = context || Span::Context.new(sync: sync)
43
62
  @stacktrace_builder = stacktrace_builder
44
63
  end
45
- # rubocop:enable Metrics/ParameterLists, Metrics/MethodLength
64
+ # rubocop:enable Metrics/ParameterLists
65
+
66
+ def_delegators :@trace_context, :trace_id, :parent_id, :id
46
67
 
47
68
  attr_accessor(
48
69
  :action,
@@ -55,8 +76,12 @@ module Atatus
55
76
  attr_reader(
56
77
  :context,
57
78
  :duration,
79
+ :parent,
80
+ :sample_rate,
81
+ :self_time,
58
82
  :stacktrace,
59
83
  :timestamp,
84
+ :transaction,
60
85
  :transaction_id
61
86
  )
62
87
 
@@ -65,21 +90,25 @@ module Atatus
65
90
  def start(clock_start = Util.monotonic_micros)
66
91
  @timestamp = Util.micros
67
92
  @clock_start = clock_start
93
+ @parent.child_started
68
94
  self
69
95
  end
70
96
 
71
97
  def stop(clock_end = Util.monotonic_micros)
72
98
  @duration ||= (clock_end - @clock_start)
99
+ @parent.child_stopped
100
+ @self_time = @duration - child_durations.duration
73
101
  self
74
102
  end
75
103
 
76
104
  def done(clock_end: Util.monotonic_micros)
77
105
  stop clock_end
106
+ self
107
+ end
78
108
 
109
+ def prepare_for_serialization!
79
110
  # build_stacktrace! if should_build_stacktrace?
80
111
  self.original_backtrace = nil # release original
81
-
82
- self
83
112
  end
84
113
 
85
114
  def stopped?
@@ -97,7 +126,7 @@ module Atatus
97
126
  # relations
98
127
 
99
128
  def inspect
100
- "<Atatus::Span id:#{id}" \
129
+ "<Atatus::Span id:#{trace_context&.id}" \
101
130
  " name:#{name.inspect}" \
102
131
  " type:#{type.inspect}" \
103
132
  '>'
@@ -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
  module Atatus
@@ -50,4 +67,4 @@ module Atatus
50
67
  end
51
68
  end
52
69
  end
53
- end
70
+ end
@@ -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
  module Atatus
@@ -6,15 +23,19 @@ module Atatus
6
23
  # @api private
7
24
  class ActionDispatchSpy
8
25
  def install
9
- ::ActionDispatch::ShowExceptions.class_eval do
10
- alias render_exception_without_apm render_exception
26
+ if defined?(::ActionDispatch) && defined?(::ActionDispatch::ShowExceptions)
27
+
28
+ ::ActionDispatch::ShowExceptions.class_eval do
29
+ alias render_exception_without_apm render_exception
11
30
 
12
- def render_exception(env, exception)
13
- context = Atatus.build_context(rack_env: env, for_type: :error)
14
- Atatus.report(exception, context: context, handled: false)
31
+ def render_exception(env, exception)
32
+ context = Atatus.build_context(rack_env: env, for_type: :error)
33
+ Atatus.report(exception, context: context, handled: false)
15
34
 
16
- render_exception_without_apm env, exception
35
+ render_exception_without_apm env, exception
36
+ end
17
37
  end
38
+
18
39
  end
19
40
  end
20
41
  end
@@ -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
  module Atatus
@@ -10,13 +27,17 @@ module Atatus
10
27
  TYPE = 'Delayed::Job'
11
28
 
12
29
  def install
13
- ::Delayed::Backend::Base.class_eval do
14
- alias invoke_job_without_apm invoke_job
30
+ if defined?(::Delayed) && defined?(::Delayed::Backend) && defined?(::Delayed::Backend::Base)
31
+
32
+ ::Delayed::Backend::Base.class_eval do
33
+ alias invoke_job_without_apm invoke_job
15
34
 
16
- def invoke_job(*args, &block)
17
- ::Atatus::Spies::DelayedJobSpy
18
- .invoke_job(self, *args, &block)
35
+ def invoke_job(*args, &block)
36
+ ::Atatus::Spies::DelayedJobSpy
37
+ .invoke_job(self, *args, &block)
38
+ end
19
39
  end
40
+
20
41
  end
21
42
  end
22
43
 
@@ -0,0 +1,62 @@
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
+ # @api private
22
+ module Spies
23
+ # @api private
24
+ class DynamoDBSpy
25
+ def self.without_net_http
26
+ return yield unless defined?(NetHTTPSpy)
27
+
28
+ Atatus::Spies::NetHTTPSpy.disable_in do
29
+ yield
30
+ end
31
+ end
32
+
33
+ def install
34
+ if defined?(::Aws) && defined?(::Aws::DynamoDB) && defined?(::Aws::DynamoDB::Client)
35
+
36
+ ::Aws::DynamoDB::Client.class_eval do
37
+ # Alias all available operations
38
+ api.operation_names.each do |operation_name|
39
+ alias :"#{operation_name}_without_apm" :"#{operation_name}"
40
+
41
+ define_method(operation_name) do |params = {}, options = {}|
42
+ Atatus.with_span(operation_name, 'db', subtype: 'dynamodb', action: operation_name) do
43
+ Atatus::Spies::DynamoDBSpy.without_net_http do
44
+ original_method = method("#{operation_name}_without_apm")
45
+ original_method.call(params, options)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ end
53
+ end
54
+ end
55
+
56
+ register(
57
+ 'Aws::DynamoDB::Client',
58
+ 'aws-sdk-dynamodb',
59
+ DynamoDBSpy.new
60
+ )
61
+ end
62
+ end
@@ -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
  module Atatus
@@ -6,9 +23,13 @@ module Atatus
6
23
  # @api private
7
24
  class ElasticsearchSpy
8
25
  NAME_FORMAT = '%s %s'
9
- TYPE = 'db.elasticsearch'
26
+ TYPE = 'db'
27
+ SUBTYPE = 'elasticsearch'
28
+
29
+ def self.sanitizer
30
+ @sanitizer ||= Atatus::Transport::Filters::HashSanitizer.new
31
+ end
10
32
 
11
- # rubocop:disable Metrics/MethodLength
12
33
  def install
13
34
  if defined?(::Elasticsearch) &&
14
35
  defined?(::Elasticsearch::Transport) &&
@@ -18,18 +39,43 @@ module Atatus
18
39
  alias perform_request_without_apm perform_request
19
40
 
20
41
  def perform_request(method, path, *args, &block)
42
+ unless Atatus.current_transaction
43
+ return perform_request_without_apm(method, path, *args, &block)
44
+ end
45
+
21
46
  name = format(NAME_FORMAT, method, path)
22
- statement = args[0].is_a?(String) ? args[0] : args[0].to_json
23
- context = Span::Context.new(db: { statement: statement })
47
+ statement = []
24
48
 
25
- Atatus.with_span name, TYPE, context: context do
26
- perform_request_without_apm(method, path, *args, &block)
49
+ statement << { params: args&.[](0) }
50
+
51
+ if Atatus.agent.config.capture_elasticsearch_queries
52
+ unless args[1].nil? || args[1].empty?
53
+ statement << {
54
+ body: Atatus::Spies::ElasticsearchSpy.sanitizer.strip_from!(args[1])
55
+ }
56
+ end
27
57
  end
58
+
59
+ context = Span::Context.new(
60
+ db: { statement: statement.reduce({}, :merge).to_json },
61
+ destination: {
62
+ name: SUBTYPE,
63
+ resource: SUBTYPE,
64
+ type: TYPE
65
+ }
66
+ )
67
+
68
+ Atatus.with_span(
69
+ name,
70
+ TYPE,
71
+ subtype: SUBTYPE,
72
+ context: context
73
+ ) { perform_request_without_apm(method, path, *args, &block) }
28
74
  end
29
75
  end
76
+
30
77
  end
31
78
  end
32
- # rubocop:enable Metrics/MethodLength
33
79
  end
34
80
 
35
81
  register(
@@ -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
  module Atatus
@@ -16,8 +33,6 @@ module Atatus
16
33
  end
17
34
  end
18
35
 
19
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
20
- # rubocop:disable Metrics/BlockLength, Metrics/PerceivedComplexity
21
36
  # rubocop:disable Metrics/CyclomaticComplexity
22
37
  def install
23
38
  if defined?(::Faraday) && defined?(::Faraday::Connection)
@@ -30,42 +45,61 @@ module Atatus
30
45
  return run_request_without_apm(method, url, body, headers, &block)
31
46
  end
32
47
 
33
- host = if url_prefix.is_a?(URI) && url_prefix.host
34
- url_prefix.host
35
- elsif url.nil?
36
- tmp_request = build_request(method) do |req|
37
- yield(req) if block_given?
38
- end
39
- URI(tmp_request.path).host
40
- else
41
- URI(url).host
42
- end
48
+ uri = URI(build_url(url))
49
+
50
+ # If url is set inside block it isn't available until yield,
51
+ # so we temporarily build the request to yield. This could be a
52
+ # problem if the block has side effects as it will be yielded twice
53
+ # ~mikker
54
+ unless uri.host
55
+ tmp_request = build_request(method) do |req|
56
+ yield(req) if block_given?
57
+ end
58
+ uri = URI(tmp_request.path)
59
+ end
60
+
61
+ host = uri.host
43
62
 
44
- name = "#{method.upcase} #{host}"
63
+ upcased_method = method.to_s.upcase
64
+
65
+ destination = Atatus::Span::Context::Destination.from_uri(uri)
66
+
67
+ context =
68
+ Atatus::Span::Context.new(
69
+ http: { url: uri, method: upcased_method },
70
+ destination: destination
71
+ )
45
72
 
46
73
  Atatus.with_span(
47
- name,
74
+ "#{upcased_method} #{host}",
48
75
  TYPE,
49
76
  subtype: SUBTYPE,
50
- action: method.to_s
77
+ action: upcased_method,
78
+ context: context
51
79
  ) do |span|
52
80
  Atatus::Spies::FaradaySpy.without_net_http do
53
81
  trace_context = span&.trace_context || transaction.trace_context
54
82
 
55
- run_request_without_apm(method, url, body, headers) do |req|
56
- req['Atatus-Apm-Traceparent'] = trace_context.to_header
83
+ result =
84
+ run_request_without_apm(method, url, body, headers) do |req|
85
+ trace_context.apply_headers { |k, v| req[k] = v }
86
+
87
+ yield req if block_given?
88
+ end
57
89
 
58
- yield req if block_given?
90
+ if (http = span&.context&.http)
91
+ http.status_code = result.status.to_s
59
92
  end
93
+
94
+ result
60
95
  end
61
96
  end
62
97
  end
63
98
  end
99
+
64
100
  end
65
101
  end
66
102
  # rubocop:enable Metrics/CyclomaticComplexity
67
- # rubocop:enable Metrics/BlockLength, Metrics/PerceivedComplexity
68
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
69
103
  end
70
104
 
71
105
  register 'Faraday', 'faraday', FaradaySpy.new
@@ -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
  module Atatus
@@ -7,8 +24,6 @@ module Atatus
7
24
  class HTTPSpy
8
25
  TYPE = 'ext'
9
26
  SUBTYPE = 'http_rb'
10
-
11
- # rubocop:disable Metrics/MethodLength
12
27
  def install
13
28
  if defined?(::HTTP) && defined?(::HTTP::Client)
14
29
 
@@ -23,23 +38,38 @@ module Atatus
23
38
  method = req.verb.to_s.upcase
24
39
  host = req.uri.host
25
40
 
41
+ destination =
42
+ Atatus::Span::Context::Destination.from_uri(req.uri)
43
+ context = Atatus::Span::Context.new(
44
+ http: { url: req.uri, method: method },
45
+ destination: destination
46
+ )
47
+
26
48
  name = "#{method} #{host}"
27
49
 
28
50
  Atatus.with_span(
29
51
  name,
30
52
  TYPE,
31
53
  subtype: SUBTYPE,
32
- action: method
54
+ action: method,
55
+ context: context
33
56
  ) do |span|
34
57
  trace_context = span&.trace_context || transaction.trace_context
35
- req['Atatus-Apm-Traceparent'] = trace_context.to_header
36
- perform_without_apm(req, options)
58
+ trace_context.apply_headers { |key, value| req[key] = value }
59
+
60
+ result = perform_without_apm(req, options)
61
+
62
+ if (http = span&.context&.http)
63
+ http.status_code = result.status.to_s
64
+ end
65
+
66
+ result
37
67
  end
38
68
  end
39
69
  end
70
+
40
71
  end
41
72
  end
42
- # rubocop:enable Metrics/MethodLength
43
73
  end
44
74
 
45
75
  register 'HTTP', 'http', HTTPSpy.new
@@ -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/span_helpers'
@@ -16,6 +33,7 @@ module Atatus
16
33
  span_class_method :parse!, 'JSON#parse!', 'json.parse'
17
34
  span_class_method :generate, 'JSON#generate', 'json.generate'
18
35
  end
36
+
19
37
  end
20
38
  end
21
39
  end