instana 1.11.7 → 1.192.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.editorconfig +10 -0
  3. data/Rakefile +26 -37
  4. data/gemfiles/libraries.gemfile +2 -0
  5. data/lib/instana/agent.rb +6 -0
  6. data/lib/instana/base.rb +2 -0
  7. data/lib/instana/config.rb +11 -0
  8. data/lib/instana/frameworks/cuba.rb +33 -0
  9. data/lib/instana/frameworks/instrumentation/action_controller.rb +11 -0
  10. data/lib/instana/frameworks/instrumentation/mysql2_adapter.rb +7 -1
  11. data/lib/instana/frameworks/instrumentation/mysql_adapter.rb +7 -1
  12. data/lib/instana/frameworks/instrumentation/postgresql_adapter.rb +23 -5
  13. data/lib/instana/frameworks/roda.rb +41 -0
  14. data/lib/instana/frameworks/sinatra.rb +17 -0
  15. data/lib/instana/instrumentation/excon.rb +1 -1
  16. data/lib/instana/instrumentation/graphql.rb +77 -0
  17. data/lib/instana/instrumentation/instrumented_request.rb +68 -0
  18. data/lib/instana/instrumentation/net-http.rb +2 -0
  19. data/lib/instana/instrumentation/rack.rb +17 -52
  20. data/lib/instana/secrets.rb +42 -0
  21. data/lib/instana/setup.rb +1 -0
  22. data/lib/instana/tracer.rb +6 -0
  23. data/lib/instana/tracing/span.rb +23 -10
  24. data/lib/instana/util.rb +15 -29
  25. data/lib/instana/version.rb +1 -1
  26. data/test/apps/cuba.rb +4 -0
  27. data/test/apps/roda.rb +3 -0
  28. data/test/apps/sinatra.rb +4 -0
  29. data/test/config_test.rb +1 -17
  30. data/test/frameworks/cuba_test.rb +14 -1
  31. data/test/frameworks/rack_test.rb +52 -19
  32. data/test/frameworks/rails/actioncontroller_test.rb +12 -0
  33. data/test/frameworks/rails/activerecord_test.rb +80 -28
  34. data/test/frameworks/roda_test.rb +14 -0
  35. data/test/frameworks/sinatra_test.rb +14 -0
  36. data/test/instrumentation/excon_test.rb +0 -2
  37. data/test/instrumentation/graphql_test.rb +116 -0
  38. data/test/instrumentation/instrumented_request_test.rb +84 -0
  39. data/test/instrumentation/redis_test.rb +10 -0
  40. data/test/secrets_test.rb +73 -0
  41. data/test/tracing/id_management_test.rb +4 -66
  42. data/test/tracing/tracer_test.rb +31 -1
  43. metadata +16 -8
  44. data/test/tracing/trace_test.rb +0 -67
@@ -1,4 +1,3 @@
1
-
2
1
  if defined?(::Cuba)
3
2
  require 'test_helper'
4
3
  require File.expand_path(File.dirname(__FILE__) + '/../apps/cuba')
@@ -40,5 +39,19 @@ if defined?(::Cuba)
40
39
  assert first_span[:data][:http].key?(:host)
41
40
  assert_equal "example.org", first_span[:data][:http][:host]
42
41
  end
42
+
43
+ def test_path_template
44
+ clear_all!
45
+
46
+ r = get '/greet/instana'
47
+ assert last_response.ok?
48
+
49
+ spans = ::Instana.processor.queued_spans
50
+ assert_equal 1, spans.count
51
+
52
+ first_span = spans.first
53
+ assert_equal :rack, first_span[:n]
54
+ assert_equal '/greet/{name}', first_span[:data][:http][:path_tpl]
55
+ end
43
56
  end
44
57
  end
@@ -6,15 +6,21 @@ require "instana/rack"
6
6
  class RackTest < Minitest::Test
7
7
  include Rack::Test::Methods
8
8
 
9
+ class PathTemplateApp
10
+ def call(env)
11
+ env['INSTANA_HTTP_PATH_TEMPLATE'] = 'sample_template'
12
+ [200, {}, ['Ok']]
13
+ end
14
+ end
15
+
9
16
  def app
10
- @app = Rack::Builder.new {
17
+ @app = Rack::Builder.new do
11
18
  use Rack::CommonLogger
12
19
  use Rack::ShowExceptions
13
20
  use Instana::Rack
14
- map "/mrlobster" do
15
- run Rack::Lobster.new
16
- end
17
- }
21
+ map("/mrlobster") { run Rack::Lobster.new }
22
+ map("/path_tpl") { run PathTemplateApp.new }
23
+ end
18
24
  end
