instana 1.195.2 → 1.197.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -2
  3. data/Rakefile +1 -1
  4. data/instana.gemspec +3 -7
  5. data/lib/instana.rb +3 -0
  6. data/lib/instana/activator.rb +2 -0
  7. data/lib/instana/backend/agent.rb +60 -0
  8. data/lib/instana/backend/gc_snapshot.rb +41 -0
  9. data/lib/instana/backend/host_agent.rb +74 -0
  10. data/lib/instana/backend/host_agent_activation_observer.rb +97 -0
  11. data/lib/instana/backend/host_agent_lookup.rb +57 -0
  12. data/lib/instana/backend/host_agent_reporting_observer.rb +106 -0
  13. data/lib/instana/backend/process_info.rb +64 -0
  14. data/lib/instana/backend/request_client.rb +73 -0
  15. data/lib/instana/backend/serverless_agent.rb +118 -0
  16. data/lib/instana/base.rb +8 -27
  17. data/lib/instana/config.rb +8 -22
  18. data/lib/instana/instrumentation/excon.rb +17 -8
  19. data/lib/instana/instrumentation/instrumented_request.rb +62 -7
  20. data/lib/instana/instrumentation/net-http.rb +7 -5
  21. data/lib/instana/instrumentation/rack.rb +12 -7
  22. data/lib/instana/logger_delegator.rb +31 -0
  23. data/lib/instana/{opentracing → open_tracing}/carrier.rb +0 -0
  24. data/lib/instana/open_tracing/instana_tracer.rb +99 -0
  25. data/lib/instana/secrets.rb +6 -2
  26. data/lib/instana/setup.rb +20 -11
  27. data/lib/instana/snapshot/deltable.rb +25 -0
  28. data/lib/instana/snapshot/docker_container.rb +151 -0
  29. data/lib/instana/snapshot/fargate_container.rb +88 -0
  30. data/lib/instana/snapshot/fargate_process.rb +67 -0
  31. data/lib/instana/snapshot/fargate_task.rb +72 -0
  32. data/lib/instana/snapshot/lambda_function.rb +36 -0
  33. data/lib/instana/snapshot/ruby_process.rb +48 -0
  34. data/lib/instana/tracer.rb +25 -143
  35. data/lib/instana/tracing/processor.rb +14 -22
  36. data/lib/instana/tracing/span.rb +31 -34
  37. data/lib/instana/tracing/span_context.rb +15 -10
  38. data/lib/instana/util.rb +8 -69
  39. data/lib/instana/version.rb +1 -1
  40. data/lib/opentracing.rb +26 -3
  41. data/test/backend/agent_test.rb +54 -0
  42. data/test/backend/gc_snapshot_test.rb +11 -0
  43. data/test/backend/host_agent_activation_observer_test.rb +72 -0
  44. data/test/backend/host_agent_lookup_test.rb +78 -0
  45. data/test/backend/host_agent_reporting_observer_test.rb +192 -0
  46. data/test/backend/host_agent_test.rb +47 -0
  47. data/test/backend/process_info_test.rb +63 -0
  48. data/test/backend/request_client_test.rb +39 -0
  49. data/test/backend/serverless_agent_test.rb +73 -0
  50. data/test/config_test.rb +10 -0
  51. data/test/instana_test.rb +11 -4
  52. data/test/instrumentation/excon_test.rb +15 -1
  53. data/test/instrumentation/rack_instrumented_request_test.rb +5 -2
  54. data/test/instrumentation/rack_test.rb +2 -14
  55. data/test/secrets_test.rb +41 -22
  56. data/test/snapshot/deltable_test.rb +17 -0
  57. data/test/snapshot/docker_container_test.rb +82 -0
  58. data/test/snapshot/fargate_container_test.rb +82 -0
  59. data/test/snapshot/fargate_process_test.rb +35 -0
  60. data/test/snapshot/fargate_task_test.rb +49 -0
  61. data/test/snapshot/ruby_process_test.rb +14 -0
  62. data/test/support/mock_timer.rb +20 -0
  63. data/test/test_helper.rb +16 -4
  64. data/test/tracing/custom_test.rb +1 -3
  65. data/test/tracing/id_management_test.rb +4 -0
  66. data/test/tracing/opentracing_test.rb +15 -2
  67. data/test/tracing/processor_test.rb +58 -0
  68. data/test/tracing/span_context_test.rb +21 -0
  69. data/test/tracing/span_test.rb +136 -0
  70. data/test/tracing/tracer_async_test.rb +29 -0
  71. data/test/tracing/tracer_test.rb +82 -16
  72. data/test/util_test.rb +10 -0
  73. metadata +71 -43
  74. data/lib/instana/agent.rb +0 -508
  75. data/lib/instana/agent/helpers.rb +0 -87
  76. data/lib/instana/agent/hooks.rb +0 -44
  77. data/lib/instana/agent/tasks.rb +0 -51
  78. data/lib/instana/collector.rb +0 -119
  79. data/lib/instana/collectors/gc.rb +0 -60
  80. data/lib/instana/collectors/memory.rb +0 -37
  81. data/lib/instana/collectors/thread.rb +0 -33
  82. data/lib/instana/eum/eum-test.js.erb +0 -17
  83. data/lib/instana/eum/eum.js.erb +0 -17
  84. data/lib/instana/helpers.rb +0 -47
  85. data/lib/instana/opentracing/tracer.rb +0 -21
  86. data/lib/instana/thread_local.rb +0 -18
  87. data/lib/oj_check.rb +0 -19
  88. data/test/agent/agent_test.rb +0 -151
@@ -5,7 +5,7 @@ require 'thread'
5
5
 
6
6
  module Instana
7
7
  class Processor
8
- def initialize
8
+ def initialize(logger: ::Instana.logger)
9
9
  # The main queue before being reported to the
10
10
  # host agent. Spans in this queue are complete
11
11
  # and ready to be sent.
@@ -14,25 +14,22 @@ module Instana
14
14
  # This is the maximum number of spans we send to the host
15
15
  # agent at once.
16
16
  @batch_size = 3000
17
- end
18
-
19
- # Adds a Set of spans to the queue
20
- #
21
- # @param [spans] - the trace to be added to the queue
22
- def add_spans(spans)
23
- spans.each { |span| @queue.push(span)}
17
+ @logger = logger
18
+ @pid = $PROCESS_ID
24
19
  end
25
20
 
26
21
  # Adds a span to the span queue
27
22
  #
28
23
  # @param [Trace] - the trace to be added to the queue
29
24
  def add_span(span)
30
- # Occasionally, do a checkup on our background thread.
31
- if rand(10) > 8
32
- if ::Instana.agent.collect_thread.nil? || !::Instana.agent.collect_thread.alive?
33
- ::Instana.agent.spawn_background_thread
34
- end
25
+ # :nocov:
26
+ if @pid != $PROCESS_ID
27
+ @logger.info("Proces `#{@pid}` has forked into #{$PROCESS_ID}. Resetting discovery.")
28
+ ::Instana.agent.spawn_background_thread
29
+ @pid = $PROCESS_ID
35
30
  end
31
+ # :nocov:
32
+
36
33
  @queue.push(span)
37
34
  end
38
35
 
@@ -46,18 +43,14 @@ module Instana
46
43
  # - Out of control/growing queue
47
44
  # - Prevent another run of the timer while this is running
48
45
  #
49
- def send
46
+ def send(&block)
50
47
  return if @queue.empty? || ENV.key?('INSTANA_TEST')
51
48
 
52
49
  # Retrieve all spans for queued traces
53
50
  spans = queued_spans
54
51
 
55
52
  # Report spans in batches
56
- batch = spans.shift(@batch_size)
57
- while !batch.empty? do
58
- ::Instana.agent.report_spans(batch)
59
- batch = spans.shift(@batch_size)
60
- end
53
+ spans.each_slice(@batch_size, &block)
61
54
  end
62
55
 
63
56
  # Retrieves all of the traces in @queue and returns
@@ -75,10 +68,9 @@ module Instana
75
68
  until @queue.empty? do
