instana 1.197.0.pre1 → 1.199.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/instana/backend/agent.rb +9 -1
  3. data/lib/instana/backend/host_agent.rb +27 -10
  4. data/lib/instana/backend/host_agent_activation_observer.rb +17 -7
  5. data/lib/instana/backend/request_client.rb +6 -16
  6. data/lib/instana/backend/serverless_agent.rb +13 -18
  7. data/lib/instana/base.rb +2 -0
  8. data/lib/instana/config.rb +1 -1
  9. data/lib/instana/instrumentation/excon.rb +7 -5
  10. data/lib/instana/instrumentation/instrumented_request.rb +62 -7
  11. data/lib/instana/instrumentation/net-http.rb +7 -5
  12. data/lib/instana/instrumentation/rack.rb +12 -7
  13. data/lib/instana/serverless.rb +139 -0
  14. data/lib/instana/setup.rb +5 -0
  15. data/lib/instana/snapshot/google_cloud_run_instance.rb +69 -0
  16. data/lib/instana/snapshot/google_cloud_run_process.rb +58 -0
  17. data/lib/instana/snapshot/lambda_function.rb +39 -0
  18. data/lib/instana/tracing/processor.rb +11 -1
  19. data/lib/instana/tracing/span.rb +10 -4
  20. data/lib/instana/tracing/span_context.rb +14 -9
  21. data/lib/instana/util.rb +4 -2
  22. data/lib/instana/version.rb +1 -1
  23. data/test/backend/agent_test.rb +26 -0
  24. data/test/backend/host_agent_activation_observer_test.rb +16 -9
  25. data/test/backend/host_agent_test.rb +17 -2
  26. data/test/backend/request_client_test.rb +0 -22
  27. data/test/instrumentation/rack_instrumented_request_test.rb +2 -0
  28. data/test/serverless_test.rb +323 -0
  29. data/test/snapshot/google_cloud_run_instance_test.rb +74 -0
  30. data/test/snapshot/google_cloud_run_process_test.rb +33 -0
  31. data/test/snapshot/lambda_function_test.rb +37 -0
  32. data/test/test_helper.rb +1 -1
  33. data/test/tracing/id_management_test.rb +4 -0
  34. data/test/tracing/span_context_test.rb +3 -3
  35. data/test/tracing/span_test.rb +9 -0
  36. metadata +16 -4
data/lib/instana/util.rb CHANGED
@@ -156,8 +156,10 @@ module Instana
156
156
  #
157
157
  # @return [String]
158
158
  #
159
- def header_to_id(header_id)
160
- 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
161
163
  end
162
164
  end
163
165
  end
@@ -2,6 +2,6 @@
2
2
  # (c) Copyright Instana Inc. 2016
3
3
 
4
4
  module Instana
5
- VERSION = "1.197.0.pre1"
5
+ VERSION = "1.199.0"
6
6
  VERSION_FULL = "instana-#{VERSION}"
7
7
  end
@@ -43,6 +43,32 @@ class AgentTest < Minitest::Test
43
43
  ENV['ECS_CONTAINER_METADATA_URI'] = nil
44
44
  end
45
45
 
46
+ def test_lambda
47
+ ENV['_HANDLER'] = 'TEST_FUNCTION'
48
+ ENV['INSTANA_ENDPOINT_URL'] = 'http://example.com'
49
+
50
+ subject = Instana::Backend::Agent.new
51
+ assert_nil subject.delegate
52
+ subject.setup
53
+ assert subject.delegate.is_a?(Instana::Backend::ServerlessAgent)
54
+ ensure
55
+ ENV['_HANDLER'] = nil
56
+ ENV['INSTANA_ENDPOINT_URL'] = nil
57
+ end
58
+
59
+ def test_google_cloud
60
+ ENV['K_REVISION'] = 'TEST'
61
+ ENV['INSTANA_ENDPOINT_URL'] = 'http://example.com'
62
+
63
+ subject = Instana::Backend::Agent.new
64
+ assert_nil subject.delegate
65
+ subject.setup
66
+ assert subject.delegate.is_a?(Instana::Backend::ServerlessAgent)
67
+ ensure
68
+ ENV['K_REVISION'] = nil
69
+ ENV['INSTANA_ENDPOINT_URL'] = nil
70
+ end
71
+
46
72
  def test_delegate_super
47
73
  subject = Instana::Backend::Agent.new
48
74
  assert_raises NoMethodError do
@@ -4,6 +4,10 @@
4
4
  require 'test_helper'
5
5
 
6
6
  class HostAgentActivationObserverTest < Minitest::Test
7
+ def setup
8
+ @socket_proc = ->(_c) { OpenStruct.new(fileno: 0) }
9
+ end
10
+
7
11
  def test_standard_discovery
8
12
  stub_request(:put, "http://10.10.10.10:9292/com.instana.plugin.ruby.discovery")
9
13
  .and_timeout
@@ -17,7 +21,7 @@ class HostAgentActivationObserverTest < Minitest::Test
17
21
  client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
18
22
  discovery = Concurrent::Atom.new(nil)
19
23
 
20
- subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), max_wait_tries: 1)
24
+ subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), max_wait_tries: 1, socket_proc: @socket_proc)
21
25
 
22
26
  subject.update(nil, nil, nil)
23
27
  assert_equal({'pid' => 1234}, discovery.value)
@@ -31,15 +35,18 @@ class HostAgentActivationObserverTest < Minitest::Test
31
35
  .and_return(status: 200, body: '{"ok": true}')
32
36
 
33
37
  client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
34
- # This is the cleanest way to fake it so it works across all test environments
35
- client.define_singleton_method(:fileno) { '0' }
36
- client.define_singleton_method(:inode) { '0' }
37
-
38
38
  discovery = Concurrent::Atom.new(nil)
39
39
 
40
- subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), max_wait_tries: 1)
40
+ subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), max_wait_tries: 1, socket_proc: @socket_proc)
41
41
 
42
- subject.update(nil, nil, nil)
42
+ FakeFS.with_fresh do
43
+ FakeFS::FileSystem.clone('test/support/proc', '/proc')
44
+ FakeFS::FileSystem.clone('test/support/proc/0', "/proc/#{Process.pid}")
45
+ Dir.mkdir('/proc/self/fd')
46
+ File.symlink('/proc/self/sched', "/proc/self/fd/0")
47
+
48
+ subject.update(nil, nil, nil)
49
+ end
43
50
 
44
51
  assert_equal({'pid' => 1234}, discovery.value)
45
52
  end
@@ -48,7 +55,7 @@ class HostAgentActivationObserverTest < Minitest::Test
48
55
  client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
49
56
  discovery = Concurrent::Atom.new(nil)
50
57
 
51
- subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), proc_table: nil)
58
+ subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, wait_time: 0, logger: Logger.new('/dev/null'), proc_table: nil, socket_proc: @socket_proc)
52
59
 
53
60
  subject.update(nil, nil, nil)
54
61
  assert_nil discovery.value
@@ -58,7 +65,7 @@ class HostAgentActivationObserverTest < Minitest::Test
58
65
  client = Instana::Backend::RequestClient.new('10.10.10.10', 9292)
59
66
  discovery = Concurrent::Atom.new(nil)
60
67
 
61
- subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery)
68
+ subject = Instana::Backend::HostAgentActivationObserver.new(client, discovery, socket_proc: @socket_proc)
62
69
  assert_nil subject.update(nil, nil, true)
63
70
  assert_nil discovery.value
64
71
  end
@@ -4,19 +4,34 @@
4
4
  require 'test_helper'
5
5
 
6
6
  class HostAgentTest < Minitest::Test
7
- def test_setup
7
+ def test_spawn_background_thread
8
8
  ENV['INSTANA_TEST'] = nil
9
9
  ::Instana.config[:agent_host] = '10.10.10.10'
10
10
 
11
+ if File.exist?('/sbin/ip')
12
+ addr = `/sbin/ip route | awk '/default/ { print $3 }'`.strip
13
+ stub_request(:get, "http://#{addr}:42699/")
14
+ .to_timeout
15
+ end
16
+
11
17
  stub_request(:get, "http://10.10.10.10:42699/")
18
+ .to_timeout.times(3).then
12
19
  .to_return(status: 200, body: "", headers: {})
13
20
 
14
21
  discovery = Minitest::Mock.new
22
+ discovery.expect(:delete_observers, discovery, [])
15
23
  discovery.expect(:with_observer, discovery, [Instana::Backend::HostAgentActivationObserver])
16
24
  discovery.expect(:with_observer, discovery, [Instana::Backend::HostAgentReportingObserver])
25
+ discovery.expect(:swap, discovery, [])
17
26
 
18
27
  subject = Instana::Backend::HostAgent.new(discovery: discovery)
19
- subject.setup
28
+
29
+ FakeFS.with_fresh do
30
+ FakeFS::FileSystem.clone('test/support/ecs', '/proc')
31
+ subject.spawn_background_thread
32
+ end
33
+
34
+ subject.future.value!
20
35
 
21
36
  discovery.verify
