Skip to content

Commit ae6bcb1

Browse files
committed
fix: action_pack assuming sdk span
1 parent 08e3ff4 commit ae6bcb1

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,18 @@ def start(_name, _id, payload)
3737
attributes[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath
3838

3939
span = OpenTelemetry::Instrumentation::Rack.current_span
40-
span_name = if @span_naming == :semconv
41-
if http_route
40+
if @span_naming == :semconv
41+
span.name = if http_route
4242
"#{request.method} #{http_route}"
4343
else
4444
"#{request.method} /#{payload.dig(:params, :controller)}/#{payload.dig(:params, :action)}"
4545
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
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 = "#{payload[:controller]}##{payload[:action]}"
50+
end
5351

54-
span.name = span_name
5552
span.add_attributes(attributes)
5653
rescue StandardError => e
5754
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)