76
69
  # Non-blocking pop; ignore exception
77
70
  span = @queue.pop(true) rescue nil
78
- if span
79
- spans << span.raw
80
- end
71
+ spans << span.raw if span.is_a?(Span) && span.context.level == 1
81
72
  end
73
+
82
74
  spans
83
75
  end
84
76
 
@@ -20,38 +20,40 @@ module Instana
20
20
  def initialize(name, parent_ctx: nil, start_time: ::Instana::Util.now_in_ms)
21
21
  @data = {}
22
22
 
23
- if parent_ctx == nil
24
- # No parent specified so we're starting a new Trace - this will be the root span
25
- id = ::Instana::Util.generate_id
26
- @data[:t] = id # Trace ID
27
- @data[:s] = id # Span ID
28
- @is_root = true
29
- else
30
- if parent_ctx.is_a?(::Instana::Span)
31
- @parent = parent_ctx
32
- parent_context = parent_ctx.context
33
- elsif parent_ctx.is_a?(::Instana::SpanContext)
34
- parent_context = parent_ctx
35
- end
23
+ if parent_ctx.is_a?(::Instana::Span)
24
+ @parent = parent_ctx
25
+ parent_ctx = parent_ctx.context
26
+ end
36
27
 
37
- @data[:t] = parent_context.trace_id # Trace ID
38
- @data[:s] = ::Instana::Util.generate_id # Span ID
39
- @data[:p] = parent_context.span_id # Parent ID
28
+ if parent_ctx.is_a?(::Instana::SpanContext)
29
+ @is_root = false
40
30
 
41
- if parent_context.baggage
42
- @baggage = parent_ctx.baggage.dup
31
+ # If we have a parent trace, link to it
32
+ if parent_ctx.trace_id
33
+ @data[:t] = parent_ctx.trace_id # Trace ID
34
+ @data[:p] = parent_ctx.span_id # Parent ID
43
35
  else
44
- @baggage = nil
36
+ @data[:t] = ::Instana::Util.generate_id
45
37
  end
46
38
 
47
- @is_root = false
39
+ @data[:s] = ::Instana::Util.generate_id # Span ID
40
+
41
+ @baggage = parent_ctx.baggage.dup
42
+ @level = parent_ctx.level
43
+ else
44
+ # No parent specified so we're starting a new Trace - this will be the root span
45
+ @is_root = true
46
+ @level = 1
47
+
48
+ id = ::Instana::Util.generate_id
49
+ @data[:t] = id # Trace ID
50
+ @data[:s] = id # Span ID
48
51
  end
49
52
 
50
53
  @data[:data] = {}
51
54
 
52
55
  # Entity Source
53
- @data[:f] = { :e => ::Instana.agent.report_pid,
54
- :h => ::Instana.agent.agent_uuid }
56
+ @data[:f] = ::Instana.agent.source
55
57
  # Start time
56
58
  if start_time.is_a?(Time)
57
59
  @data[:ts] = ::Instana::Util.time_to_ms(start_time)
@@ -59,17 +61,15 @@ module Instana
59
61
  @data[:ts] = start_time
60
62
  end
61
63
 
62
- if ::Instana.config[:collect_backtraces]
63
- # Attach a backtrace to all exit spans
64
- add_stack if EXIT_SPANS.include?(name)
65
- end
66
-
67
64
  # Check for custom tracing
68
65
  if REGISTERED_SPANS.include?(name.to_sym)
69
66
  @data[:n] = name.to_sym
70
67
  else
71
68
  configure_custom(name)
72
69
  end
70
+
71
+ # Attach a backtrace to all exit spans
72
+ add_stack if ::Instana.config[:collect_backtraces] && exit_span?
73
73
  end
74
74
 
75
75
  # Adds a backtrace to this span
@@ -195,7 +195,7 @@ module Instana
195
195
  # @return [Instana::SpanContext]
196
196
  #
197
197
  def context
198
- @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], 1, @baggage)
198
+ @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], @level, @baggage)
199
199
  end
200
200
 
201
201
  # Retrieve the ID for this span