19
25
 
20
26
  def test_basic_get
@@ -51,11 +57,7 @@ class RackTest < Minitest::Test
51
57
  assert rack_span[:f].key?(:e)
52
58
  assert rack_span[:f].key?(:h)
53
59
  assert_equal ::Instana.agent.agent_uuid, rack_span[:f][:h]
54
-
55
- # Backtrace fingerprint validation
56
- assert rack_span.key?(:stack)
57
- assert_equal 2, rack_span[:stack].count
58
- refute_nil rack_span[:stack].first[:c].match(/instana\/instrumentation\/rack.rb/)
60
+ assert !rack_span.key?(:stack)
59
61
 
60
62
  # Restore to default
61
63
  ::Instana.config[:collect_backtraces] = false
@@ -145,8 +147,9 @@ class RackTest < Minitest::Test
145
147
 
146
148
  def test_context_continuation
147
149
  clear_all!
148
- header 'X-INSTANA-T', Instana::Util.id_to_header(1234)
149
- header 'X-INSTANA-S', Instana::Util.id_to_header(4321)
150
+ continuation_id = Instana::Util.generate_id
151
+ header 'X-INSTANA-T', continuation_id
152
+ header 'X-INSTANA-S', continuation_id
150
153
 
151
154
  get '/mrlobster'
152
155
  assert last_response.ok?
@@ -180,10 +183,30 @@ class RackTest < Minitest::Test
180
183
  # Context validation
181
184
  # The first span should have the passed in trace ID
182
185
  # and specify the passed in span ID as it's parent.
183
- assert_equal 1234, rack_span[:t]
184
- assert_equal 4321, rack_span[:p]
186
+ assert_equal continuation_id, rack_span[:t]
187
+ assert_equal continuation_id, rack_span[:p]
185
188
  end
186
189
 
190
+ def test_correlation_information
191
+ clear_all!
192
+
193
+ header 'X-INSTANA-L', '1,correlationType=test;correlationId=abcdefh123'
194
+
195
+ get '/mrlobster'
196
+ assert last_response.ok?
197
+
198
+ spans = ::Instana.processor.queued_spans
199
+
200
+ # Span validation
201
+ assert_equal 1, spans.count
202
+ rack_span = spans.first
203
+ assert_equal :rack, rack_span[:n]
204
+
205
+ assert_equal 'abcdefh123', rack_span[:crid]
206
+ assert_equal 'test', rack_span[:crtp]
207
+ end
208
+
209
+
187
210
  def test_instana_response_headers
188
211
  clear_all!
189
212
  get '/mrlobster'
@@ -227,14 +250,24 @@ class RackTest < Minitest::Test
227
250
  assert rack_span[:data][:http][:header].key?(:"X-Capture-This")
228
251
  assert !rack_span[:data][:http][:header].key?(:"X-Capture-That")
229
252
  assert_equal "ThereYouGo", rack_span[:data][:http][:header][:"X-Capture-This"]
230
-
231
- # Backtrace fingerprint validation
232
- assert rack_span.key?(:stack)
233
- assert_equal 2, rack_span[:stack].count
234
- refute_nil rack_span[:stack].first[:c].match(/instana\/instrumentation\/rack.rb/)
253
+ assert !rack_span.key?(:stack)
235
254
 
236
255
  # Restore to default
237
256
  ::Instana.config[:collect_backtraces] = false
238
257
  ::Instana.agent.extra_headers = nil
239
258
  end
259
+
260
+ def test_capture_http_path_template
261
+ clear_all!
262
+
263
+ get '/path_tpl'
264
+ assert last_response.ok?
265
+
266
+ spans = ::Instana.processor.queued_spans
267
+ assert_equal 1, spans.length
268
+
269
+ rack_span = spans.first
270
+ assert_equal :rack, rack_span[:n]
271
+ assert_equal 'sample_template', rack_span[:data][:http][:path_tpl]
272
+ end
240
273
  end
@@ -153,4 +153,16 @@ class ActionControllerTest < Minitest::Test
153
153
  assert ac_span.key?(:stack)
154
154
  assert 1, ac_span[:ec]
155
155
  end
