Skip to content

Commit e7bca6b

Browse files
authored
Merge pull request #256 from instana/hm/ruby-trace-stats
Report statistics on generated traces
2 parents acec297 + da49f70 commit e7bca6b

File tree

7 files changed

+76
-8
lines changed

7 files changed

+76
-8
lines changed

lib/instana/backend/host_agent_reporting_observer.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class HostAgentReportingObserver
99
ENTITY_DATA_URL = '/com.instana.plugin.ruby.%i'.freeze
1010
RESPONSE_DATA_URL = '/com.instana.plugin.ruby/response.%i?messageId=%s'.freeze
1111
TRACES_DATA_URL = "/com.instana.plugin.ruby/traces.%i".freeze
12+
TRACE_METRICS_URL = "/tracermetrics".freeze
1213

1314
attr_reader :report_timer
1415

@@ -35,10 +36,24 @@ def update(time, _old_version, new_version)
3536
def report_to_backend
3637
report_metrics if ::Instana.config[:metrics][:enabled]
3738
report_traces if ::Instana.config[:tracing][:enabled]
39+
report_trace_stats if ::Instana.config[:tracing][:enabled]
3840
rescue StandardError => e
3941
@logger.error(%(#{e}\n#{e.backtrace.join("\n")}))
4042
end
4143

44+
def report_trace_stats
45+
discovery = @discovery.value
46+
return unless discovery
47+
48+
payload = {
49+
tracer: 'ruby',
50+
pid: discovery['pid'],
51+
metrics: @processor.span_metrics
52+
}
53+
54+
@client.send_request('POST', TRACE_METRICS_URL, payload)
55+
end
56+
4257
def report_traces
4358
discovery = @discovery.value
4459
return unless discovery

lib/instana/instrumentation/active_record.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def log(sql, name = 'SQL', binds = [], *args)
1919
}
2020
}
2121

22-
if binds.all? { |b| b.respond_to?(:value_before_type_cast) }
22+
if binds.all? { |b| b.respond_to?(:value_before_type_cast) } && !::Instana.config[:sanitize_sql]
2323
mapped = binds.map(&:value_before_type_cast)
2424
call_payload[:activerecord][:binds] = mapped
2525
end

lib/instana/tracing/processor.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def initialize(logger: ::Instana.logger)
2020
@batch_size = 3000
2121
@logger = logger
2222
@pid = Process.pid
23+
24+
@spans_opened = Concurrent::AtomicFixnum.new(0)
25+
@spans_closed = Concurrent::AtomicFixnum.new(0)
2326
end
2427

2528
# Adds a span to the span queue
@@ -34,9 +37,31 @@ def add_span(span)
3437
end
3538
# :nocov:
3639

40+
@spans_closed.increment
3741
@queue.push(span)
3842
end
3943

44+
# Note that we've started a new span. Used to
45+
# generate monitoring metrics.
46+
def start_span(_)
47+
@spans_opened.increment
48+
end
49+
50+
# Clears and retrieves metrics associated with span creation and submission
51+
def span_metrics
52+
response = {
53+
opened: @spans_opened.value,
54+
closed: @spans_closed.value,
55+
filtered: 0,
56+
dropped: 0
57+
}
58+
59+
@spans_opened.value = 0
60+
@spans_closed.value = 0
61+
62+
response
63+
end
64+
4065
##
4166
# send
4267
#
@@ -82,6 +107,9 @@ def queued_spans
82107
# test suite to reset state.
83108
#
84109
def clear!
110+
@spans_opened.value = 0
111+
@spans_closed.value = 0
112+
85113
until @queue.empty? do
86114
# Non-blocking pop; ignore exception
87115
@queue.pop(true) rescue nil

lib/instana/tracing/span.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def initialize(name, parent_ctx: nil, start_time: ::Instana::Util.now_in_ms)
7171
configure_custom(name)
7272
end
7373

74+
::Instana.processor.start_span(self)
75+
7476
# Attach a backtrace to all exit spans
7577
add_stack if ::Instana.config[:collect_backtraces] && exit_span?
7678
end

test/backend/host_agent_reporting_observer_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def test_start_stop
2323
end
2424

2525
def test_report
26+
stub_request(:post, "http://10.10.10.10:9292/tracermetrics")
27+
.to_return(status: 200)
2628
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
2729
.to_return(status: 200)
2830

@@ -35,6 +37,8 @@ def test_report
3537
end
3638

3739
def test_report_fail
40+
stub_request(:post, "http://10.10.10.10:9292/tracermetrics")
41+
.to_return(status: 200)
3842
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
3943
.to_return(status: 500)
4044

@@ -54,6 +58,9 @@ def test_agent_action
5458
args: {file: 'test_helper.rb'}
5559
)
5660

61+
stub_request(:post, "http://10.10.10.10:9292/tracermetrics")
62+
.to_return(status: 200)
63+
5764
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
5865
.to_return(status: 200, body: action)
5966

@@ -75,6 +82,9 @@ def test_agent_actions
7582
args: {file: 'test_helper.rb'}
7683
])
7784

85+
stub_request(:post, "http://10.10.10.10:9292/tracermetrics")
86+
.to_return(status: 200)
87+
7888
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
7989
.to_return(status: 200, body: action)
8090

@@ -90,6 +100,9 @@ def test_agent_actions
90100
end
91101

92102
def test_agent_action_error
103+
stub_request(:post, "http://10.10.10.10:9292/tracermetrics")
104+
.to_return(status: 200)
105+
93106
stub_request(:post, "http://10.10.10.10:9292/com.instana.plugin.ruby.0")
94107
.to_return(status: 200, body: 'INVALID')
95108

test/instrumentation/rails_active_record_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ def test_create
3838
data = span[:data][:activerecord]
3939

4040
assert data[:sql].start_with?('INSERT INTO')
41-
assert 'core', data[:binds][0]
42-
assert 'blue', data[:binds][1]
4341
end
4442

4543
def test_read
@@ -54,8 +52,6 @@ def test_read
5452
data = span[:data][:activerecord]
5553

5654
assert data[:sql].start_with?('SELECT')
57-
assert 'core', data[:binds][0]
58-
assert 1, data[:binds][1]
5955
end
6056

6157
def test_update
@@ -73,8 +69,6 @@ def test_update
7369
data = span[:data][:activerecord]
7470

7571
assert data[:sql].start_with?('UPDATE')
76-
assert 'red', data[:binds][0]
77-
assert 1, data[:binds][2]
7872
end
7973

8074
def test_delete
@@ -90,7 +84,6 @@ def test_delete
9084
data = span[:data][:activerecord]
9185

9286
assert data[:sql].start_with?('DELETE')
93-
assert 1, data[:binds][0]
9487
end
9588

9689
def test_raw

test/tracing/span_test.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,21 @@ def test_log_error
133133
assert_equal({}, span.tags)
134134
time.verify
135135
end
136+
137+
def test_inc_processed_counts
138+
clear_all!
139+
140+
span = Instana::Span.new(:excon)
141+
span.close
142+
143+
metrics = Instana.processor.span_metrics
144+
145+
assert_equal 1, metrics[:opened]
146+
assert_equal 1, metrics[:closed]
147+
148+
metrics = Instana.processor.span_metrics
149+
150+
assert_equal 0, metrics[:opened]
151+
assert_equal 0, metrics[:closed]
152+
end
136153
end

0 commit comments

Comments
 (0)