instana 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +2 -0
  3. data/.gitignore +1 -0
  4. data/.travis.yml +24 -2
  5. data/Gemfile +2 -22
  6. data/README.md +1 -1
  7. data/Rakefile +15 -0
  8. data/Tracing.md +29 -2
  9. data/gemfiles/libraries.gemfile +50 -0
  10. data/gemfiles/rails32.gemfile +45 -0
  11. data/gemfiles/rails42.gemfile +44 -0
  12. data/gemfiles/rails50.gemfile +44 -0
  13. data/instana.gemspec +6 -8
  14. data/lib/instana/config.rb +7 -5
  15. data/lib/instana/frameworks/instrumentation/abstract_mysql_adapter.rb +55 -0
  16. data/lib/instana/frameworks/instrumentation/action_controller.rb +105 -0
  17. data/lib/instana/frameworks/instrumentation/active_record.rb +22 -0
  18. data/lib/instana/frameworks/instrumentation/mysql2_adapter.rb +81 -0
  19. data/lib/instana/frameworks/instrumentation/mysql_adapter.rb +56 -0
  20. data/lib/instana/frameworks/instrumentation/postgresql_adapter.rb +71 -0
  21. data/lib/instana/frameworks/rails.rb +5 -0
  22. data/lib/instana/test.rb +40 -0
  23. data/lib/instana/tracer.rb +19 -0
  24. data/lib/instana/tracing/span.rb +3 -3
  25. data/lib/instana/version.rb +1 -1
  26. data/log/.keep +0 -0
  27. data/test/agent/agent_test.rb +139 -0
  28. data/test/apps/cuba.rb +15 -0
  29. data/test/apps/roda.rb +10 -0
  30. data/test/apps/sinatra.rb +5 -0
  31. data/test/config_test.rb +17 -0
  32. data/test/frameworks/cuba_test.rb +44 -0
  33. data/test/frameworks/rack_test.rb +152 -0
  34. data/test/frameworks/rails/actioncontroller_test.rb +123 -0
  35. data/test/frameworks/rails/activerecord3_test.rb +134 -0
  36. data/test/frameworks/rails/activerecord4_test.rb +134 -0
  37. data/test/frameworks/rails/activerecord5_test.rb +90 -0
  38. data/test/frameworks/roda_test.rb +44 -0
  39. data/test/frameworks/sinatra_test.rb +44 -0
  40. data/test/instana_test.rb +27 -0
  41. data/test/instrumentation/dalli_test.rb +274 -0
  42. data/test/instrumentation/excon_test.rb +171 -0
  43. data/test/instrumentation/net-http_test.rb +140 -0
  44. data/test/instrumentation/rest-client_test.rb +61 -0
  45. data/test/models/block.rb +18 -0
  46. data/test/servers/rackapp_6511.rb +20 -0
  47. data/test/servers/rails_3205.rb +95 -0
  48. data/test/test_helper.rb +39 -0
  49. data/test/tracing/custom_test.rb +143 -0
  50. data/test/tracing/id_management_test.rb +96 -0
  51. data/test/tracing/opentracing_test.rb +377 -0
  52. data/test/tracing/trace_test.rb +50 -0
  53. data/test/tracing/tracer_async_test.rb +298 -0
  54. data/test/tracing/tracer_test.rb +202 -0
  55. metadata +114 -4