156
+
157
+ def test_path_template
158
+ clear_all!
159
+
160
+ Net::HTTP.get(URI.parse('http://localhost:3205/test/world'))
161
+
162
+ spans = ::Instana.processor.queued_spans
163
+ rack_span = find_first_span_by_name(spans, :rack)
164
+
165
+ assert_equal false, rack_span.key?(:error)
166
+ assert_equal '/test/world(.:format)', rack_span[:data][:http][:path_tpl]
167
+ end
156
168
  end
@@ -2,7 +2,14 @@ require 'test_helper'
2
2
  require 'active_record'
3
3
 
4
4
  class ActiveRecordTest < Minitest::Test
5
+
6
+ def teardown
7
+ # Make sure defaults are back in place
8
+ ::Instana.config[:sanitize_sql] = true
9
+ end
10
+
5
11
  def test_config_defaults
12
+ assert ::Instana.config[:sanitize_sql] == true
6
13
  assert ::Instana.config[:active_record].is_a?(Hash)
7
14
  assert ::Instana.config[:active_record].key?(:enabled)
8
15
  assert_equal true, ::Instana.config[:active_record][:enabled]
@@ -28,19 +35,14 @@ class ActiveRecordTest < Minitest::Test
28
35
  assert span[:data][:activerecord].key?(:username)
29
36
  end
30
37
 
31
-
32
- found = false
33
38
  if ::Rails::VERSION::MAJOR < 4
34
39
  sql = "INSERT INTO \"blocks\" (\"color\", \"created_at\", \"name\", \"updated_at\") VALUES ($?, $?, $?, $?) RETURNING \"id\""
35
40
  else
36
41
  sql = "INSERT INTO \"blocks\" (\"name\", \"color\", \"created_at\", \"updated_at\") VALUES ($?, $?, $?, $?) RETURNING \"id\""
37
42
  end
38
- ar_spans.each do |span|
39
- if span[:data][:activerecord][:sql] ==
40
- found = true
41
- end
43
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
44
+ span[:data][:activerecord][:sql] == sql
42
45
  end
43
- assert found
44
46
 
45
47
  found = false
46
48
  if ::Rails::VERSION::MAJOR >= 5
@@ -50,12 +52,9 @@ class ActiveRecordTest < Minitest::Test
50
52
  else
51
53
  sql = "SELECT \"blocks\".* FROM \"blocks\" WHERE \"blocks\".\"name\" = ? LIMIT ?"
52
54
  end
53
- ar_spans.each do |span|
54
- if span[:data][:activerecord][:sql] == sql
55
- found = true
56
- end
55
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
56
+ span[:data][:activerecord][:sql] == sql
57
57
  end
58
- assert found
59
58
 
60
59
  found = false
61
60
  if ::Rails::VERSION::MAJOR == 3
@@ -63,12 +62,73 @@ class ActiveRecordTest < Minitest::Test
63
62
  else
64
63
  sql = "DELETE FROM \"blocks\" WHERE \"blocks\".\"id\" = $?"
65
64
  end
65
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
66
+ span[:data][:activerecord][:sql] == sql
67
+ end
68
+ end
69
+
70
+ def test_postgresql_without_sanitize
71
+ skip unless ::Instana::Test.postgresql?
72
+
73
+ # Shut SQL sanitization off
74
+ ::Instana.config[:sanitize_sql] = false
75
+ # Pause so the thread can syncronize values
76
+ sleep 1
77
+
78
+ clear_all!
79
+
80
+ Net::HTTP.get(URI.parse('http://localhost:3205/test/db'))
81
+
82
+ spans = Instana.processor.queued_spans
83
+ assert_equal 6, spans.length
84
+ rack_span = find_first_span_by_name(spans, :rack)
85
+
86
+ ar_spans = find_spans_by_name(spans, :activerecord)
87
+ assert_equal 3, ar_spans.length
88
+
66
89
  ar_spans.each do |span|
67
- if span[:data][:activerecord][:sql] == sql
68
- found = true
69
- end
90
+ assert_equal "postgresql", span[:data][:activerecord][:adapter]
91
+ assert span[:data][:activerecord].key?(:host)
92
+ assert span[:data][:activerecord].key?(:username)
93
+ end
94
+
95
+ if ::Rails::VERSION::MAJOR < 4
96
+ sql = "INSERT INTO \"blocks\" (\"color\", \"created_at\", \"name\", \"updated_at\") VALUES ($1, $2, $3, $4) RETURNING \"id\""
97
+ else
98
+ sql = "INSERT INTO \"blocks\" (\"name\", \"color\", \"created_at\", \"updated_at\") VALUES ($1, $2, $3, $4) RETURNING \"id\""
70
99
  end
