instana 1.195.1 → 1.197.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +16 -2
- data/.rubocop.yml +6 -2
- data/Appraisals +13 -1
- data/Rakefile +1 -1
- data/docker-compose.yml +20 -0
- data/gemfiles/aws_30.gemfile +3 -0
- data/gemfiles/excon_021.gemfile +18 -0
- data/gemfiles/excon_079.gemfile +18 -0
- data/gemfiles/shoryuken_50.gemfile +19 -0
- data/instana.gemspec +3 -7
- data/lib/instana.rb +3 -0
- data/lib/instana/activator.rb +2 -0
- data/lib/instana/activators/aws_sdk_s3.rb +20 -0
- data/lib/instana/activators/aws_sdk_sns.rb +20 -0
- data/lib/instana/activators/aws_sdk_sqs.rb +20 -0
- data/lib/instana/activators/excon.rb +1 -1
- data/lib/instana/activators/shoryuken.rb +24 -0
- data/lib/instana/backend/agent.rb +60 -0
- data/lib/instana/backend/gc_snapshot.rb +41 -0
- data/lib/instana/backend/host_agent.rb +74 -0
- data/lib/instana/backend/host_agent_activation_observer.rb +97 -0
- data/lib/instana/backend/host_agent_lookup.rb +57 -0
- data/lib/instana/backend/host_agent_reporting_observer.rb +106 -0
- data/lib/instana/backend/process_info.rb +64 -0
- data/lib/instana/backend/request_client.rb +73 -0
- data/lib/instana/backend/serverless_agent.rb +118 -0
- data/lib/instana/base.rb +8 -27
- data/lib/instana/config.rb +8 -22
- data/lib/instana/instrumentation/aws_sdk_dynamodb.rb +21 -2
- data/lib/instana/instrumentation/aws_sdk_s3.rb +55 -0
- data/lib/instana/instrumentation/aws_sdk_sns.rb +29 -0
- data/lib/instana/instrumentation/aws_sdk_sqs.rb +98 -0
- data/lib/instana/instrumentation/excon.rb +19 -9
- data/lib/instana/instrumentation/instrumented_request.rb +68 -8
- data/lib/instana/instrumentation/net-http.rb +11 -7
- data/lib/instana/instrumentation/rack.rb +12 -7
- data/lib/instana/instrumentation/shoryuken.rb +44 -0
- data/lib/instana/logger_delegator.rb +31 -0
- data/lib/instana/{opentracing → open_tracing}/carrier.rb +0 -0
- data/lib/instana/open_tracing/instana_tracer.rb +99 -0
- data/lib/instana/secrets.rb +8 -4
- data/lib/instana/setup.rb +20 -11
- data/lib/instana/snapshot/deltable.rb +25 -0
- data/lib/instana/snapshot/docker_container.rb +151 -0
- data/lib/instana/snapshot/fargate_container.rb +88 -0
- data/lib/instana/snapshot/fargate_process.rb +67 -0
- data/lib/instana/snapshot/fargate_task.rb +72 -0
- data/lib/instana/snapshot/ruby_process.rb +48 -0
- data/lib/instana/tracer.rb +29 -147
- data/lib/instana/tracing/processor.rb +14 -22
- data/lib/instana/tracing/span.rb +34 -37
- data/lib/instana/tracing/span_context.rb +16 -10
- data/lib/instana/util.rb +8 -69
- data/lib/instana/version.rb +1 -1
- data/lib/opentracing.rb +26 -3
- data/test/backend/agent_test.rb +54 -0
- data/test/backend/gc_snapshot_test.rb +11 -0
- data/test/backend/host_agent_activation_observer_test.rb +72 -0
- data/test/backend/host_agent_lookup_test.rb +78 -0
- data/test/backend/host_agent_reporting_observer_test.rb +192 -0
- data/test/backend/host_agent_test.rb +47 -0
- data/test/backend/process_info_test.rb +63 -0
- data/test/backend/request_client_test.rb +39 -0
- data/test/backend/serverless_agent_test.rb +73 -0
- data/test/config_test.rb +10 -0
- data/test/instana_test.rb +11 -4
- data/test/instrumentation/aws_test.rb +130 -2
- data/test/instrumentation/excon_test.rb +16 -1
- data/test/instrumentation/net_http_test.rb +18 -0
- data/test/instrumentation/rack_instrumented_request_test.rb +9 -4
- data/test/instrumentation/rack_test.rb +2 -14
- data/test/instrumentation/shoryuken_test.rb +47 -0
- data/test/secrets_test.rb +41 -22
- data/test/snapshot/deltable_test.rb +17 -0
- data/test/snapshot/docker_container_test.rb +82 -0
- data/test/snapshot/fargate_container_test.rb +82 -0
- data/test/snapshot/fargate_process_test.rb +35 -0
- data/test/snapshot/fargate_task_test.rb +49 -0
- data/test/snapshot/ruby_process_test.rb +14 -0
- data/test/support/mock_timer.rb +20 -0
- data/test/test_helper.rb +16 -4
- data/test/tracing/custom_test.rb +1 -3
- data/test/tracing/id_management_test.rb +4 -0
- data/test/tracing/opentracing_test.rb +15 -2
- data/test/tracing/processor_test.rb +58 -0
- data/test/tracing/span_context_test.rb +21 -0
- data/test/tracing/span_test.rb +136 -0
- data/test/tracing/tracer_async_test.rb +29 -0
- data/test/tracing/tracer_test.rb +82 -16
- data/test/util_test.rb +10 -0
- metadata +86 -46
- data/Dockerfile +0 -16
- data/lib/instana/agent.rb +0 -508
- data/lib/instana/agent/helpers.rb +0 -87
- data/lib/instana/agent/hooks.rb +0 -44
- data/lib/instana/agent/tasks.rb +0 -51
- data/lib/instana/collector.rb +0 -119
- data/lib/instana/collectors/gc.rb +0 -60
- data/lib/instana/collectors/memory.rb +0 -37
- data/lib/instana/collectors/thread.rb +0 -33
- data/lib/instana/eum/eum-test.js.erb +0 -17
- data/lib/instana/eum/eum.js.erb +0 -17
- data/lib/instana/helpers.rb +0 -47
- data/lib/instana/opentracing/tracer.rb +0 -21
- data/lib/instana/thread_local.rb +0 -18
- data/lib/oj_check.rb +0 -19
- data/test/agent/agent_test.rb +0 -151
@@ -0,0 +1,41 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
require 'singleton'
|
5
|
+
|
6
|
+
module Instana
|
7
|
+
module Backend
|
8
|
+
# Keeps track of garbage collector related metrics
|
9
|
+
# @since 1.197.0
|
10
|
+
class GCSnapshot
|
11
|
+
include Singleton
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
::GC::Profiler.enable
|
15
|
+
|
16
|
+
@last_major_count = 0
|
17
|
+
@last_minor_count = 0
|
18
|
+
end
|
19
|
+
|
20
|
+
def report
|
21
|
+
stats = ::GC.stat
|
22
|
+
total_time = ::GC::Profiler.total_time * 1000
|
23
|
+
|
24
|
+
::GC::Profiler.clear
|
25
|
+
|
26
|
+
payload = {
|
27
|
+
totalTime: total_time,
|
28
|
+
heap_live: stats[:heap_live_slots] || stats[:heap_live_num],
|
29
|
+
heap_free: stats[:heap_free_slots] || stats[:heap_free_num],
|
30
|
+
minorGcs: stats[:minor_gc_count] - @last_minor_count,
|
31
|
+
majorGcs: stats[:major_gc_count] - @last_major_count
|
32
|
+
}
|
33
|
+
|
34
|
+
@last_major_count = stats[:major_gc_count]
|
35
|
+
@last_minor_count = stats[:minor_gc_count]
|
36
|
+
|
37
|
+
payload
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
module Instana
|
5
|
+
module Backend
|
6
|
+
# @since 1.197.0
|
7
|
+
class HostAgent
|
8
|
+
attr_reader :future
|
9
|
+
|
10
|
+
def initialize(discovery: Concurrent::Atom.new(nil), logger: ::Instana.logger)
|
11
|
+
@discovery = discovery
|
12
|
+
@logger = logger
|
13
|
+
@future = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def setup; end
|
17
|
+
|
18
|
+
def spawn_background_thread
|
19
|
+
return if ENV.key?('INSTANA_TEST')
|
20
|
+
|
21
|
+
@future = Concurrent::Promises.future do
|
22
|
+
client = until_not_nil { HostAgentLookup.new.call }
|
23
|
+
@discovery.delete_observers
|
24
|
+
@discovery
|
25
|
+
.with_observer(HostAgentActivationObserver.new(client, @discovery))
|
26
|
+
.with_observer(HostAgentReportingObserver.new(client, @discovery))
|
27
|
+
|
28
|
+
@discovery.swap { nil }
|
29
|
+
client
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Boolean] true if the agent able to send spans to the backend
|
34
|
+
def ready?
|
35
|
+
ENV.key?('INSTANA_TEST') || !@discovery.value.nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Hash, NilClass] the backend friendly description of the current in process collector
|
39
|
+
def source
|
40
|
+
{
|
41
|
+
e: discovery_value['pid'],
|
42
|
+
h: discovery_value['agentUuid']
|
43
|
+
}.compact
|
44
|
+
end
|
45
|
+
|
46
|
+
# @return [Array] extra headers to include in the trace
|
47
|
+
def extra_headers
|
48
|
+
discovery_value['extraHeaders']
|
49
|
+
end
|
50
|
+
|
51
|
+
# @return [Hash] values which are removed from urls sent to the backend
|
52
|
+
def secret_values
|
53
|
+
discovery_value['secrets']
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def until_not_nil
|
59
|
+
loop do
|
60
|
+
result = yield
|
61
|
+
return result unless result.nil?
|
62
|
+
|
63
|
+
@logger.debug("Waiting on a connection to the agent.")
|
64
|
+
sleep(1)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def discovery_value
|
69
|
+
v = @discovery.value
|
70
|
+
v || {}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
module Instana
|
5
|
+
module Backend
|
6
|
+
# Process which is responsible for initiating monitoring of a Ruby program with a local agent.
|
7
|
+
# @since 1.197.0
|
8
|
+
class HostAgentActivationObserver
|
9
|
+
DISCOVERY_URL = '/com.instana.plugin.ruby.discovery'.freeze
|
10
|
+
ENTITY_DATA_URL = '/com.instana.plugin.ruby.%i'.freeze
|
11
|
+
class DiscoveryError < StandardError; end
|
12
|
+
|
13
|
+
# @param [RequestClient] client used to make requests to the backend
|
14
|
+
# @param [Concurrent::Atom] discovery object used to store discovery response in
|
15
|
+
def initialize(client, discovery, wait_time: 1, logger: ::Instana.logger, max_wait_tries: 60, proc_table: Sys::ProcTable, socket_proc: default_socket_proc) # rubocop:disable Metrics/ParameterLists
|
16
|
+
@client = client
|
17
|
+
@discovery = discovery
|
18
|
+
@wait_time = wait_time
|
19
|
+
@logger = logger
|
20
|
+
@max_wait_tries = max_wait_tries
|
21
|
+
@proc_table = proc_table
|
22
|
+
@socket_proc = socket_proc
|
23
|
+
end
|
24
|
+
|
25
|
+
def update(_time, _old_version, new_version)
|
26
|
+
return unless new_version.nil?
|
27
|
+
|
28
|
+
socket = @socket_proc.call(@client)
|
29
|
+
|
30
|
+
try_forever_with_backoff do
|
31
|
+
payload = discovery_payload(socket)
|
32
|
+
discovery_response = @client.send_request('PUT', DISCOVERY_URL, payload)
|
33
|
+
|
34
|
+
raise DiscoveryError, "Discovery response was #{discovery_response.code} with `#{payload}`." unless discovery_response.ok?
|
35
|
+
|
36
|
+
discovery = discovery_response.json
|
37
|
+
@logger.debug("Discovery complete (`#{discovery}`). Waiting for agent.")
|
38
|
+
wait_for_backend(discovery['pid'])
|
39
|
+
@logger.debug("Agent ready.")
|
40
|
+
@discovery.swap { discovery }
|
41
|
+
end
|
42
|
+
|
43
|
+
socket.close
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def discovery_payload(socket)
|
49
|
+
proc_table = @proc_table.ps(pid: Process.pid)
|
50
|
+
process = ProcessInfo.new(proc_table)
|
51
|
+
|
52
|
+
payload = {
|
53
|
+
name: process.name,
|
54
|
+
args: process.arguments,
|
55
|
+
pid: process.parent_pid,
|
56
|
+
pidFromParentNS: process.from_parent_namespace,
|
57
|
+
cpuSetFileContent: process.cpuset
|
58
|
+
}
|
59
|
+
|
60
|
+
inode_path = "/proc/self/fd/#{socket.fileno}"
|
61
|
+
if socket.fileno && File.exist?(inode_path)
|
62
|
+
payload[:fd] = socket.fileno
|
63
|
+
payload[:inode] = File.readlink(inode_path)
|
64
|
+
end
|
65
|
+
|
66
|
+
payload.compact
|
67
|
+
end
|
68
|
+
|
69
|
+
def wait_for_backend(pid)
|
70
|
+
response = @max_wait_tries.times do
|
71
|
+
path = format(ENTITY_DATA_URL, pid)
|
72
|
+
wait_response = @client.send_request('HEAD', path)
|
73
|
+
|
74
|
+
break(wait_response) if wait_response.ok?
|
75
|
+
|
76
|
+
sleep(1)
|
77
|
+
end
|
78
|
+
|
79
|
+
raise DiscoveryError, "The backend didn't respond in time." unless response.is_a?(RequestClient::Response) && response.ok?
|
80
|
+
end
|
81
|
+
|
82
|
+
def try_forever_with_backoff
|
83
|
+
yield
|
84
|
+
rescue DiscoveryError, Net::OpenTimeout => e
|
85
|
+
@logger.warn(e)
|
86
|
+
sleep(@wait_time)
|
87
|
+
retry
|
88
|
+
rescue StandardError => e
|
89
|
+
@logger.error(%(#{e}\n#{e.backtrace.join("\n")}))
|
90
|
+
end
|
91
|
+
|
92
|
+
def default_socket_proc
|
93
|
+
->(c) { TCPSocket.new(c.host, c.port) }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
require 'csv'
|
5
|
+
|
6
|
+
module Instana
|
7
|
+
module Backend
|
8
|
+
# Utility class to discover the agent that a given instance of the collector
|
9
|
+
# needs to communicate with.
|
10
|
+
# @since 1.197.0
|
11
|
+
class HostAgentLookup
|
12
|
+
def initialize(host = ::Instana.config[:agent_host], port = ::Instana.config[:agent_port], destination: '00000000')
|
13
|
+
@host = host
|
14
|
+
@port = port
|
15
|
+
@destination = destination
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [RequestClient, NilClass] the request client to use to communicate with the agent or nil if no agent could be found
|
19
|
+
def call
|
20
|
+
host_listening?(@host, @port) || host_listening?(default_gateway, @port)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# @return [RequestClient, nil] the request client if it responds to '/' with a success
|
26
|
+
def host_listening?(host, port)
|
27
|
+
client = RequestClient.new(host, port)
|
28
|
+
client.send_request('GET', '/').ok? ? client : nil
|
29
|
+
rescue Net::OpenTimeout, Errno::ECONNREFUSED => _e
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [String] the default gateway to attempt to connect to or the @host if a default gateway can not be identified
|
34
|
+
def default_gateway
|
35
|
+
return @host unless File.exist?('/proc/self/net/route')
|
36
|
+
|
37
|
+
routes = CSV.read(
|
38
|
+
'/proc/self/net/route',
|
39
|
+
headers: :first_row,
|
40
|
+
col_sep: "\t",
|
41
|
+
header_converters: [->(v) { v.strip }],
|
42
|
+
converters: [->(v) { v.strip }]
|
43
|
+
)
|
44
|
+
|
45
|
+
route = routes.detect { |r| r['Destination'] == @destination }
|
46
|
+
return @host unless route
|
47
|
+
|
48
|
+
route['Gateway']
|
49
|
+
.split(/([0-9A-Z]{2})/)
|
50
|
+
.reject(&:empty?)
|
51
|
+
.reverse
|
52
|
+
.map { |s| s.to_i(16) }
|
53
|
+
.join('.')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
module Instana
|
5
|
+
module Backend
|
6
|
+
# Process which is responsible for reporting metrics and tracing to the local agent
|
7
|
+
# @since 1.197.0
|
8
|
+
class HostAgentReportingObserver
|
9
|
+
ENTITY_DATA_URL = '/com.instana.plugin.ruby.%i'.freeze
|
10
|
+
RESPONSE_DATA_URL = '/com.instana.plugin.ruby/response.%i?messageId=%s'.freeze
|
11
|
+
TRACES_DATA_URL = "/com.instana.plugin.ruby/traces.%i".freeze
|
12
|
+
|
13
|
+
attr_reader :report_timer
|
14
|
+
|
15
|
+
# @param [RequestClient] client used to make requests to the backend
|
16
|
+
# @param [Concurrent::Atom] discovery object used to store discovery response in
|
17
|
+
def initialize(client, discovery, logger: ::Instana.logger, timer_class: Concurrent::TimerTask, processor: ::Instana.processor)
|
18
|
+
@client = client
|
19
|
+
@discovery = discovery
|
20
|
+
@logger = logger
|
21
|
+
@report_timer = timer_class.new(execution_interval: 1, run_now: true) { report_to_backend }
|
22
|
+
@nonce = Time.now
|
23
|
+
@processor = processor
|
24
|
+
end
|
25
|
+
|
26
|
+
def update(time, _old_version, new_version)
|
27
|
+
return unless time > @nonce
|
28
|
+
|
29
|
+
@nonce = time
|
30
|
+
new_version.nil? ? @report_timer.shutdown : @report_timer.execute
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def report_to_backend
|
36
|
+
report_metrics if ::Instana.config[:metrics][:enabled]
|
37
|
+
report_traces if ::Instana.config[:tracing][:enabled]
|
38
|
+
rescue StandardError => e
|
39
|
+
@logger.error(%(#{e}\n#{e.backtrace.join("\n")}))
|
40
|
+
end
|
41
|
+
|
42
|
+
def report_traces
|
43
|
+
discovery = @discovery.value
|
44
|
+
return unless discovery
|
45
|
+
|
46
|
+
path = format(TRACES_DATA_URL, discovery['pid'])
|
47
|
+
|
48
|
+
@processor.send do |spans|
|
49
|
+
response = @client.send_request('POST', path, spans)
|
50
|
+
|
51
|
+
unless response.ok?
|
52
|
+
@discovery.swap { nil }
|
53
|
+
break
|
54
|
+
end
|
55
|
+
|
56
|
+
@logger.debug("Sent `#{spans.count}` spans to `#{path}` and got `#{response.code}`.")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def report_metrics
|
61
|
+
discovery = @discovery.value
|
62
|
+
return unless discovery
|
63
|
+
|
64
|
+
path = format(ENTITY_DATA_URL, discovery['pid'])
|
65
|
+
payload = metrics_payload(discovery).merge(Util.take_snapshot)
|
66
|
+
response = @client.send_request('POST', path, payload)
|
67
|
+
|
68
|
+
if response.ok?
|
69
|
+
handle_agent_tasks(response, discovery) unless response.body.empty?
|
70
|
+
else
|
71
|
+
@discovery.swap { nil }
|
72
|
+
end
|
73
|
+
|
74
|
+
@logger.debug("Sent `#{payload}` to `#{path}` and got `#{response.code}`.")
|
75
|
+
end
|
76
|
+
|
77
|
+
def handle_agent_tasks(response, discovery)
|
78
|
+
payload = response.json
|
79
|
+
payload = [payload] if payload.is_a?(Hash)
|
80
|
+
payload
|
81
|
+
.select { |t| t['action'] == 'ruby.source' }
|
82
|
+
.each do |action|
|
83
|
+
payload = ::Instana::Util.get_rb_source(action['args']['file'])
|
84
|
+
path = format(RESPONSE_DATA_URL, discovery['pid'], action['messageId'])
|
85
|
+
@client.send_request('POST', path, payload)
|
86
|
+
end
|
87
|
+
rescue StandardError => e
|
88
|
+
@logger.debug("Error processing agent task #{e.inspect}")
|
89
|
+
end
|
90
|
+
|
91
|
+
def metrics_payload(discovery)
|
92
|
+
proc_table = Sys::ProcTable.ps(pid: Process.pid)
|
93
|
+
process = ProcessInfo.new(proc_table)
|
94
|
+
|
95
|
+
{
|
96
|
+
pid: discovery['pid'],
|
97
|
+
name: Util.get_app_name,
|
98
|
+
exec_args: process.arguments,
|
99
|
+
gc: GCSnapshot.instance.report,
|
100
|
+
thread: {count: ::Thread.list.count},
|
101
|
+
memory: {rss_size: proc_table.rss / 1024} # Bytes to Kilobytes
|
102
|
+
}
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
module Instana
|
5
|
+
module Backend
|
6
|
+
# Wrapper around {Sys::ProcTable} that adds support for reading the /proc
|
7
|
+
# file system for extra information around containers
|
8
|
+
# @since 1.197.0
|
9
|
+
class ProcessInfo < SimpleDelegator
|
10
|
+
def name
|
11
|
+
cmdline
|
12
|
+
.split(' ').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def arguments
|
16
|
+
_, *arguments = cmdline.split(' ')
|
17
|
+
clean_arguments(arguments)
|
18
|
+
end
|
19
|
+
|
20
|
+
def parent_pid
|
21
|
+
if in_container? && sched_pid != pid
|
22
|
+
sched_pid
|
23
|
+
else
|
24
|
+
pid
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def from_parent_namespace
|
29
|
+
!in_container? || in_container? && sched_pid != pid
|
30
|
+
end
|
31
|
+
|
32
|
+
def cpuset
|
33
|
+
path = "/proc/#{pid}/cpuset"
|
34
|
+
return unless File.exist?(path)
|
35
|
+
|
36
|
+
File.read(path).strip
|
37
|
+
end
|
38
|
+
|
39
|
+
def in_container?
|
40
|
+
!cpuset.nil? && cpuset != '/'
|
41
|
+
end
|
42
|
+
|
43
|
+
def sched_pid
|
44
|
+
path = '/proc/self/sched'
|
45
|
+
return unless File.exist?(path)
|
46
|
+
|
47
|
+
File.read(path).match(/\d+/).to_s.to_i
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def clean_arguments(arguments)
|
53
|
+
return arguments unless RbConfig::CONFIG['host_os'].include?('darwin')
|
54
|
+
|
55
|
+
arguments.reject do |a|
|
56
|
+
if a.include?('=')
|
57
|
+
k, = a.split('=', 2)
|
58
|
+
ENV[k]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# (c) Copyright IBM Corp. 2021
|
2
|
+
# (c) Copyright Instana Inc. 2021
|
3
|
+
|
4
|
+
require 'net/http'
|
5
|
+
require 'delegate'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
# :nocov:
|
9
|
+
begin
|
10
|
+
require 'oj'
|
11
|
+
rescue LoadError => _e
|
12
|
+
Instana.logger.warn("Unable to load Oj.")
|
13
|
+
end
|
14
|
+
# :nocov:
|
15
|
+
|
16
|
+
module Instana
|
17
|
+
module Backend
|
18
|
+
# Convince wrapper around {Net::HTTP}.
|
19
|
+
# @since 1.197.0
|
20
|
+
class RequestClient
|
21
|
+
attr_reader :host, :port
|
22
|
+
|
23
|
+
class Response < SimpleDelegator
|
24
|
+
# @return [Hash] the decoded json response
|
25
|
+
def json
|
26
|
+
JSON.parse(body)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Boolean] true if the request was successful
|
30
|
+
def ok?
|
31
|
+
__getobj__.is_a?(Net::HTTPSuccess)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def initialize(host, port, use_ssl: false)
|
36
|
+
@host = host
|
37
|
+
@port = port
|
38
|
+
@client = Net::HTTP.start(host, port, use_ssl: use_ssl)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Send a request to the backend. If data is a {Hash},
|
42
|
+
# encode the object as JSON and set the proper headers.
|
43
|
+
#
|
44
|
+
# @param [String] method request method
|
45
|
+
# @param [String] path request path
|
46
|
+
# @param [Hash, String] data request body
|
47
|
+
# @param [Hash] headers extra request headers to send
|
48
|
+
def send_request(method, path, data = nil, headers = {})
|
49
|
+
body = if data.is_a?(Hash) || data.is_a?(Array)
|
50
|
+
headers['Content-Type'] = 'application/json'
|
51
|
+
headers['Accept'] = 'application/json'
|
52
|
+
|
53
|
+
encode_body(data)
|
54
|
+
else
|
55
|
+
headers['Content-Type'] = 'application/octet-stream'
|
56
|
+
|
57
|
+
data
|
58
|
+
end
|
59
|
+
|
60
|
+
response = @client.send_request(method, path, body, headers)
|
61
|
+
Response.new(response)
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def encode_body(data)
|
67
|
+
# :nocov:
|
68
|
+
defined?(Oj) ? Oj.dump(data, mode: :strict) : JSON.dump(data)
|
69
|
+
# :nocov:
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|