Skip to content

Commit 6703511

Browse files
authored
fix: action_pack always assuming sdk spans (#1562)
fix: action_pack assuming sdk span
1 parent 08e3ff4 commit 6703511

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

instrumentation/action_pack/lib/opentelemetry/instrumentation/action_pack/handlers/action_controller.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ def initialize(config)
2323
# @param payload [Hash] the payload passed as a method argument
2424
# @return [Hash] the payload passed as a method argument
2525
def start(_name, _id, payload)
26+
span = OpenTelemetry::Instrumentation::Rack.current_span
27+
return unless span.recording?
28+
2629
request = payload[:request]
2730
# It seems that there are cases in Rails functional tests where it bypasses the routing system and the `action_dispatch.route_uri_pattern` header not being set.
2831
# Our Test suite executes the routing system so we are unable to recreate this error case.
@@ -36,22 +39,18 @@ def start(_name, _id, payload)
3639
attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_ROUTE] = http_route if http_route
3740
attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath
3841

39-
span = OpenTelemetry::Instrumentation::Rack.current_span
40-
span_name = if @span_naming == :semconv
41-
if http_route
42+
if @span_naming == :semconv
43+
span.name = if http_route
4244
"#{request.method} #{http_route}"
4345
else
4446
"#{request.method} /#{payload.dig(:params, :controller)}/#{payload.dig(:params, :action)}"
4547
end
46-
# If there is an exception we want to keep the original span name
47-
# so it is easier to see where the request was routed to.
48-
elsif request.env['action_dispatch.exception']
49-
span.name
50-
else
51-
"#{payload[:controller]}##{payload[:action]}"
52-
end
48+
# If there is an exception we want to keep the original span name
49+
# so it is easier to see where the request was routed to.
50+
elsif !request.env['action_dispatch.exception']
51+
span.name = "#{payload[:controller]}##{payload[:action]}"
52+
end
5353

54-
span.name = span_name
5554
span.add_attributes(attributes)
5655
rescue StandardError => e
5756
OpenTelemetry.handle_error(exception: e)

instrumentation/action_pack/test/opentelemetry/instrumentation/action_pack/handlers/action_controller_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@
236236
_(span.attributes['code.namespace']).must_equal 'ExceptionsController'
237237
_(span.attributes['code.function']).must_equal 'show'
238238
end
239+
240+
it 'does not raise with api/non recording spans' do
241+
with_sampler(OpenTelemetry::SDK::Trace::Samplers::ALWAYS_OFF) do
242+
get 'internal_server_error'
243+
end
244+
end
239245
end
240246

241247
it 'sets filters `http.target`' do

instrumentation/action_pack/test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@
2424
c.use 'OpenTelemetry::Instrumentation::ActionPack'
2525
c.add_span_processor span_processor
2626
end
27+
28+
def with_sampler(sampler)
29+
previous_sampler = OpenTelemetry.tracer_provider.sampler
30+
OpenTelemetry.tracer_provider.sampler = sampler
31+
yield
32+
ensure
33+
OpenTelemetry.tracer_provider.sampler = previous_sampler
34+
end

0 commit comments

Comments
 (0)