-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for ActionController::Live #772
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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} stream", 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 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is bad. We shouldn't have to reach into the API internals in this way. This happens because
OpenTelemetry::Context.clear
is implemented asstack.clear
, which empties the backing array instead of replacing it with a new empty instance.I think it'd be worthwhile to change the implementation of
OpenTelemetry::Context.clear
to beThread.current[STACK_KEY] = []
.On the other hand, I think there is a race condition when fetching the current context. Specifically, since the thread-locals are copied, the backing array for the context stack is shared with the parent thread, and if that parent thread modifies its context before this thread retrieves it, the context used in this thread will be incorrect.
Side note: the tracing library we built at Shopify before OpenTelemetry Ruby patched
ActionController::Live
in a very similar way, other than unsetting the thread local. It worked there because the thread-local was aSpan
instead of an array, and we managed the "stack" as a linked list of spans.I think it might be preferable to explicitly propagate context in some way, but I'm not familiar enough with
ActionController::Live
to know how to accomplish that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we could leverage/patch
ActiveSupport::IsolatedExecutionState.share_with
in some way? 🤔That would be a good place to "propagate" the context, probably (if we fix
Context.clear
) by:It's a bit of a hack, in that we're not going to
Context.detach
. The patch forLive
could callOpenTelemetry::Context.clear
after thein_span
call.I.e. our patch for
ActiveSupport::IsolatedExecutionState.share_with
would be:And our patch for
Live
would be simply:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @fbogsany. I will open a PR in
opentelemetry-ruby
to propose your suggested change toContext.clear
. I'll also probably close this PR and reopen a new one with a different approach, using explicit propagation like you suggest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried a linked list implementation of the context stack in open-telemetry/opentelemetry-ruby#1597 but the performance impact was significant (~9% with the interpreter, as much as 24% with yjit).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open-telemetry/opentelemetry-ruby#1598
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
open-telemetry/opentelemetry-ruby#1598 is merged. Once we cut a new release of the API, we can use that as a minimum version here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thomasmarshall - A new version of the API has been released! Could you incorporate it into this PR?