instana 1.10.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +23 -0
  3. data/.gitignore +16 -0
  4. data/.rubocop.yml +1156 -0
  5. data/.travis.yml +43 -0
  6. data/Configuration.md +149 -0
  7. data/Dockerfile +13 -0
  8. data/Gemfile +41 -0
  9. data/LICENSE +21 -0
  10. data/README.md +102 -0
  11. data/Rakefile +56 -0
  12. data/Tracing.md +145 -0
  13. data/Troubleshooting.md +32 -0
  14. data/benchmarks/Gemfile +7 -0
  15. data/benchmarks/id_generation.rb +12 -0
  16. data/benchmarks/opentracing.rb +26 -0
  17. data/benchmarks/rack_vanilla_vs_traced.rb +80 -0
  18. data/benchmarks/stackprof_rack_tracing.rb +77 -0
  19. data/benchmarks/time_processing.rb +12 -0
  20. data/bin/console +7 -0
  21. data/bin/setup +8 -0
  22. data/examples/opentracing.rb +31 -0
  23. data/examples/tracing.rb +80 -0
  24. data/gemfiles/libraries.gemfile +71 -0
  25. data/gemfiles/rails32.gemfile +51 -0
  26. data/gemfiles/rails42.gemfile +50 -0
  27. data/gemfiles/rails50.gemfile +52 -0
  28. data/instana.gemspec +46 -0
  29. data/lib/instana.rb +12 -0
  30. data/lib/instana/agent.rb +441 -0
  31. data/lib/instana/agent/helpers.rb +61 -0
  32. data/lib/instana/agent/hooks.rb +37 -0
  33. data/lib/instana/agent/tasks.rb +48 -0
  34. data/lib/instana/base.rb +54 -0
  35. data/lib/instana/collector.rb +116 -0
  36. data/lib/instana/collectors/gc.rb +57 -0
  37. data/lib/instana/collectors/memory.rb +34 -0
  38. data/lib/instana/collectors/thread.rb +30 -0
  39. data/lib/instana/config.rb +79 -0
  40. data/lib/instana/eum/eum-test.js.erb +16 -0
  41. data/lib/instana/eum/eum.js.erb +14 -0
  42. data/lib/instana/frameworks/cuba.rb +6 -0
  43. data/lib/instana/frameworks/instrumentation/abstract_mysql_adapter.rb +58 -0
  44. data/lib/instana/frameworks/instrumentation/action_controller.rb +183 -0
  45. data/lib/instana/frameworks/instrumentation/action_view.rb +43 -0
  46. data/lib/instana/frameworks/instrumentation/active_record.rb +27 -0
  47. data/lib/instana/frameworks/instrumentation/mysql2_adapter.rb +81 -0
  48. data/lib/instana/frameworks/instrumentation/mysql_adapter.rb +56 -0
  49. data/lib/instana/frameworks/instrumentation/postgresql_adapter.rb +71 -0
  50. data/lib/instana/frameworks/rails.rb +42 -0
  51. data/lib/instana/frameworks/roda.rb +6 -0
  52. data/lib/instana/frameworks/sinatra.rb +9 -0
  53. data/lib/instana/helpers.rb +40 -0
  54. data/lib/instana/instrumentation.rb +21 -0
  55. data/lib/instana/instrumentation/dalli.rb +78 -0
  56. data/lib/instana/instrumentation/excon.rb +74 -0
  57. data/lib/instana/instrumentation/grpc.rb +84 -0
  58. data/lib/instana/instrumentation/net-http.rb +66 -0
  59. data/lib/instana/instrumentation/rack.rb +77 -0
  60. data/lib/instana/instrumentation/redis.rb +82 -0
  61. data/lib/instana/instrumentation/resque.rb +131 -0
  62. data/lib/instana/instrumentation/rest-client.rb +34 -0
  63. data/lib/instana/instrumentation/sidekiq-client.rb +45 -0
  64. data/lib/instana/instrumentation/sidekiq-worker.rb +54 -0
  65. data/lib/instana/opentracing/carrier.rb +4 -0
  66. data/lib/instana/opentracing/tracer.rb +18 -0
  67. data/lib/instana/rack.rb +10 -0
  68. data/lib/instana/setup.rb +36 -0
  69. data/lib/instana/test.rb +40 -0
  70. data/lib/instana/thread_local.rb +15 -0
  71. data/lib/instana/tracer.rb +392 -0
  72. data/lib/instana/tracing/processor.rb +92 -0
  73. data/lib/instana/tracing/span.rb +401 -0
  74. data/lib/instana/tracing/span_context.rb +33 -0
  75. data/lib/instana/util.rb +261 -0
  76. data/lib/instana/version.rb +4 -0
  77. data/lib/oj_check.rb +16 -0
  78. data/lib/opentracing.rb +6 -0
  79. data/test/agent/agent_test.rb +143 -0
  80. data/test/apps/cuba.rb +15 -0
  81. data/test/apps/grpc_server.rb +81 -0
  82. data/test/apps/roda.rb +10 -0
  83. data/test/apps/sinatra.rb +5 -0
  84. data/test/benchmarks/bench_id_generation.rb +12 -0
  85. data/test/benchmarks/bench_opentracing.rb +13 -0
  86. data/test/config_test.rb +37 -0
  87. data/test/frameworks/cuba_test.rb +44 -0
  88. data/test/frameworks/rack_test.rb +167 -0
  89. data/test/frameworks/rails/actioncontroller_test.rb +93 -0
  90. data/test/frameworks/rails/actionview3_test.rb +255 -0
  91. data/test/frameworks/rails/actionview4_test.rb +254 -0
  92. data/test/frameworks/rails/actionview5_test.rb +221 -0
  93. data/test/frameworks/rails/activerecord3_test.rb +134 -0
  94. data/test/frameworks/rails/activerecord4_test.rb +134 -0
  95. data/test/frameworks/rails/activerecord5_test.rb +87 -0
  96. data/test/frameworks/roda_test.rb +44 -0
  97. data/test/frameworks/sinatra_test.rb +44 -0
  98. data/test/instana_test.rb +27 -0
  99. data/test/instrumentation/dalli_test.rb +253 -0
  100. data/test/instrumentation/excon_test.rb +147 -0
  101. data/test/instrumentation/grpc_test.rb +377 -0
  102. data/test/instrumentation/net-http_test.rb +160 -0
  103. data/test/instrumentation/redis_test.rb +119 -0
  104. data/test/instrumentation/resque_test.rb +128 -0
  105. data/test/instrumentation/rest-client_test.rb +55 -0
  106. data/test/instrumentation/sidekiq-client_test.rb +125 -0
  107. data/test/instrumentation/sidekiq-worker_test.rb +173 -0
  108. data/test/jobs/resque_error_job.rb +22 -0
  109. data/test/jobs/resque_fast_job.rb +20 -0
  110. data/test/jobs/sidekiq_job_1.rb +6 -0
  111. data/test/jobs/sidekiq_job_2.rb +7 -0
  112. data/test/models/block.rb +18 -0
  113. data/test/servers/grpc_50051.rb +20 -0
  114. data/test/servers/helpers/sidekiq_worker_initializer.rb +27 -0
  115. data/test/servers/rackapp_6511.rb +25 -0
  116. data/test/servers/rails_3205.rb +167 -0
  117. data/test/servers/sidekiq/worker.rb +27 -0
  118. data/test/test_helper.rb +145 -0
  119. data/test/tracing/custom_test.rb +158 -0
  120. data/test/tracing/id_management_test.rb +130 -0
  121. data/test/tracing/opentracing_test.rb +335 -0
  122. data/test/tracing/trace_test.rb +67 -0
  123. data/test/tracing/tracer_async_test.rb +198 -0
  124. data/test/tracing/tracer_test.rb +223 -0
  125. metadata +327 -0