@@ -344,9 +344,7 @@ module Instana
344
344
  end
345
345
  end
346
346
  else
347
- if !@data[:data].key?(key)
348
- @data[:data][key] = value
349
- elsif value.is_a?(Hash) && self[:data][key].is_a?(Hash)
347
+ if value.is_a?(Hash) && @data[:data][key].is_a?(Hash)
350
348
  @data[:data][key].merge!(value)
351
349
  else
352
350
  @data[:data][key] = value
@@ -381,7 +379,7 @@ module Instana
381
379
  if @context
382
380
  @context.baggage = @baggage
383
381
  else
384
- @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], 1, @baggage)
382
+ @context ||= ::Instana::SpanContext.new(@data[:t], @data[:s], @level, @baggage)
385
383
  end
386
384
  self
387
385
  end
@@ -393,7 +391,6 @@ module Instana
393
391
  # @return Value of the baggage item
394
392
  #
395
393
  def get_baggage_item(key)
396
- return nil if @baggage.nil?
397
394
  @baggage[key]
398
395
  end
399
396
 
@@ -403,7 +400,7 @@ module Instana
403
400
  if custom?
404
401
  tags = @data[:data][:sdk][:custom][:tags]
405
402
  else
406
- tags = @data[:data][key]
403
+ tags = @data[:data]
407
404
  end
408
405
  key ? tags[key] : tags
409
406
  end
@@ -18,7 +18,7 @@ module Instana
18
18
  def initialize(tid, sid, level = 1, baggage = {})
19
19
  @trace_id = tid
20
20
  @span_id = sid
21
- @level = level
21
+ @level = Integer(level || 1)
22
22
  @baggage = baggage || {}
23
23
  end
24
24
 
@@ -31,19 +31,22 @@ module Instana
31
31
  end
32
32
 
33
33
  def trace_parent_header
34
- return '' unless valid?
35
-
36
- trace = (@baggage[:external_trace_id] || @trace_id).rjust(32, '0')
37
- parent = @span_id.rjust(16, '0')
34
+ trace = (@baggage[:external_trace_id] || trace_id_header).rjust(32, '0')
35
+ parent = span_id_header.rjust(16, '0')
38
36
  flags = @level == 1 ? "01" : "00"
39
37
 
40
38
  "00-#{trace}-#{parent}-#{flags}"
41
39
  end
42
40
 
43
41
  def trace_state_header
44
- return '' unless valid?
42
+ external_state = @baggage[:external_state] || ''
43
+ state = external_state.split(/,/)
44
+
45
+ if @level == 1
46
+ state = state.reject { |s| s.start_with?('in=') }
47
+ state.unshift("in=#{trace_id_header};#{span_id_header}")
48
+ end
45
49
 
46
- state = ["in=#{trace_id_header};#{span_id_header}", @baggage[:external_state]]
47
50
  state.compact.join(',')
48
51
  end
49
52
 
@@ -51,10 +54,12 @@ module Instana
51
54
  { :trace_id => @trace_id, :span_id => @span_id }
52
55
  end
53
56
 
54
- private
55
-
56
57
  def valid?
57
- @baggage && @trace_id && @span_id
58
+ @baggage && @trace_id && !@trace_id.emtpy?
59
+ end
60
+
61
+ def active?
62
+ @level == 1
58
63
  end
59
64
  end
60
65
  end
data/lib/instana/util.rb CHANGED
@@ -5,30 +5,6 @@ module Instana
5
5
  module Util
6
6
  class << self
7
7
  ID_RANGE = -2**63..2**63-1
8
- # Debugging helper method
9
- #
10
- def pry!
11
- # Only valid for development or test environments
12
- #env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
13
- #return unless %w(development, test).include? env
14
- require 'pry-byebug'
15
-
16
- if defined?(PryByebug)
17
- Pry.commands.alias_command 'c', 'continue'
18
- Pry.commands.alias_command 's', 'step'
19
- Pry.commands.alias_command 'n', 'next'
20
- Pry.commands.alias_command 'f', 'finish'
21
-
22
- Pry::Commands.command(/^$/, 'repeat last command') do
23
- _pry_.run_command Pry.history.to_a.last
24
- end
25
- end
26
-
27
- binding.pry
28
- rescue LoadError
29
- ::Instana.logger.warn("No debugger in bundle. Couldn't load pry-byebug.")
30
- end
31
-
32
8
  # Retrieves and returns the source code for any ruby
