-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ActionController::Live
This commit adds a patch to ensure `ActionController::Live` actions, which are processed in a new thread, are instrumented as a child span in the current trace. This is a patch rather than an `ActiveSupport::Notifications` handler. Although the `action_controller.process_action` event is processed in the new thread, we don't have a reference to the controller or anything to indicate that it is an `ActionController::Live` controller. The patch needs to be included rather than prepended, as `ActionController::Live` is a module that is included in each controller as required by the host app, rather than a class to inherit from. As such, `include` is the only way to get our `#process_action` method into the right place. We need to manually unset the `:__opentelemetry_context_storage__` thread local, as this gets copied across from the parent thread and points to the same stack array. Calling `#clear` would modify that shared stack array, so we need to unset it instead.
- Loading branch information
1 parent
73fa809
commit 8613d1f
Showing
6 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...tion_pack/lib/opentelemetry/instrumentation/action_pack/patches/action_controller/live.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module ActionPack | ||
module Patches | ||
module ActionController | ||
# Module to append to ActionController::Live for instrumentation | ||
module Live | ||
def process_action(*) | ||
current_context = OpenTelemetry::Context.current | ||
|
||
# Unset thread local to avoid modifying stack array shared with parent thread | ||
Thread.current[:__opentelemetry_context_storage__] = nil | ||
|
||
attributes = { | ||
OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => self.class.name, | ||
OpenTelemetry::SemanticConventions::Trace::CODE_FUNCTION => action_name | ||
} | ||
|
||
OpenTelemetry::Context.with_current(current_context) do | ||
Instrumentation.instance.tracer.in_span("#{self.class.name}##{action_name}", attributes: attributes) do | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
53 changes: 53 additions & 0 deletions
53
...ack/test/opentelemetry/instrumentation/action_pack/patches/action_controller/live_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
|
||
describe OpenTelemetry::Instrumentation::ActionPack::Patches::ActionController::Live do | ||
include Rack::Test::Methods | ||
|
||
let(:instrumentation) { OpenTelemetry::Instrumentation::ActionPack::Instrumentation.instance } | ||
let(:exporter) { EXPORTER } | ||
let(:spans) { exporter.finished_spans } | ||
let(:span) { exporter.finished_spans.last } | ||
let(:rails_app) { DEFAULT_RAILS_APP } | ||
let(:config) { {} } | ||
|
||
# Clear captured spans | ||
before do | ||
OpenTelemetry::Instrumentation::ActionPack::Handlers.unsubscribe | ||
|
||
instrumentation.instance_variable_set(:@config, config) | ||
instrumentation.instance_variable_set(:@installed, false) | ||
|
||
instrumentation.install(config) | ||
|
||
exporter.reset | ||
end | ||
|
||
it 'creates a child span for the new thread' do | ||
get '/stream' | ||
|
||
parent_span = spans[-2] | ||
|
||
_(last_response.ok?).must_equal true | ||
_(span.name).must_equal 'ExampleLiveController#stream' | ||
_(span.kind).must_equal :internal | ||
_(span.status.ok?).must_equal true | ||
|
||
_(span.instrumentation_library.name).must_equal 'OpenTelemetry::Instrumentation::ActionPack' | ||
_(span.instrumentation_library.version).must_equal OpenTelemetry::Instrumentation::ActionPack::VERSION | ||
|
||
_(span.attributes['code.namespace']).must_equal 'ExampleLiveController' | ||
_(span.attributes['code.function']).must_equal 'stream' | ||
|
||
_(span.parent_span_id).must_equal parent_span.span_id | ||
end | ||
|
||
def app | ||
rails_app | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
instrumentation/action_pack/test/test_helpers/controllers/example_live_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
class ExampleLiveController < ActionController::Base | ||
include ActionController::Live | ||
|
||
def stream | ||
response.headers['Content-Type'] = 'text/event-stream' | ||
10.times do | ||
response.stream.write "hello world\n" | ||
sleep 0.1 | ||
end | ||
ensure | ||
response.stream.close | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters