Skip to content

Commit b015a66

Browse files
authored
Merge pull request #37 from instana/some_refactoring_and_cleanup
Refactor and cleanup
2 parents 0a0461f + 9354c07 commit b015a66

File tree

7 files changed

+55
-53
lines changed

7 files changed

+55
-53
lines changed

lib/instana/agent.rb

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ def initialize
2222
# Supported two states (unannounced & announced)
2323
@state = :unannounced
2424

25-
# Store the pid from process boot so we can detect forks
26-
@pid = Process.pid
27-
2825
# Snapshot data is collected once per process but resent
2926
# every 10 minutes along side process metrics.
3027
@snapshot = ::Instana::Util.take_snapshot
@@ -57,29 +54,7 @@ def initialize
5754
# The agent UUID returned from the host agent
5855
@agent_uuid = nil
5956

60-
collect_process_info
61-
end
62-
63-
# Used in class initialization and after a fork, this method
64-
# collects up process information and stores it in @process
65-
#
66-
def collect_process_info
67-
@process = {}
68-
cmdline = ProcTable.ps(Process.pid).cmdline.split("\0")
69-
@process[:name] = cmdline.shift
70-
@process[:arguments] = cmdline
71-
72-
if @is_osx
73-
# Handle OSX bug where env vars show up at the end of process name
74-
# such as MANPATH etc..
75-
@process[:name].gsub!(/[_A-Z]+=\S+/, '')
76-
@process[:name].rstrip!
77-
end
78-
79-
@process[:original_pid] = @pid
80-
# This is usually Process.pid but in the case of docker, the host agent
81-
# will return to us the true host pid in which we use to report data.
82-
@process[:report_pid] = nil
57+
@process = ::Instana::Util.collect_process_info
8358
end
8459

8560
# Used post fork to re-initialize state and restart communications with
@@ -89,8 +64,7 @@ def after_fork
8964
::Instana.logger.agent "after_fork hook called. Falling back to unannounced state and spawning a new background agent thread."
9065

9166
# Re-collect process information post fork
92-
@pid = Process.pid
93-
collect_process_info
67+
@process ::Instana::Util.collect_process_info
9468

9569
transition_to(:unannounced)
9670
setup
@@ -451,7 +425,7 @@ def get_real_pid
451425
# @ return [Boolean] true or false to indicate if forked
452426
#
453427
def forked?
454-
@pid != Process.pid
428+
@process[:pid] != Process.pid
455429
end
456430
end
457431
end

lib/instana/base.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@ class << self
2020
#
2121
def setup
2222
@logger = ::Instana::XLogger.new(STDOUT)
23-
if ENV.key?('INSTANA_GEM_TEST') || ENV.key?('INSTANA_GEM_DEV')
24-
@logger.level = Logger::DEBUG
25-
else
26-
@logger.level = Logger::WARN
27-
end
2823
@logger.unknown "Stan is on the scene. Starting Instana instrumentation."
2924

3025
@agent = ::Instana::Agent.new
3126
@tracer = ::Instana::Tracer.new
3227
@processor = ::Instana::Processor.new
3328
@collectors = []
29+
end
3430

35-
# Store the current pid so we can detect a potential fork
36-
# later on
37-
@pid = ::Process.pid
31+
# Indicates whether we are running in a development environment.
32+
#
33+
# @return Boolean
34+
#
35+
def debug?
36+
ENV.key?('INSTANA_GEM_DEV')
3837
end
3938

40-
def pid_change?
41-
@pid != ::Process.pid
39+
# Indicates whether we are running in the test environment.
40+
#
41+
# @return Boolean
42+
#
43+
def test?
44+
ENV.key?('INSTANA_GEM_TEST')
4245
end
4346
end
4447
end

lib/instana/collectors.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module Instana
77
module Collector
88
class << self
99
attr_accessor :interval
10-
attr_accessor :snapshot
1110

1211
##
1312
# collect_and_report

