Skip to content

Commit 47653f6

Browse files
committed
Add a root section to prevent missing timing in some specific cases
1 parent bd43537 commit 47653f6

14 files changed

+35
-34
lines changed

lib/rorvswild/agent.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ def measure_job(name, parameters: nil, &block)
113113

114114
def start_execution(execution)
115115
Thread.current[:rorvswild_execution] ||= execution
116+
RorVsWild::Section.start
116117
end
117118

118119
def stop_execution
119120
return unless execution = current_execution
121+
RorVsWild::Section.stop
120122
execution.stop
121123
case execution
122124
when Execution::Job then queue_job

test/agent_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_measure_section_with_exception
3232
end
3333
end
3434
end
35-
assert_equal(2, current_sections_without_gc.size)
35+
assert_equal(2, current_user_sections.size)
3636
end
3737

3838
def test_measure_job_when_ignored
@@ -45,7 +45,7 @@ def test_measure_job_when_recursive
4545
agent.measure_job("parent") do
4646
agent.measure_job("child") { }
4747
end
48-
assert_equal(1, (sections = current_sections_without_gc).size)
48+
assert_equal(1, (sections = current_user_sections).size)
4949
assert_equal("child", sections[0].command)
5050
end
5151

@@ -63,7 +63,7 @@ def test_measure_class_method
6363
line = Example.method(:foo).source_location[1]
6464
agent.measure_method(Example.method(:foo))
6565
agent.measure_job("job") { assert_equal(1, Example.foo) }
66-
section = current_sections_without_gc.first
66+
section = current_user_sections.first
6767
assert_equal("code", section.kind)
6868
assert_equal("/agent_test.rb", section.file)
6969
assert_equal(line, section.line)
@@ -74,7 +74,7 @@ def test_measure_instance_method
7474
line = Example.instance_method(:bar).source_location[1]
7575
agent.measure_method(Example.instance_method(:bar))
7676
agent.measure_job("job") { assert_equal(2, Example.new.bar) }
77-
section = current_sections_without_gc.first
77+
section = current_user_sections.first
7878
assert_equal("code", section.kind)
7979
assert_equal("/agent_test.rb", section.file)
8080
assert_equal(line, section.line)

test/helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ def client.transmit(request)
4141
agent
4242
end
4343

44-
def current_sections_without_gc
45-
agent.current_execution.sections.select { |s| s.kind != "gc" }
44+
# Returns all section except the GC and the root one which is the last
45+
def current_user_sections
46+
agent.current_execution.sections[0..-2].select { |s| s.kind != "gc" }
4647
end
4748

4849
def start_request(path = "/")

test/plugin/action_mailer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_callback
1313
end
1414
end
1515

16-
sections = current_sections_without_gc
16+
sections = current_user_sections
1717
assert_equal(1, sections.size)
1818
assert_equal("Mailer", sections[0].command)
1919
assert_equal(line, sections[0].line.to_i)

test/plugin/action_view_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_render_template_callback
1818
end
1919
end
2020

21-
sections = current_sections_without_gc
21+
sections = current_user_sections
2222
sub_partial, partial, template = sections[0], sections[1], sections[2]
2323
assert_equal(3, sections.size)
2424

@@ -48,7 +48,7 @@ def test_render_collection
4848
end
4949
end
5050

51-
sections = current_sections_without_gc
51+
sections = current_user_sections
5252
collection, template = sections[0], sections[1]
5353
assert_equal(2, sections.size)
5454

@@ -65,14 +65,14 @@ def test_render_empty_collection
6565
agent.measure_block("test") do
6666
instrument("render_template.action_view", {identifier: "_collection.html.erb", layout: nil, count: 0}) { }
6767
end
68-
assert_empty(current_sections_without_gc)
68+
assert_empty(current_user_sections)
6969
end
7070

7171
def test_render_withtout_identifier
7272
agent.measure_block("test") do
7373
instrument("render_template.action_view", {identifier: nil}) { }
7474
end
75-
assert_empty(current_sections_without_gc)
75+
assert_empty(current_user_sections)
7676
end
7777

7878
private

test/plugin/active_job_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ def setup
2828

2929
def test_callback
3030
SampleJob.perform_now
31-
sections = current_sections_without_gc
31+
sections = current_user_sections
3232
assert_equal("RorVsWild::Plugin::ActiveJobTest::SampleJob", agent.current_execution.name)
3333
assert_equal(1, sections.size)
3434
assert_equal("RorVsWild::Plugin::ActiveJobTest::SampleJob#perform", sections[0].command)
3535
end
3636

3737
def test_callback_on_exception
3838
assert_raises { SampleJob.perform_now("Error") }
39-
sections = current_sections_without_gc
39+
sections = current_user_sections
4040
assert_equal("RorVsWild::Plugin::ActiveJobTest::SampleJob", agent.current_execution.name)
4141
assert_equal(1, sections.size)
4242
assert_equal("RorVsWild::Plugin::ActiveJobTest::SampleJob#perform", sections[0].command)