@@ -0,0 +1,96 @@
1
+
2
+ require 'test_helper'
3
+
4
+ class TracerIDMgmtTest < Minitest::Test
5
+ def test_id_to_header_conversion
6
+ # Test passing a standard Integer ID
7
+ original_id = ::Instana::Util.generate_id
8
+ converted_id = Instana::Util.id_to_header(original_id)
9
+
10
+ # Assert that it is a string and there are no non-hex characters
11
+ assert converted_id.is_a?(String)
12
+ assert !converted_id[/\H/]
13
+
14
+ # Test passing a standard Integer ID as a String
15
+ original_id = ::Instana::Util.generate_id
16
+ converted_id = Instana::Util.id_to_header(original_id)
17
+
18
+ # Assert that it is a string and there are no non-hex characters
19
+ assert converted_id.is_a?(String)
20
+ assert !converted_id[/\H/]
21
+ end
22
+
23
+ def test_id_to_header_conversion_with_bogus_id
24
+ # Test passing an empty String
25
+ converted_id = Instana::Util.id_to_header('')
26
+
27
+ # Assert that it is a string and there are no non-hex characters
28
+ assert converted_id.is_a?(String)
29
+ assert converted_id == "0000000000000000"
30
+
31
+ # Test passing a nil
32
+ converted_id = Instana::Util.id_to_header(nil)
33
+
34
+ # Assert that it is a string and there are no non-hex characters
35
+ assert converted_id.is_a?(String)
36
+ assert converted_id == ''
37
+
38
+ # Test passing an Array
39
+ converted_id = Instana::Util.id_to_header([])
40
+
41
+ # Assert that it is a string and there are no non-hex characters
42
+ assert converted_id.is_a?(String)
43
+ assert converted_id == ''
44
+ end
45
+
46
+ def test_header_to_id_conversion
47
+ # Get a hex string to test against & convert
48
+ header_id = Instana::Util.id_to_header(::Instana::Util.generate_id)
49
+ converted_id = Instana::Util.header_to_id(header_id)
50
+
51
+ # Assert that it is an Integer
52
+ assert converted_id.is_a?(Integer)
53
+ end
54
+
55
+ def test_header_to_id_conversion_with_bogus_header
56
+ # Bogus nil arg
57
+ bogus_result = Instana::Util.header_to_id(nil)
58
+ assert_equal 0, bogus_result
59
+
60
+ # Bogus Integer arg
61
+ bogus_result = Instana::Util.header_to_id(1234)
62
+ assert_equal 0, bogus_result
63
+
64
+ # Bogus Array arg
65
+ bogus_result = Instana::Util.header_to_id([1234])
66
+ assert_equal 0, bogus_result
67
+ end
68
+
69
+ def test_id_conversion_back_and_forth
70
+ # id --> header --> id
71
+ original_id = ::Instana::Util.generate_id
72
+ header_id = Instana::Util.id_to_header(original_id)
73
+ converted_back_id = Instana::Util.header_to_id(header_id)
74
+ assert original_id == converted_back_id
75
+
76
+ # header --> id --> header
77
+ original_header_id = "c025ee93b1aeda7b"
78
+ id = Instana::Util.header_to_id(original_header_id)
79
+ converted_back_header_id = Instana::Util.id_to_header(id)
80
+ assert_equal original_header_id, converted_back_header_id
81
+ end
82
+
83
+ def test_id_max_value_and_conversion
84
+ max_id = 9223372036854775807
85
+ min_id = -9223372036854775808
86
+ max_hex = "7fffffffffffffff"
87
+ min_hex = "8000000000000000"
88
+
89
+ assert_equal max_hex, Instana::Util.id_to_header(max_id)
90
+ assert_equal min_hex, Instana::Util.id_to_header(min_id)
91
+
92
+ assert_equal max_id, Instana::Util.header_to_id(max_hex)
93
+ assert_equal min_id, Instana::Util.header_to_id(min_hex)
94
+ end
95
+
96
+ end
@@ -0,0 +1,377 @@
1
+ require 'test_helper'
2
+ require 'rack/test'
3
+ require 'rack/lobster'
4
+ require "opentracing"
5
+
6
+ module Instana
7
+ class OTRack1
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ otrack1_span = OpenTracing.start_span(:otrack1)
14
+ result = @app.call(env)
15
+ otrack1_span.finish
16
+ result
17
+ end
18
+ end
19
+
20
+ class OTRack2
21
+ def initialize(app)
22
+ @app = app
23
+ end
24
+
25
+ def call(env)
26
+ otrack2_span = OpenTracing.start_span(:otrack2)
27
+ result = @app.call(env)
28
+ otrack2_span.finish
29
+ result
30
+ end
31
+ end
32
+ end
33
+
34
+ OpenTracing.global_tracer = ::Instana.tracer
35
+
36
+ class OpenTracerTest < Minitest::Test
37
+ include Rack::Test::Methods
38
+
39
+ def app
40
+ @app = Rack::Builder.new {
41
+ use Instana::Rack
42
+ use Instana::OTRack1
43
+ use Rack::CommonLogger
44
+ use Rack::ShowExceptions
45
+ use Instana::OTRack2
46
+ map "/mrlobster" do
47
+ run Rack::Lobster.new
48
+ end
49
+ }
50
+ end
51
+
52
+ def test_supplies_all_ot_interfaces
53
+ clear_all!
54
+ assert defined?(OpenTracing)
55
+ assert OpenTracing.respond_to?(:global_tracer)
56
+ assert OpenTracing.global_tracer.respond_to?(:start_span)
57
+ assert OpenTracing.global_tracer.respond_to?(:inject)
58
+ assert OpenTracing.global_tracer.respond_to?(:extract)
59
+
60
+ assert defined?(OpenTracing::Carrier)
61
+ carrier = OpenTracing::Carrier.new
62
+ assert carrier.respond_to?(:[])
63
+ assert carrier.respond_to?(:[]=)
64
+ assert carrier.respond_to?(:each)
65
+
66
+ span = OpenTracing.start_span(:blah)
67
+ assert span.respond_to?(:finish)
68
+ assert span.respond_to?(:set_tag)
69
+ assert span.respond_to?(:tags)
70
+ assert span.respond_to?(:operation_name=)
71
+ assert span.respond_to?(:set_baggage_item)
72
+ assert span.respond_to?(:get_baggage_item)
73
+ assert span.respond_to?(:context)
74
+ assert span.respond_to?(:log)
75
+ end
76
+
77
+ def test_basic_get_with_opentracing
78
+ clear_all!
79
+ get '/mrlobster'
80
+ assert last_response.ok?
81
+
82
+ traces = ::Instana.processor.queued_traces
83
+ assert_equal 1, traces.count
84
+
85
+ trace = traces[0]
86
+ assert trace.valid?
87
+
88
+ # Span validation
89
+ assert_equal 3, trace.spans.count
90
+ first_span, second_span, third_span = trace.spans.to_a
91
+
92
+ assert_equal :rack, first_span.name
93
+ assert first_span.key?(:data)
94
+ assert first_span[:data].key?(:http)
95
+ assert_equal "GET", first_span[:data][:http][:method]
96
+ assert_equal "/mrlobster", first_span[:data][:http][:url]
97
+ assert_equal 200, first_span[:data][:http][:status]
98
+ assert_equal 'example.org', first_span[:data][:http][:host]
99
+ assert_equal :otrack1, second_span.name
100
+ assert second_span.key?(:data)
101
+ assert second_span[:data].key?(:sdk)
102
+ assert second_span[:data][:sdk].key?(:name)
103
+ assert_equal :otrack2, third_span.name
104
+ assert third_span.key?(:data)
105
+ assert third_span[:data].key?(:sdk)
106
+ assert third_span[:data][:sdk].key?(:name)
107
+
108
+ # ID Validation
109
+ assert_equal trace.id, first_span[:t]
110
+ assert_equal trace.id, third_span[:t]
111
+
112
+ # Linkage
113
+ assert_equal second_span[:s], third_span[:p]
114
+ end
115
+
116
+ def test_get_with_inject_extract
117
+ clear_all!
118
+
119
+ trace_id = ::Instana::Util.generate_id
120
+ span_id = ::Instana::Util.generate_id
121
+
122
+ header 'X-Instana-T', ::Instana::Util.id_to_header(trace_id)
123
+ header 'X-Instana-S', ::Instana::Util.id_to_header(span_id)
124
+
125
+ get '/mrlobster'
126
+ assert last_response.ok?
127
+
128
+ traces = ::Instana.processor.queued_traces
129
+ assert_equal 1, traces.count
130
+
131
+ trace = traces[0]
132
+ assert trace.valid?
133
+
134
+ # Span validation
135
+ assert_equal 3, trace.spans.count
136
+ first_span = trace.spans.to_a.first
137
+
138
+ # Make sure context was picked up and continued in the resulting
139
+ # trace
140
+ assert_equal trace_id, trace.id
141
+ assert_equal span_id, first_span[:p]
142
+ end
143
+
144
+ def test_start_span_with_tags
145
+ clear_all!
146
+ span = OpenTracing.start_span('my_app_entry')
147
+
148
+ assert span.is_a?(::Instana::Span)
149
+ assert_equal :my_app_entry, OpenTracing.current_trace.current_span.name
150
+
151
+ span.set_tag(:tag_integer, 1234)
152
+ span.set_tag(:tag_boolean, true)
153
+ span.set_tag(:tag_array, [1,2,3,4])
154
+ span.set_tag(:tag_string, "1234")
155
+
156
+ assert_equal 1234, span.tags(:tag_integer)
157
+ assert_equal true, span.tags(:tag_boolean)
158
+ assert_equal [1,2,3,4], span.tags(:tag_array)
159
+ assert_equal "1234", span.tags(:tag_string)
160
+ span.finish
161
+ end
162
+
163
+ def test_start_span_with_baggage
164
+ clear_all!
165
+ span = OpenTracing.start_span('my_app_entry')
166
+ span.set_baggage_item(:baggage_integer, 1234)
167
+ span.set_baggage_item(:baggage_boolean, false)
168
+ span.set_baggage_item(:baggage_array, [1,2,3,4])
169
+ span.set_baggage_item(:baggage_string, '1234')
170
+
171
+ assert_equal 1234, span.get_baggage_item(:baggage_integer)
172
+ assert_equal false, span.get_baggage_item(:baggage_boolean)
173
+ assert_equal [1,2,3,4], span.get_baggage_item(:baggage_array)
174
+ assert_equal "1234", span.get_baggage_item(:baggage_string)
175
+ span.finish
176
+ end
177
+
178
+ def test_start_span_with_timestamps
179
+ clear_all!
180
+ span_tags = {:start_tag => 1234, :another_tag => 'tag_value'}
181
+
182
+ ts_start = Time.now - 1 # Put start time a bit in the past
183
+ ts_start_ms = ::Instana::Util.time_to_ms(ts_start)
184
+
185
+ span = OpenTracing.start_span('my_app_entry', tags: span_tags, start_time: ts_start)
186
+ sleep 0.1
187
+
188
+ ts_finish = Time.now + 5 # Put end time in the future
189
+ ts_finish_ms = ::Instana::Util.time_to_ms(ts_finish)
190
+
191
+ span.finish(ts_finish)
192
+
193
+ assert_equal ts_start_ms, span[:ts]
194
+ assert_equal (ts_finish_ms - ts_start_ms), span[:d]
195
+ end
196
+
197
+ def test_nested_spans_using_child_of
198
+ clear_all!
199
+ entry_span = OpenTracing.start_span(:rack)
200
+ ac_span = OpenTracing.start_span(:action_controller, child_of: entry_span)
201
+ av_span = OpenTracing.start_span(:action_view, child_of: ac_span)
202
+ sleep 0.1
203
+ av_span.finish
204
+ ac_span.finish
205
+ entry_span.finish
206
+
207
+ traces = ::Instana.processor.queued_traces
208
+
209
+ assert_equal 1, traces.count
210
+
211
+ trace = traces.first
212
+ first_span, second_span, third_span = trace.spans.to_a
213
+
214
+ assert_equal 3, trace.spans.count
215
+ assert trace.valid?
216
+
217
+ # IDs
218
+ assert_equal trace.id, first_span[:t]
219
+ assert_equal trace.id, second_span[:t]
220
+ assert_equal trace.id, third_span[:t]
221
+
222
+ # Linkage
223
+ assert first_span.is_root?
224
+ assert_equal first_span[:s], second_span[:p]
225
+ assert_equal second_span[:s], third_span[:p]
226
+ end
227
+
228
+ def test_start_span_with_nested_spans
229
+ clear_all!
230
+ entry_span = OpenTracing.start_span(:rack)
231
+ ac_span = OpenTracing.start_span(:action_controller)
232
+ av_span = OpenTracing.start_span(:action_view)
233
+ sleep 0.1
234
+ av_span.finish
235
+ ac_span.finish
236
+ entry_span.finish
237
+
238
+ traces = ::Instana.processor.queued_traces
239
+
240
+ assert_equal 1, traces.count
241
+ trace = traces.first
242
+ assert trace.valid?
243
+ assert_equal 3, trace.spans.count
244
+
245
+ first_span, second_span, third_span = trace.spans.to_a
246
+
247
+ # IDs
248
+ assert_equal trace.id, first_span[:t]
249
+ assert_equal trace.id, second_span[:t]
250
+ assert_equal trace.id, third_span[:t]
251
+
252
+ # Linkage
253
+ assert first_span.is_root?
254
+ assert_equal first_span[:s], second_span[:p]
255
+ assert_equal second_span[:s], third_span[:p]
256
+ end
257
+
258
+ def test_nested_spans_with_baggage
259
+ clear_all!
260
+ entry_span = OpenTracing.start_span(:rack)
261
+ ac_span = OpenTracing.start_span(:action_controller)
262
+ ac_span.set_baggage_item(:my_bag, 1)
263
+ av_span = OpenTracing.start_span(:action_view)
264
+ sleep 0.1
265
+ av_span.finish
266
+ ac_span.finish
267
+ entry_span.finish
268
+
269
+ traces = ::Instana.processor.queued_traces
270
+
271
+ assert_equal 1, traces.count
272
+ trace = traces.first
273
+ assert trace.valid?
274
+ assert_equal 3, trace.spans.count
275
+
276
+ first_span, second_span, third_span = trace.spans.to_a
277
+
278
+ # IDs
279
+ assert_equal trace.id, first_span[:t]
280
+ assert_equal trace.id, second_span[:t]
281
+ assert_equal trace.id, third_span[:t]
282
+
283
+ # Linkage
284
+ assert first_span.is_root?
285
+ assert_equal first_span[:s], second_span[:p]
286
+ assert_equal second_span[:s], third_span[:p]
287
+
288
+ # Every span should have baggage
289
+ assert_equal nil, first_span.get_baggage_item(:my_bag)
290
+ assert_equal 1, second_span.get_baggage_item(:my_bag)
291
+ assert_equal 1, third_span.get_baggage_item(:my_bag)
292
+ end
293
+
294
+ def test_context_should_carry_baggage
295
+ clear_all!
296
+
297
+ entry_span = OpenTracing.start_span(:rack)
298
+ entry_span_context = entry_span.context
299
+
300
+ ac_span = OpenTracing.start_span(:action_controller)
301
+ ac_span.set_baggage_item(:my_bag, 1)
302
+ ac_span_context = ac_span.context
303
+
304
+ av_span = OpenTracing.start_span(:action_view)
305
+ av_span_context = av_span.context
306
+
307
+ sleep 0.1
308
+ av_span.finish
309
+ ac_span.finish
310
+ entry_span.finish
311
+
312
+ traces = ::Instana.processor.queued_traces
313
+
314
+ assert_equal 1, traces.count
315
+ trace = traces.first
316
+ assert trace.valid?
317
+ assert_equal 3, trace.spans.count
318
+
319
+ assert_equal nil, entry_span_context.baggage[:my_bag]
320
+ assert_equal 1, ac_span_context.baggage[:my_bag]
321
+ assert_equal 1, av_span_context.baggage[:my_bag]
322
+ end
323
+
324
+ def test_baggage_with_complex_data
325
+ clear_all!
326
+
327
+ entry_span = OpenTracing.start_span(:rack)
328
+ entry_span_context = entry_span.context
329
+
330
+ ac_span = OpenTracing.start_span(:action_controller)
331
+
332
+ ac_span.set_baggage_item(:integer, 1)
333
+ ac_span.set_baggage_item(:float, 1.0123948293)
334
+ ac_span.set_baggage_item(:hash, { :hash_sublevel => "blah",
335
+ :another => {} })
336
+ ac_span_context = ac_span.context
337
+
338
+ av_span = OpenTracing.start_span(:action_view)
339
+ av_span_context = av_span.context
340
+
341
+ sleep 0.1
342
+ av_span.finish
343
+ ac_span.finish
344
+ entry_span.finish
345
+
346
+ traces = ::Instana.processor.queued_traces
347
+
348
+ assert_equal 1, traces.count
349
+ trace = traces.first
350
+ assert trace.valid?
351
+ assert_equal 3, trace.spans.count
352
+
353
+ # Context
354
+ assert_equal true, entry_span_context.baggage.empty?
355
+ assert_equal true, entry_span.baggage.empty?
356
+
357
+ assert_equal 1, ac_span_context.baggage[:integer]
358
+ assert_equal 1.0123948293, ac_span_context.baggage[:float]
359
+ assert_equal true, ac_span_context.baggage[:hash][:another].empty?
360
+ assert_equal "blah", ac_span_context.baggage[:hash][:hash_sublevel]
361
+ assert_equal 1, av_span_context.baggage[:integer]
362
+ assert_equal 1.0123948293, av_span_context.baggage[:float]
363
+ assert_equal true, av_span_context.baggage[:hash][:another].empty?
364
+ assert_equal "blah", av_span_context.baggage[:hash][:hash_sublevel]
365
+
366
+ # Spans
367
+ assert_equal true, entry_span.baggage.empty?
368
+ assert_equal 1, ac_span.baggage[:integer]
369
+ assert_equal 1.0123948293, ac_span.baggage[:float]
370
+ assert_equal true, ac_span.baggage[:hash][:another].empty?
371
+ assert_equal "blah", ac_span.baggage[:hash][:hash_sublevel]
372
+ assert_equal 1, av_span.baggage[:integer]
373
+ assert_equal 1.0123948293, av_span.baggage[:float]
374
+ assert_equal true, av_span.baggage[:hash][:another].empty?
375
+ assert_equal "blah", av_span.baggage[:hash][:hash_sublevel]
376
+ end
377
+ end