33
9
  # files requested by the UI via the host agent
34
10
  #
@@ -47,6 +23,7 @@ module Instana
47
23
  # Method to collect up process info for snapshots. This
48
24
  # is generally used once per process.
49
25
  #
26
+ # :nocov:
50
27
  def take_snapshot
51
28
  data = {}
52
29
 
@@ -86,53 +63,12 @@ module Instana
86
63
  ::Instana.logger.debug { e.backtrace.join("\r\n") }
87
64
  return data
88
65
  end
89
-
90
- # Used in class initialization and after a fork, this method
91
- # collects up process information
92
- #
93
- def collect_process_info
94
- process = {}
95
- cmdline_file = "/proc/#{Process.pid}/cmdline"
96
-
97
- # If there is a /proc filesystem, we read this manually so
98
- # we can split on embedded null bytes. Otherwise (e.g. OSX, Windows)
99
- # use ProcTable.
100
- if File.exist?(cmdline_file)
101
- cmdline = IO.read(cmdline_file).split(?\x00)
102
- else
103
- # Attempt to support older versions of sys-proctable and ffi.
104
- #
105
- # Alternatively we could use Sys::ProcTable::VERSION here but the
106
- # consistency across historical versions is unknown. Alternative
107
- # to the alternative, would be Ruby metaprogramming using the `arity`
108
- # and `parameters` methods.
109
- # e.g ProcTable.method(:ps).arity/parameters
110
- if Gem.loaded_specs.key?("sys-proctable") &&
111
- (Gem.loaded_specs["sys-proctable"].version >= Gem::Version.new("1.2.0"))
112
- cmdline = ProcTable.ps(:pid => Process.pid).cmdline.split(' ')
113
- else
114
- cmdline = ProcTable.ps(Process.pid).cmdline.split(' ')
115
- end
116
- end
117
-
118
- if RUBY_PLATFORM =~ /darwin/i
119
- cmdline.delete_if{ |e| e.include?('=') }
120
- process[:name] = cmdline.join(' ')
121
- else
122
- process[:name] = cmdline.shift
123
- process[:arguments] = cmdline
124
- end
125
-
126
- process[:pid] = Process.pid
127
- # This is usually Process.pid but in the case of containers, the host agent
128
- # will return to us the true host pid in which we use to report data.
129
- process[:report_pid] = nil
130
- process
131
- end
66
+ # :nocov:
132
67
 
133
68
  # Best effort to determine a name for the instrumented application
134
69
  # on the dashboard.
135
70
  #
71
+ # :nocov:
136
72
  def get_app_name
137
73
  if ENV.key?('INSTANA_SERVICE_NAME')
138
74
  return ENV['INSTANA_SERVICE_NAME']
@@ -169,6 +105,7 @@ module Instana
169
105
  Instana.logger.debug { e.backtrace.join("\r\n") }
170
106
  return "Ruby"
171
107
  end
108
+ # :nocov:
172
109
 
173
110
  # Get the current time in milliseconds from the epoch
174
111
  #
@@ -219,8 +156,10 @@ module Instana
219
156
  #
220
157
  # @return [String]
221
158
  #
222
- def header_to_id(header_id)
223
- header_id.is_a?(String) && header_id.match(/\A[a-z\d]{16,32}\z/i) ? header_id : ''
159
+ def header_to_id(given)
160
+ return '' unless given.is_a?(String)
161
+ return '' unless given.match(/\A[a-z\d]{16,32}\z/i)
162
+ given
224
163
  end
225
164
  end
226
165
  end
@@ -2,6 +2,6 @@
2
2
  # (c) Copyright Instana Inc. 2016
3
3
 
4
4
  module Instana