71
- assert found
100
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
101
+ span[:data][:activerecord][:sql] == sql
102
+ end
103
+ assert ar_span[:data][:activerecord].key?(:binds)
104
+ assert ar_span[:data][:activerecord][:binds].is_a?(Array)
105
+ assert_equal 4, ar_span[:data][:activerecord][:binds].length
106
+
107
+ if ::Rails::VERSION::MAJOR >= 5
108
+ sql = "SELECT \"blocks\".* FROM \"blocks\" WHERE \"blocks\".\"name\" = $1 ORDER BY \"blocks\".\"id\" ASC LIMIT $2"
109
+ elsif ::Rails::VERSION::MAJOR == 4
110
+ sql = "SELECT \"blocks\".* FROM \"blocks\" WHERE \"blocks\".\"name\" = $? ORDER BY \"blocks\".\"id\" ASC LIMIT ?"
111
+ else
112
+ sql = "SELECT \"blocks\".* FROM \"blocks\" WHERE \"blocks\".\"name\" = ? LIMIT ?"
113
+ end
114
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
115
+ span[:data][:activerecord][:sql] == sql
116
+ end
117
+ assert ar_span[:data][:activerecord].key?(:binds)
118
+ assert ar_span[:data][:activerecord][:binds].is_a?(Array)
119
+ assert_equal 2, ar_span[:data][:activerecord][:binds].length
120
+
121
+ if ::Rails::VERSION::MAJOR == 3
122
+ sql = "DELETE FROM \"blocks\" WHERE \"blocks\".\"id\" = 1"
123
+ else
124
+ sql = "DELETE FROM \"blocks\" WHERE \"blocks\".\"id\" = $1"
125
+ end
126
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
127
+ span[:data][:activerecord][:sql] == sql
128
+ end
129
+ assert ar_span[:data][:activerecord].key?(:binds)
130
+ assert ar_span[:data][:activerecord][:binds].is_a?(Array)
131
+ assert_equal 1, ar_span[:data][:activerecord][:binds].length
72
132
  end
73
133
 
74
134
  def test_postgresql_lock_table
@@ -93,21 +153,13 @@ class ActiveRecordTest < Minitest::Test
93
153
  assert_equal "postgres", ar_span[:data][:activerecord][:username]
94
154
  end
95
155
 
96
- found = false
97
- ar_spans.each do |span|
98
- if span[:data][:activerecord][:sql] == "LOCK blocks IN ACCESS EXCLUSIVE MODE"
99
- found = true
100
- end
156
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
157
+ span[:data][:activerecord][:sql] == "LOCK blocks IN ACCESS EXCLUSIVE MODE"
101
158
  end
102
- assert found
103
159
 
104
- found = false
105
- ar_spans.each do |span|
106
- if span[:data][:activerecord][:sql] == "SELECT ?"
107
- found = true
108
- end
160
+ ar_span = find_first_span_by_qualifier(ar_spans) do |span|
161
+ span[:data][:activerecord][:sql] == "SELECT ?"
109
162
  end
110
- assert found
111
163
  end
112
164
 
113
165
  def test_postgresql_raw_execute
@@ -40,5 +40,19 @@ if defined?(::Roda)
40
40
  assert first_span[:data][:http].key?(:host)
41
41
  assert_equal "example.org", first_span[:data][:http][:host]
42
42
  end
43
+
44
+ def test_path_template
45
+ clear_all!
46
+
47
+ r = get '/greet/instana'
48
+ assert last_response.ok?
49
+
50
+ spans = ::Instana.processor.queued_spans
51
+ assert_equal 1, spans.count
52
+
53
+ first_span = spans.first
54
+ assert_equal :rack, first_span[:n]
55
+ assert_equal '/greet/{name}', first_span[:data][:http][:path_tpl]
56
+ end
43
57
  end
44
58
  end
@@ -48,5 +48,19 @@ if defined?(::Sinatra)
48
48
  assert rack_span[:data][:http].key?(:host)
49
49
  assert_equal "example.org", rack_span[:data][:http][:host]
50
50
  end