22
37
  ensure
@@ -36,26 +36,4 @@ class RequestClientTest < Minitest::Test
36
36
 
37
37
  refute response.ok?
38
38
  end
39
-
40
- def test_fileno
41
- subject = Instana::Backend::RequestClient.new('example.com', 9292)
42
- assert_nil subject.fileno
43
- end
44
-
45
- def test_inode
46
- subject = Instana::Backend::RequestClient.new('example.com', 9292)
47
- subject.define_singleton_method(:fileno) { '0' } # This is the cleanest way to fake it so it works across all test environments
48
- FakeFS.with_fresh do
49
- FakeFS::FileSystem.clone('test/support/proc', '/proc')
50
- Dir.mkdir('/proc/self/fd')
51
- File.symlink('/proc/self/sched', '/proc/self/fd/0')
52
-
53
- assert_equal '/proc/self/sched', subject.inode
54
- end
55
- end
56
-
57
- def test_inode_no_proc
58
- subject = Instana::Backend::RequestClient.new('example.com', 9292)
59
- assert_nil subject.inode
60
- end
61
39
  end
@@ -29,6 +29,7 @@ class RackInstrumentedRequestTest < Minitest::Test
29
29
  expected = {
30
30
  trace_id: id,
31
31
  span_id: id,
32
+ from_w3: false,
32
33
  level: '1'
33
34
  }
34
35
 
@@ -47,6 +48,7 @@ class RackInstrumentedRequestTest < Minitest::Test
47
48
  external_state: nil,
48
49
  trace_id: 'a3ce929d0e0e4736',
49
50
  span_id: '00f067aa0ba902b7',
51
+ from_w3: true,
50
52
  level: '1'
51
53
  }
52
54
 