5
- VERSION = "1.195.2"
5
+ VERSION = "1.197.0"
6
6
  VERSION_FULL = "instana-#{VERSION}"
7
7
  end
data/lib/opentracing.rb CHANGED
@@ -1,9 +1,32 @@
1
1
  # (c) Copyright IBM Corp. 2021
2
2
  # (c) Copyright Instana Inc. 2017
3
3
 
4
- require "instana/opentracing/tracer"
5
- require "instana/opentracing/carrier"
4
+ require "instana/open_tracing/carrier"
5
+ require "instana/open_tracing/instana_tracer"
6
+
7
+ module OpenTracing
8
+ class << self
9
+ # Text format for #inject and #extract
10
+ FORMAT_TEXT_MAP = 1
11
+
12
+ # Binary format for #inject and #extract
13
+ FORMAT_BINARY = 2
14
+
15
+ # Ruby Specific format to handle how Rack changes environment variables.
16
+ FORMAT_RACK = 3
17
+
18
+ attr_accessor :global_tracer
19
+
20
+ def method_missing(method_name, *args, &block)
21
+ @global_tracer.send(method_name, *args, &block)
22
+ end
23
+
24
+ def respond_to_missing?(name, all)
25
+ @global_tracer.respond_to?(name, all)
26
+ end
27
+ end
28
+ end
6
29
 
7
30
  # Set the global tracer to our OT tracer
8
31
  # which supports the OT specification
9
- OpenTracing.global_tracer = ::Instana.tracer
32
+ OpenTracing.global_tracer = OpenTracing::InstanaTracer.new(::Instana.tracer)
@@ -0,0 +1,54 @@
1
+ # (c) Copyright IBM Corp. 2021
2
+ # (c) Copyright Instana Inc. 2021
3
+
4
+ require 'test_helper'
5
+
6
+ class AgentTest < Minitest::Test
7
+ def test_host
8
+ subject = Instana::Backend::Agent.new
9
+ assert_nil subject.delegate
10
+ subject.setup
11
+ assert subject.delegate.is_a?(Instana::Backend::HostAgent)
12
+ end
13
+
14
+ def test_fargate
15
+ ENV['ECS_CONTAINER_METADATA_URI'] = 'https://10.10.10.10:9292/v3'
16
+ ENV['INSTANA_ENDPOINT_URL'] = 'http://example.com'
17
+
18
+ stub_request(:get, 'https://10.10.10.10:9292/v3/task')
19
+ .to_return(status: 200, body: File.read('test/support/ecs/task.json'))
20
+
21
+ subject = Instana::Backend::Agent.new(fargate_metadata_uri: 'https://10.10.10.10:9292/v3')
22
+ assert_nil subject.delegate
23
+ subject.setup
24
+ assert subject.delegate.is_a?(Instana::Backend::ServerlessAgent)
25
+ ensure
26
+ ENV['INSTANA_ENDPOINT_URL'] = nil
27
+ ENV['ECS_CONTAINER_METADATA_URI'] = nil
28
+ end
29
+
30
+ def test_fargate_error
31
+ ENV['ECS_CONTAINER_METADATA_URI'] = 'https://10.10.10.10:9292/v3'
32
+ ENV['INSTANA_ENDPOINT_URL'] = 'http://example.com'
33
+
34
+ stub_request(:get, 'https://10.10.10.10:9292/v3/task')
35
+ .to_return(status: 500)
36
+
37
+ subject = Instana::Backend::Agent.new(logger: Logger.new('/dev/null'))
38
+ assert_nil subject.delegate
39
+ subject.setup
40
+ assert subject.delegate.is_a?(Instana::Backend::ServerlessAgent)
41
+ ensure
42
+ ENV['INSTANA_ENDPOINT_URL'] = nil
43
+ ENV['ECS_CONTAINER_METADATA_URI'] = nil
44
+ end
45
+
46
+ def test_delegate_super
47
+ subject = Instana::Backend::Agent.new
48
+ assert_raises NoMethodError do
49
+ subject.invalid
50
+ end
51
+
52
+ refute subject.respond_to?(:invalid)
53
+ end
54
+ end