51
+
52
+ def test_path_template
53
+ clear_all!
54
+
55
+ r = get '/greet/instana'
56
+ assert last_response.ok?
57
+
58
+ spans = ::Instana.processor.queued_spans
59
+ assert_equal 1, spans.count
60
+
61
+ first_span = spans.first
62
+ assert_equal :rack, first_span[:n]
63
+ assert_equal '/greet/:name', first_span[:data][:http][:path_tpl]
64
+ end
51
65
  end
52
66
  end
@@ -116,8 +116,6 @@ class ExconTest < Minitest::Test
116
116
  assert_equal 3, rack_spans.length
117
117
  assert_equal 3, excon_spans.length
118
118
 
119
- # ::Instana::Util.pry!
120
-
121
119
  for rack_span in rack_spans
122
120
  # data keys/values
123
121
  refute_nil rack_span.key?(:data)
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ class GraphqlTest < Minitest::Test
4
+ class TaskType < GraphQL::Schema::Object
5
+ field :id, ID, null: false
6
+ field :action, String, null: false
7
+ end
8
+
9
+ class NewTaskType < GraphQL::Schema::Mutation
10
+ argument :action, String, required: true
11
+ field :task, TaskType, null: true
12
+
13
+ def resolve(action:)
14
+ {
15
+ task: OpenStruct.new(id: '0', action: action)
16
+ }
17
+ end
18
+ end
19
+
20
+ class QueryType < GraphQL::Schema::Object
21
+ field :tasks, TaskType.connection_type, null: false
22
+
23
+ def tasks()
24
+ [
25
+ OpenStruct.new(id: '0', action: 'Sample')
26
+ ]
27
+ end
28
+ end
29
+
30
+ class MutationType < GraphQL::Schema::Object
31
+ field :create_task, mutation: NewTaskType
32
+ end
33
+
34
+ class Schema < GraphQL::Schema
35
+ query QueryType
36
+ mutation MutationType
37
+ end
38
+
39
+ def test_it_works
40
+ assert defined?(GraphQL)
41
+ end
42
+
43
+ def test_config_defaults
44
+ assert ::Instana.config[:graphql].is_a?(Hash)
45
+ assert ::Instana.config[:graphql].key?(:enabled)
46
+ assert_equal true, ::Instana.config[:graphql][:enabled]
47
+ end
48
+
49
+ def test_query
50
+ clear_all!
51
+
52
+ query = "query Sample {
53
+ tasks(after: \"\", first: 10) {
54
+ nodes {
55
+ action
56
+ }
57
+ }
58
+ }"
59
+
60
+ expected_data = {
61
+ :operationName => "Sample",
62
+ :operationType => "query",
63
+ :arguments => { "tasks" => ["after", "first"] },
64
+ :fields => { "tasks" => ["nodes"], "nodes" => ["action"] }
65
+ }
66
+ expected_results = {
67
+ "data" => {
68
+ "tasks" => {
69
+ "nodes" => [{ "action" => "Sample" }]
70
+ }
71
+ }
72
+ }
73
+
74
+ results = Instana.tracer.start_or_continue_trace('graphql-test') { Schema.execute(query) }
75
+ query_span, root_span = *Instana.processor.queued_spans
76
+
77
+ assert_equal expected_results, results.to_h
78
+ assert_equal :sdk, root_span[:n]
79
+ assert_equal :'graphql.server', query_span[:n]
80
+ assert_equal expected_data, query_span[:data][:graphql]
81
+ end
82
+
83
+ def test_mutation
84
+ clear_all!
85
+
86
+ query = "mutation Sample {
87
+ createTask(action: \"Sample\") {
88
+ task {
89
+ action
90
+ }
91
+ }
92
+ }"
93
+
94
+ expected_data = {
95
+ :operationName => "Sample",
96
+ :operationType => "mutation",
97
+ :arguments => { "createTask" => ["action"] },
98
+ :fields => { "createTask" => ["task"], "task" => ["action"] }
99
+ }
100
+ expected_results = {
101
+ "data" => {
102
+ "createTask" => {
103
+ "task" => { "action" => "Sample" }
104
+ }
105
+ }
106
+ }
107
+
108
+ results = Instana.tracer.start_or_continue_trace('graphql-test') { Schema.execute(query) }
109
+ query_span, root_span = *Instana.processor.queued_spans
110
+
111
+ assert_equal expected_results, results.to_h
112
+ assert_equal :sdk, root_span[:n]
113
+ assert_equal :'graphql.server', query_span[:n]
114
+ assert_equal expected_data, query_span[:data][:graphql]
115
+ end
116
+ end