@@ -0,0 +1,323 @@
1
+ # (c) Copyright IBM Corp. 2021
2
+ # (c) Copyright Instana Inc. 2021
3
+
4
+ require 'test_helper'
5
+
6
+ class ServerlessTest < Minitest::Test
7
+ def setup
8
+ @mock_agent = Minitest::Mock.new
9
+ @mock_agent.expect(:send_bundle, true, [])
10
+ @subject = Instana::Serverless.new(agent: @mock_agent)
11
+ end
12
+
13
+ def teardown
14
+ @mock_agent.verify
15
+ end
16
+
17
+ def test_lambda_send_error
18
+ mock_logger = Minitest::Mock.new
19
+ mock_logger.expect(:error, true, [String])
20
+
21
+ @mock_agent.expect(:send_bundle, true) { |_args| raise StandardError, 'error' }
22
+
23
+ mock_context = OpenStruct.new(
24
+ invoked_function_arn: 'test_arn',
25
+ function_name: 'test_function',
26
+ function_version: '$TEST'
27
+ )
28
+
29
+ subject = Instana::Serverless.new(agent: @mock_agent, logger: mock_logger)
30
+ subject.wrap_aws(nil, mock_context) { 0 }
31
+ subject.wrap_aws(nil, mock_context) { 0 }
32
+
33
+ mock_logger.verify
34
+ end
35
+
36
+ def test_lambda_data
37
+ clear_all!
38
+
39
+ mock_context = OpenStruct.new(
40
+ invoked_function_arn: 'test_arn',
41
+ function_name: 'test_function',
42
+ function_version: '$TEST'
43
+ )
44
+
45
+ @subject.wrap_aws(nil, mock_context) { 0 }
46
+
47
+ lambda_span, *rest = Instana.processor.queued_spans
48
+ assert rest.empty?
49
+
50
+ data = lambda_span[:data][:lambda]
51
+
52
+ assert_equal 'aws:api.gateway.noproxy', lambda_span[:data][:lambda][:trigger]
53
+
54
+ assert_equal mock_context.invoked_function_arn, data[:arn]
55
+ assert_equal mock_context.function_name, data[:functionName]
56
+ assert_equal mock_context.function_version, data[:functionVersion]
57
+ assert_equal 'ruby', data[:runtime]
58
+ end
59
+
60
+ def test_lambda_http
61
+ clear_all!
62
+
63
+ mock_id = Instana::Util.generate_id
64
+ mock_context = OpenStruct.new(
65
+ invoked_function_arn: 'test_arn',
66
+ function_name: 'test_function',
67
+ function_version: '$TEST'
68
+ )
69
+ mock_http = {
70
+ "headers" => {
71
+ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
72
+ "Accept-Encoding" => "gzip, deflate",
73
+ "Accept-Language" => "en-US,en;q=0.5",
74
+ "Connection" => "keep-alive",
75
+ "Host" => "127.0.0.1:3000",
76
+ "Upgrade-Insecure-Requests" => "1",
77
+ "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0",
78
+ "X-Forwarded-Port" => "3000",
79
+ "X-Forwarded-Proto" => "http",
80
+ 'X-Instana-S' => mock_id,
81
+ 'X-Instana-T' => mock_id,
82
+ 'X-Instana-L' => '1'
83
+ },
84
+ "httpMethod" => "GET",
85
+ "path" => "/hello",
86
+ "queryStringParameters" => {"test" => "abcde"}
87
+ }
88
+
89
+ @subject.wrap_aws(mock_http, mock_context) { 0 }
90
+
91
+ lambda_span, *rest = Instana.processor.queued_spans
92
+ assert rest.empty?
93
+
94
+ data = lambda_span[:data][:http]
95
+
96
+ assert_equal 'aws:api.gateway', lambda_span[:data][:lambda][:trigger]
97
+ assert_equal mock_id, lambda_span[:t]
98
+ assert_equal mock_id, lambda_span[:p]
99
+
100
+ assert_equal 'GET', data[:method]
101
+ assert_equal '/hello', data[:url]
102
+ assert_equal '127.0.0.1:3000', data[:host]
103
+ assert_equal 'test=abcde', data[:params]
104
+ end
105
+
106
+ def test_lambda_alb
107
+ clear_all!
108
+
109
+ mock_id = Instana::Util.generate_id
110
+ mock_context = OpenStruct.new(
111
+ invoked_function_arn: 'test_arn',
112
+ function_name: 'test_function',
113
+ function_version: '$TEST'
114
+ )
115
+ mock_http = {
116
+ "headers" => {
117
+ "Accept" => "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
118
+ "Accept-Encoding" => "gzip, deflate",
119
+ "Accept-Language" => "en-US,en;q=0.5",
120
+ "Connection" => "keep-alive",
121
+ "Host" => "127.0.0.1:3000",
122
+ "Upgrade-Insecure-Requests" => "1",
123
+ "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:87.0) Gecko/20100101 Firefox/87.0",
124
+ "X-Forwarded-Port" => "3000",
125
+ "X-Forwarded-Proto" => "http",
126
+ 'X-Instana-S' => mock_id,
127
+ 'X-Instana-T' => mock_id,
128
+ 'X-Instana-L' => '1'
129
+ },
130
+ "httpMethod" => "GET",
131
+ "path" => "/hello",
132
+ "requestContext" => { "elb" => {} }
133
+ }
134
+
135
+ @subject.wrap_aws(mock_http, mock_context) { 0 }
136
+
137
+ lambda_span, *rest = Instana.processor.queued_spans
138
+ assert rest.empty?
139
+
140
+ data = lambda_span[:data][:http]
141
+
142
+ assert_equal 'aws:application.load.balancer', lambda_span[:data][:lambda][:trigger]
143
+ assert_equal mock_id, lambda_span[:t]
144
+ assert_equal mock_id, lambda_span[:p]
145
+
146
+ assert_equal 'GET', data[:method]
147
+ assert_equal '/hello', data[:url]
148
+ assert_equal '127.0.0.1:3000', data[:host]
149
+ assert_equal '', data[:params]
150
+ end
151
+
152
+ def test_lambda_cw_event
153
+ clear_all!
154
+
155
+ mock_context = OpenStruct.new(
156
+ invoked_function_arn: 'test_arn',
157
+ function_name: 'test_function',
158
+ function_version: '$TEST'
159
+ )
160
+ mock_event = {
161
+ "detail-type" => "Scheduled Event",
162
+ "source" => "aws.events",
163
+ "id" => "test",
164
+ "resources" => ["test"]
165
+ }
166
+
167
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
168
+
169
+ lambda_span, *rest = Instana.processor.queued_spans
170
+ assert rest.empty?
171
+
172
+ data = lambda_span[:data][:lambda][:cw][:events]
173
+
174
+ assert_equal 'aws:cloudwatch.events', lambda_span[:data][:lambda][:trigger]
175
+ assert_equal 'test', data[:id]
176
+ assert_equal ["test"], data[:resources]
177
+ end
178
+
179
+ def test_lambda_cw_logs
180
+ clear_all!
181
+
182
+ mock_context = OpenStruct.new(
183
+ invoked_function_arn: 'test_arn',
184
+ function_name: 'test_function',
185
+ function_version: '$TEST'
186
+ )
187
+ mock_event = {
188
+ "awslogs" => {"data" => File.read('test/support/serverless/cloudwatch_log.bin')}
189
+ }
190
+
191
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
192
+
193
+ lambda_span, *rest = Instana.processor.queued_spans
194
+ assert rest.empty?
195
+
196
+ data = lambda_span[:data][:lambda][:cw][:logs]
197
+
198
+ assert_equal 'aws:cloudwatch.logs', lambda_span[:data][:lambda][:trigger]
199
+ assert_equal '/aws/lambda/echo-nodejs', data[:group]
200
+ assert_equal '2019/03/13/[$LATEST]94fa867e5374431291a7fc14e2f56ae7', data[:stream]
201
+ end
202
+
203
+ def test_lambda_cw_error
204
+ clear_all!
205
+
206
+ mock_context = OpenStruct.new(
207
+ invoked_function_arn: 'test_arn',
208
+ function_name: 'test_function',
209
+ function_version: '$TEST'
210
+ )
211
+ mock_event = {
212
+ "awslogs" => {"data" => "error"}
213
+ }
214
+
215
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
216
+
217
+ lambda_span, *rest = Instana.processor.queued_spans
218
+ assert rest.empty?
219
+
220
+ data = lambda_span[:data][:lambda][:cw][:logs]
221
+
222
+ assert_equal 'aws:cloudwatch.logs', lambda_span[:data][:lambda][:trigger]
223
+ assert_equal 'incorrect header check', data[:decodingError]
224
+ end
225
+
226
+ def test_lambda_s3
227
+ clear_all!
228
+
229
+ mock_context = OpenStruct.new(
230
+ invoked_function_arn: 'test_arn',
231
+ function_name: 'test_function',
232
+ function_version: '$TEST'
233
+ )
234
+ mock_event = {
235
+ "Records" => [
236
+ {
237
+ "source" => "aws:s3",
238
+ "eventName" => "test",
239
+ "s3" => {
240
+ "bucket" => {"name" => "test_bucket"},
241
+ "object" => {"key" => "test_key"}
242
+ }
243
+ }
244
+ ]
245
+ }
246
+
247
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
248
+
249
+ lambda_span, *rest = Instana.processor.queued_spans
250
+ assert rest.empty?
251
+
252
+ data = lambda_span[:data][:lambda][:s3]
253
+
254
+ assert_equal 'aws:s3', lambda_span[:data][:lambda][:trigger]
255
+ assert_equal 1, data[:events].length
256
+
257
+ assert_equal 'test', data[:events].first[:name]
258
+ assert_equal 'test_bucket', data[:events].first[:bucket]
259
+ assert_equal 'test_key', data[:events].first[:object]
260
+ end
261
+
262
+ def test_lambda_s3_no_object
263
+ clear_all!
264
+
265
+ mock_context = OpenStruct.new(
266
+ invoked_function_arn: 'test_arn',
267
+ function_name: 'test_function',
268
+ function_version: '$TEST'
269
+ )
270
+ mock_event = {
271
+ "Records" => [
272
+ {
273
+ "source" => "aws:s3",
274
+ "eventName" => "test"
275
+ }
276
+ ]
277
+ }
278
+
279
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
280
+
281
+ lambda_span, *rest = Instana.processor.queued_spans
282
+ assert rest.empty?
283
+
284
+ data = lambda_span[:data][:lambda][:s3]
285
+
286
+ assert_equal 'aws:s3', lambda_span[:data][:lambda][:trigger]
287
+ assert_equal 1, data[:events].length
288
+
289
+ assert_equal 'test', data[:events].first[:name]
290
+ assert_nil data[:events].first[:bucket]
291
+ assert_nil data[:events].first[:object]
292
+ end
293
+
294
+ def test_lambda_sqs
295
+ clear_all!
296
+
297
+ mock_context = OpenStruct.new(
298
+ invoked_function_arn: 'test_arn',
299
+ function_name: 'test_function',
300
+ function_version: '$TEST'
301
+ )
302
+ mock_event = {
303
+ "Records" => [
304
+ {
305
+ "source" => "aws:sqs",
306
+ "eventSourceARN" => "test_arn"
307
+ }
308
+ ]
309
+ }
310
+
311
+ @subject.wrap_aws(mock_event, mock_context) { 0 }
312
+
313
+ lambda_span, *rest = Instana.processor.queued_spans
314
+ assert rest.empty?
315
+
316
+ data = lambda_span[:data][:lambda][:sqs]
317
+
318
+ assert_equal 'aws:sqs', lambda_span[:data][:lambda][:trigger]
319
+ assert_equal 1, data[:messages].length
320
+
321
+ assert_equal 'test_arn', data[:messages].first[:queue]
322
+ end
323
+ end