@@ -0,0 +1,33 @@
1
+ module Instana
2
+ class SpanContext
3
+ attr_accessor :trace_id
4
+ attr_accessor :span_id
5
+ attr_accessor :baggage
6
+
7
+ # Create a new SpanContext
8
+ #
9
+ # @param tid [Integer] the trace ID
10
+ # @param sid [Integer] the span ID
11
+ # @param level [Integer] default 1
12
+ # @param baggage [Hash] baggage applied to this trace
13
+ #
14
+ def initialize(tid, sid, level = 1, baggage = nil)
15
+ @trace_id = tid
16
+ @span_id = sid
17
+ @level = level
18
+ @baggage = baggage
19
+ end
20
+
21
+ def trace_id_header
22
+ ::Instana::Util.id_to_header(@trace_id)
23
+ end
24
+
25
+ def span_id_header
26
+ ::Instana::Util.id_to_header(@span_id)
27
+ end
28
+
29
+ def to_hash
30
+ { :trace_id => @trace_id, :span_id => @span_id }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,261 @@
1
+ module Instana
2
+ module Util
3
+ class << self
4
+ ID_RANGE = -2**63..2**63-1
5
+
6
+ # An agnostic approach to method aliasing.
7
+ #
8
+ # @param klass [Object] The class or module that holds the method to be alias'd.
9
+ # @param method [Symbol] The name of the method to be aliased.
10
+ #
11
+ def method_alias(klass, method)
12
+ if klass.method_defined?(method.to_sym) ||
13
+ klass.private_method_defined?(method.to_sym)
14
+
15
+ with = "#{method}_with_instana"
16
+ without = "#{method}_without_instana"
17
+
18
+ klass.class_eval do
19
+ alias_method without, method.to_s
20
+ alias_method method.to_s, with
21
+ end
22
+ else
23
+ ::Instana.logger.debug "No such method (#{method}) to alias on #{klass}"
24
+ end
25
+ end
26
+
27
+ # Calls on target_class to 'extend' cls
28
+ #
29
+ # @param target_cls [Object] the class/module to do the 'extending'
30
+ # @param cls [Object] the class/module to be 'extended'
31
+ #
32
+ def send_extend(target_cls, cls)
33
+ target_cls.send(:extend, cls) if defined?(target_cls)
34
+ end
35
+
36
+ # Calls on <target_cls> to include <cls> into itself.
37
+ #
38
+ # @param target_cls [Object] the class/module to do the 'including'
39
+ # @param cls [Object] the class/module to be 'included'
40
+ #
41
+ def send_include(target_cls, cls)
42
+ target_cls.send(:include, cls) if defined?(target_cls)
43
+ end
44
+
45
+ # Debugging helper method
46
+ #
47
+ def pry!
48
+ # Only valid for development or test environments
49
+ #env = ENV['RACK_ENV'] || ENV['RAILS_ENV']
50
+ #return unless %w(development, test).include? env
51
+ require 'pry-byebug'
52
+
53
+ if defined?(PryByebug)
54
+ Pry.commands.alias_command 'c', 'continue'
55
+ Pry.commands.alias_command 's', 'step'
56
+ Pry.commands.alias_command 'n', 'next'
57
+ Pry.commands.alias_command 'f', 'finish'
58
+
59
+ Pry::Commands.command(/^$/, 'repeat last command') do
60
+ _pry_.run_command Pry.history.to_a.last
61
+ end
62
+ end
63
+
64
+ binding.pry
65
+ rescue LoadError
66
+ ::Instana.logger.warn("No debugger in bundle. Couldn't load pry-byebug.")
67
+ end
68
+
69
+ # Retrieves and returns the source code for any ruby
70
+ # files requested by the UI via the host agent
71
+ #
72
+ # @param file [String] The fully qualified path to a file
73
+ #
74
+ def get_rb_source(file)
75
+ if (file =~ /.rb$/).nil?
76
+ { :error => "Only Ruby source files are allowed. (*.rb)" }
77
+ else
78
+ { :data => File.read(file) }
79
+ end
80
+ rescue => e
81
+ return { :error => e.inspect }
82
+ end
83
+
84
+ # Method to collect up process info for snapshots. This
85
+ # is generally used once per process.
86
+ #
87
+ def take_snapshot
88
+ data = {}
89
+
90
+ data[:sensorVersion] = ::Instana::VERSION
91
+ data[:ruby_version] = RUBY_VERSION
92
+ data[:rpl] = RUBY_PATCHLEVEL if defined?(RUBY_PATCHLEVEL)
93
+
94
+ # Framework Detection
95
+ if defined?(::RailsLts::VERSION)
96
+ data[:framework] = "Rails on Rails LTS-#{::RailsLts::VERSION}"
97
+
98
+ elsif defined?(::Rails.version)
99
+ data[:framework] = "Ruby on Rails #{::Rails.version}"
100
+
101
+ elsif defined?(::Grape::VERSION)
102
+ data[:framework] = "Grape #{::Grape::VERSION}"
103
+
104
+ elsif defined?(::Padrino::VERSION)
105
+ data[:framework] = "Padrino #{::Padrino::VERSION}"
106
+
107
+ elsif defined?(::Sinatra::VERSION)
108
+ data[:framework] = "Sinatra #{::Sinatra::VERSION}"
109
+ end
110
+
111
+ # Report Bundle
112
+ if defined?(::Gem) && Gem.respond_to?(:loaded_specs)
113
+ data[:versions] = {}
114
+
115
+ Gem.loaded_specs.each do |k, v|
116
+ data[:versions][k] = v.version.to_s
117
+ end
118
+ end
119
+
120
+ data
121
+ rescue => e
122
+ ::Instana.logger.debug { "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}" }
123
+ ::Instana.logger.debug { e.backtrace.join("\r\n") }
124
+ return data
125
+ end
126
+
127
+ # Used in class initialization and after a fork, this method
128
+ # collects up process information
129
+ #
130
+ def collect_process_info
131
+ process = {}
132
+ cmdline_file = "/proc/#{Process.pid}/cmdline"
133
+
134
+ # If there is a /proc filesystem, we read this manually so
135
+ # we can split on embedded null bytes. Otherwise (e.g. OSX, Windows)
136
+ # use ProcTable.
137
+ if File.exist?(cmdline_file)
138
+ cmdline = IO.read(cmdline_file).split(?\x00)
139
+ else
140
+ # Attempt to support older versions of sys-proctable and ffi.
141
+ #
142
+ # Alternatively we could use Sys::ProcTable::VERSION here but the
143
+ # consistency across historical versions is unknown. Alternative
144
+ # to the alternative, would be Ruby metaprogramming using the `arity`
145
+ # and `parameters` methods.
146
+ # e.g ProcTable.method(:ps).arity/parameters
147
+ if Gem.loaded_specs.key?("sys-proctable") &&
148
+ (Gem.loaded_specs["sys-proctable"].version >= Gem::Version.new("1.2.0"))
149
+ cmdline = ProcTable.ps(:pid => Process.pid).cmdline.split(' ')
150
+ else
151
+ cmdline = ProcTable.ps(Process.pid).cmdline.split(' ')
152
+ end
153
+ end
154
+
155
+ if RUBY_PLATFORM =~ /darwin/i
156
+ cmdline.delete_if{ |e| e.include?('=') }
157
+ process[:name] = cmdline.join(' ')
158
+ else
159
+ process[:name] = cmdline.shift
160
+ process[:arguments] = cmdline
161
+ end
162
+
163
+ process[:pid] = Process.pid
164
+ # This is usually Process.pid but in the case of containers, the host agent
165
+ # will return to us the true host pid in which we use to report data.
166
+ process[:report_pid] = nil
167
+ process
168
+ end
169
+
170
+ # Best effort to determine a name for the instrumented application
171
+ # on the dashboard.
172
+ #
173
+ def get_app_name
174
+ if ENV.key?('INSTANA_SERVICE_NAME')
175
+ return ENV['INSTANA_SERVICE_NAME']
176
+ end
177
+
178
+
179
+ if defined?(::Resque) && ($0 =~ /resque-#{Resque::Version}/)
180
+ return "Resque Worker"
181
+ end
182
+
183
+ if defined?(::RailsLts) || defined?(::Rails)
184
+ return Rails.application.class.to_s.split('::')[0]
185
+ end
186
+
187
+ return File.basename($0)
188
+ rescue Exception => e
189
+ Instana.logger.info "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
190
+ Instana.logger.debug { e.backtrace.join("\r\n") }
191
+ end
192
+
193
+ # Get the current time in milliseconds from the epoch
194
+ #
195
+ # @return [Integer] the current time in milliseconds
196
+ #
197
+ def now_in_ms
198
+ Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
199
+ end
200
+ # Prior method name support. To be deprecated when appropriate.
201
+ alias ts_now now_in_ms
202
+
203
+ # Convert a Time value to milliseconds
204
+ #
205
+ # @param time [Time]
206
+ #
207
+ def time_to_ms(time)
208
+ (time.to_f * 1000).floor
209
+ end
210
+
211
+ # Generate a random 64bit ID
212
+ #
213
+ # @return [Integer] a random 64bit integer
214
+ #
215
+ def generate_id
216
+ # Max value is 9223372036854775807 (signed long in Java)
217
+ rand(ID_RANGE)
218
+ end
219
+
220
+ # Convert an ID to a value appropriate to pass in a header.
221
+ #
222
+ # @param id [Integer] the id to be converted
223
+ #
224
+ # @return [String]
225
+ #
226
+ def id_to_header(id)
227
+ unless id.is_a?(Integer) || id.is_a?(String)
228
+ Instana.logger.debug "id_to_header received a #{id.class}: returning empty string"
229
+ return String.new
230
+ end
231
+ [id.to_i].pack('q>').unpack('H*')[0].gsub(/^0+/, '')
232
+ rescue => e
233
+ Instana.logger.info "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
234
+ Instana.logger.debug { e.backtrace.join("\r\n") }
235
+ end
236
+
237
+ # Convert a received header value into a valid ID
238
+ #
239
+ # @param header_id [String] the header value to be converted
240
+ #
241
+ # @return [Integer]
242
+ #
243
+ def header_to_id(header_id)
244
+ if !header_id.is_a?(String)
245
+ Instana.logger.debug "header_to_id received a #{header_id.class}: returning 0"
246
+ return 0
247
+ end
248
+ if header_id.length < 16
249
+ # The header is less than 16 chars. Prepend
250
+ # zeros so we can convert correctly
251
+ missing = 16 - header_id.length
252
+ header_id = ("0" * missing) + header_id
253
+ end
254
+ [header_id].pack("H*").unpack("q>")[0]
255
+ rescue => e
256
+ Instana.logger.info "#{__method__}:#{File.basename(__FILE__)}:#{__LINE__}: #{e.message}"
257
+ Instana.logger.debug { e.backtrace.join("\r\n") }
258
+ end
259
+ end
260
+ end
261
+ end
@@ -0,0 +1,4 @@
1
+ module Instana
2
+ VERSION = "1.10.1"
3
+ VERSION_FULL = "instana-#{VERSION}"
4
+ end
data/lib/oj_check.rb ADDED
@@ -0,0 +1,16 @@
1
+ begin
2
+ require 'oj'
3
+ rescue LoadError => e
4
+ # OJ is not available in JRuby
5
+ module Instana
6
+ class Oj
7
+ def self.dump(*args)
8
+ args.first.to_json
9
+ end
10
+
11
+ def self.load(*args)
12
+ JSON.parse args.first
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ require "instana/opentracing/tracer"
2
+ require "instana/opentracing/carrier"
3
+
4
+ # Set the global tracer to our OT tracer
5
+ # which supports the OT specification
6
+ OpenTracing.global_tracer = ::Instana.tracer
@@ -0,0 +1,143 @@
1
+ require 'test_helper'
2
+ require './lib/oj_check'
3
+
4
+ class AgentTest < Minitest::Test
5
+
6
+ Oj = ::Instana::Oj unless defined?(Oj)
7
+
8
+ def test_agent_host_detection
9
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
10
+ stub_request(:get, url)
11
+ assert_equal true, ::Instana.agent.host_agent_available?
12
+ end
13
+
14
+ def test_successful_discovery
15
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
16
+ docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
17
+ stub_request(:get, url)
18
+ stub_request(:get, docker_url)
19
+ discovered = ::Instana.agent.run_discovery
20
+
21
+ assert discovered.is_a?(Hash)
22
+ assert_equal "127.0.0.1", discovered[:agent_host]
23
+ assert_equal 42699, discovered[:agent_port]
24
+ end
25
+
26
+ def test_failed_discovery
27
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
28
+ docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
29
+ stub_request(:get, url).to_raise(Errno::ECONNREFUSED)
30
+ stub_request(:get, docker_url).to_raise(Errno::ECONNREFUSED)
31
+ discovered = ::Instana.agent.run_discovery
32
+
33
+ assert_equal nil, discovered
34
+ end
35
+
36
+ def test_custom_configure_agent
37
+ original_host = ::Instana.config[:agent_host]
38
+ original_port = ::Instana.config[:agent_port]
39
+
40
+ # Set custom agent/port
41
+ ::Instana.config[:agent_host] = '172.0.12.100'
42
+ ::Instana.config[:agent_port] = 12829
43
+
44
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/"
45
+ stub_request(:get, url)
46
+ discovered = ::Instana.agent.run_discovery
47
+
48
+ assert discovered.is_a?(Hash)
49
+ assert_equal "172.0.12.100", discovered[:agent_host]
50
+ assert_equal 12829, discovered[:agent_port]
51
+
52
+ ::Instana.config[:agent_host] = original_host
53
+ ::Instana.config[:agent_port] = original_port
54
+ end
55
+
56
+ def test_no_host_agent
57
+ localhost_url = "http://#{::Instana::Agent::LOCALHOST}:#{::Instana.config[:agent_port]}/"
58
+ stub_request(:get, localhost_url).to_raise(Errno::ECONNREFUSED)
59
+ docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
60
+ stub_request(:get, docker_url).to_timeout
61
+ assert_equal false, ::Instana.agent.host_agent_available?
62
+ end
63
+
64
+ def test_announce_sensor
65
+ # Fake discovery values
66
+ discovery = {}
67
+ discovery[:agent_host] = ::Instana.config[:agent_host]
68
+ discovery[:agent_port] = ::Instana.config[:agent_port]
69
+ ::Instana.agent.instance_variable_set(:@discovered, discovery)
70
+
71
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"
72
+ json = Oj.dump({ 'pid' => Process.pid, 'agentUuid' => 'abc' })
73
+ stub_request(:put, url).to_return(:body => json, :status => 200)
74
+
75
+ assert_equal true, ::Instana.agent.announce_sensor
76
+ end
77
+
78
+ def test_failed_announce_sensor
79
+ # Fake discovery values
80
+ discovery = {}
81
+ discovery[:agent_host] = ::Instana.config[:agent_host]
82
+ discovery[:agent_port] = ::Instana.config[:agent_port]
83
+ ::Instana.agent.instance_variable_set(:@discovered, discovery)
84
+
85
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"
86
+ stub_request(:put, url).to_raise(Errno::ECONNREFUSED)
87
+
88
+ assert_equal false, ::Instana.agent.announce_sensor
89
+ end
90
+
91
+ def test_metric_report
92
+ # Fake discovery values
93
+ discovery = {}
94
+ discovery[:agent_host] = ::Instana.config[:agent_host]
95
+ discovery[:agent_port] = ::Instana.config[:agent_port]
96
+ ::Instana.agent.instance_variable_set(:@discovered, discovery)
97
+
98
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"
99
+ json = Oj.dump({ 'pid' => Process.pid, 'agentUuid' => 'abc' })
100
+ stub_request(:put, url).to_return(:body => json, :status => 200)
101
+ ::Instana.agent.announce_sensor
102
+
103
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.#{Process.pid}"
104
+ stub_request(:post, url)
105
+
106
+ payload = { :test => 'true' }
107
+ assert_equal true, ::Instana.agent.report_metrics(payload)
108
+ end
109
+
110
+ def test_failed_metric_report
111
+ # Fake discovery values
112
+ discovery = {}
113
+ discovery[:agent_host] = ::Instana.config[:agent_host]
114
+ discovery[:agent_port] = ::Instana.config[:agent_port]
115
+ ::Instana.agent.instance_variable_set(:@discovered, discovery)
116
+
117
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.discovery"
118
+ json = { 'pid' => Process.pid, 'agentUuid' => 'abc' }.to_json
119
+ stub_request(:put, url).to_return(:body => json, :status => 200)
120
+
121
+ ::Instana.agent.announce_sensor
122
+
123
+ url = "http://#{::Instana.config[:agent_host]}:#{::Instana.config[:agent_port]}/com.instana.plugin.ruby.#{Process.pid}"
124
+ stub_request(:post, url).to_raise(Errno::ECONNREFUSED)
125
+
126
+ payload = { :test => 'true' }
127
+ assert_equal false, ::Instana.agent.report_metrics(payload)
128
+ end
129
+
130
+ def test_agent_timeout
131
+ # Fake discovery values
132
+ discovery = {}
133
+ discovery[:agent_host] = ::Instana.config[:agent_host]
134
+ discovery[:agent_port] = ::Instana.config[:agent_port]
135
+ ::Instana.agent.instance_variable_set(:@discovered, discovery)
136
+
137
+ localhost_url = "http://#{::Instana::Agent::LOCALHOST}:#{::Instana.config[:agent_port]}/"
138
+ stub_request(:get, localhost_url).to_timeout
139
+ docker_url = "http://#{::Instana.agent.instance_variable_get(:@default_gateway)}:#{::Instana.config[:agent_port]}/"
140
+ stub_request(:get, docker_url).to_timeout
141
+ assert_equal false, ::Instana.agent.host_agent_available?
142
+ end
143
+ end