test/plugin/active_record_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_render_template_callback
2121
end
2222
end
2323

24-
sections = current_sections_without_gc
24+
sections = current_user_sections
2525
sql1, sql2 = sections[0], sections[1]
2626
assert_equal(2, sections.size)
2727

@@ -47,7 +47,7 @@ def test_transaction_begin_insert_into_and_commit
4747
instrument_sql("COMMIT")
4848
end
4949
end
50-
sections = current_sections_without_gc
50+
sections = current_user_sections
5151
assert_equal(1, sections.size)
5252
assert_equal(9, sections[0].calls)
5353
assert_equal("BEGIN\nINSERT INTO users\nCOMMIT", sections[0].command)
@@ -69,7 +69,7 @@ def test_async_query
6969
ActiveSupport::Notifications.publish_event(event)
7070
end
7171
end
72-
query, root = current_sections_without_gc
72+
query, root = current_user_sections
7373
assert_equal(root.children_ms, query.total_ms)
7474
end
7575

test/plugin/middleware_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ class RorVsWild::Plugin::MiddlewareTest < Minitest::Test
66
def test_callback
77
middleware.call("ORIGINAL_FULLPATH" => "/foo/bar")
88
assert_equal("/foo/bar", agent.current_execution.path)
9-
assert_equal(1, (sections = current_sections_without_gc).size)
9+
assert_equal(1, (sections = current_user_sections).size)
1010
assert_equal("Rails::Engine#call", sections[0].command)
1111
end
1212

1313
def test_queue_time_section
1414
middleware.call("HTTP_X_REQUEST_START" => unix_timestamp_seconds.to_s)
1515

16-
sections = current_sections_without_gc
16+
sections = current_user_sections
1717
assert_equal(2, sections.size)
1818
queue_time_section = sections[0]
1919
assert_equal "queue", queue_time_section.file
@@ -25,23 +25,23 @@ def test_queue_time_section
2525
def test_queue_time_secs
2626
middleware.call("HTTP_X_REQUEST_START" => (unix_timestamp_seconds - 0.123).to_s)
2727

28-
sections = current_sections_without_gc
28+
sections = current_user_sections
2929
assert_equal(2, sections.size)
3030
assert_operator(123, :<=, sections[0].total_ms)
3131
end
3232

3333
def test_queue_time_millis
3434
middleware.call("HTTP_X_QUEUE_START" => (unix_timestamp_seconds * 1000 - 234).to_s)
3535

36-
sections = current_sections_without_gc
36+
sections = current_user_sections
3737
assert_equal(2, sections.size)
3838
assert_operator(234, :<=, sections[0].total_ms)
3939
end
4040

4141
def test_queue_time_micros
4242
middleware.call("HTTP_X_MIDDLEWARE_START" => (unix_timestamp_seconds * 1_000_000 - 345_000).to_s)
4343

44-
sections = current_sections_without_gc
44+
sections = current_user_sections
4545
assert_equal(2, sections.size)
4646
assert_operator(345, :<=, sections[0].total_ms)
4747
end

test/plugin/mongo_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_callback
2121
mongo[:mountains].find(altitude: {"$gt": 4800}).each { |mountain| }
2222
end
2323

24-
sections = current_sections_without_gc
24+
sections = current_user_sections
2525
assert_equal(3, sections.size)
2626

2727
assert_equal(1, sections[0].calls)

test/plugin/net_http_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class RorVsWild::Plugin::NetHttpTest < Minitest::Test
77

88
def test_callback
99
agent.measure_block("test") { Net::HTTP.get("www.ruby-lang.org", "/index.html") }
10-
sections = current_sections_without_gc
10+
sections = current_user_sections
1111
assert_equal(1, sections.size)
1212
assert_equal(1, sections[0].calls)
1313
assert_equal("http", sections[0].kind)
@@ -16,7 +16,7 @@ def test_callback
1616

1717
def test_callback_with_https
1818
agent.measure_block("test") { Net::HTTP.get(URI("https://www.ruby-lang.org/index.html")) }
19-
sections = current_sections_without_gc
19+
sections = current_user_sections
2020
assert_match("GET www.ruby-lang.org", sections[0].command)
2121
assert_equal("http", sections[0].kind)
2222
end
@@ -27,7 +27,7 @@ def test_nested_query_because_net_http_request_is_recursive_when_connection_is_n
2727
http = Net::HTTP.new(uri.host, uri.port)
2828
http.request(Net::HTTP::Get.new(uri.path))
2929
end
30-
sections = current_sections_without_gc
30+
sections = current_user_sections
3131
assert_equal(1, sections[0].calls)
3232
end
3333
end

0 commit comments

Comments
 (0)