lib/instana/collectors/gc.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ def collect
2828
# GC metrics only available on newer Ruby versions
2929
if RUBY_VERSION >= '2.1'
3030
# GC runs. Calculate how many have occurred since the last call
31-
@this_gc[:minorGcs] = stats[:minor_gc_count] - @last_minor_count
32-
@this_gc[:majorGcs] = stats[:major_gc_count] - @last_major_count
31+
@this_gc[:minorGcs] = stats[:minor_gc_count] - @last_minor_count
32+
@this_gc[:majorGcs] = stats[:major_gc_count] - @last_major_count
3333

3434
# Store these counts so that we have something to compare to next
3535
# time around.

lib/instana/instrumentation/net-http.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@ def request_with_instana(*args, &block)
1111

1212
# Send out the tracing context with the request
1313
request = args[0]
14-
our_trace_id = ::Instana.tracer.trace_id
15-
our_span_id = ::Instana.tracer.span_id
1614

1715
# Set request headers; encode IDs as hexadecimal strings
18-
request['X-Instana-T'] = ::Instana.tracer.id_to_header(our_trace_id)
19-
request['X-Instana-S'] = ::Instana.tracer.id_to_header(our_span_id)
16+
request['X-Instana-T'] = ::Instana.tracer.trace_id_header
17+
request['X-Instana-S'] = ::Instana.tracer.span_id_header
2018

2119
response = request_without_instana(*args, &block)
2220

2321
# Pickup response headers; convert back to base 10 integer
24-
if response.key?('X-Instana-T')
25-
their_trace_id = ::Instana.tracer.header_to_id(response.header['X-Instana-T'])
26-
27-
if our_trace_id != their_trace_id
28-
::Instana.logger.debug "#{Thread.current}: Trace ID mismatch on net/http response! ours: #{our_trace_id} theirs: #{their_trace_id}"
22+
if ::Instana.debug? && response.key?('X-Instana-T')
23+
if ::Instana.tracer.trace_id != ::Instana.tracer.header_to_id(response.header['X-Instana-T'])
24+
::Instana.logger.debug "#{Thread.current}: Trace ID mismatch on net/http response! ours: #{::Instana.tracer.trace_id} theirs: #{their_trace_id}"
2925
end
3026
end
3127

lib/instana/logger.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ class XLogger < Logger
66
STAMP = "Instana: ".freeze
77

88
def initialize(*args)
9-
if ENV['INSTANA_GEM_DEV']
10-
self.debug_level=:agent
9+
if ENV.key?('INSTANA_GEM_TEST')
10+
self.level = Logger::DEBUG
11+
elsif ENV.key?('INSTANA_GEM_DEV')
12+
self.level = Logger::DEBUG
13+
self.debub_level = nil
14+
else
15+
self.level = Logger::WARN
1116
end
1217
super(*args)
1318
end
@@ -24,6 +29,8 @@ def initialize(*args)
2429
# ::Instana.logger.debug_level = [:agent_comm, :trace]
2530
#
2631
def debug_level=(levels)
32+
return unless levels
33+
2734
LEVELS.each do |l|
2835
instance_variable_set("@level_#{l}", false)
2936
end

lib/instana/util.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@ def take_snapshot
117117
::Instana.logger.debug e.backtrace.join("\r\n")
118118
return data
119119
end
120+
121+
# Used in class initialization and after a fork, this method
122+
# collects up process information
123+
#
124+
def collect_process_info
125+
process = {}
126+
cmdline = ProcTable.ps(Process.pid).cmdline.split("\0")
127+
process[:name] = cmdline.shift
128+
process[:arguments] = cmdline
129+
130+
if @is_osx
131+
# Handle OSX bug where env vars show up at the end of process name
132+
# such as MANPATH etc..
133+
process[:name].gsub!(/[_A-Z]+=\S+/, '')
134+
process[:name].rstrip!
135+
end
136+
137+
process[:pid] = Process.pid
138+
# This is usually Process.pid but in the case of docker, the host agent
139+
# will return to us the true host pid in which we use to report data.
140+
process[:report_pid] = nil
141+
process
142+
end
120143
end
121144
end
122145
end

0 commit comments

Comments
 (0)