-
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.
Merge branch 'main' into create-sql-instrumentation-helpers
- Loading branch information
Showing
65 changed files
with
285 additions
and
110 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
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
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
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
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
38 changes: 38 additions & 0 deletions
38
instrumentation/action_pack/lib/opentelemetry/instrumentation/action_pack/handlers.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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require_relative 'handlers/action_controller' | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module ActionPack | ||
# Module that contains custom event handlers, which are used to generate spans per event | ||
module Handlers | ||
module_function | ||
|
||
def subscribe | ||
return unless Array(@subscriptions).empty? | ||
|
||
config = ActionPack::Instrumentation.instance.config | ||
handlers_by_pattern = { | ||
'process_action.action_controller' => Handlers::ActionController.new(config) | ||
} | ||
|
||
@subscriptions = handlers_by_pattern.map do |key, handler| | ||
::ActiveSupport::Notifications.subscribe(key, handler) | ||
end | ||
end | ||
|
||
# Removes Event Handler Subscriptions for Action Controller notifications | ||
# @note this method is not thread-safe and should not be used in a multi-threaded context | ||
def unsubscribe | ||
@subscriptions&.each { |subscriber| ::ActiveSupport::Notifications.unsubscribe(subscriber) } | ||
@subscriptions = nil | ||
end | ||
end | ||
end | ||
end | ||
end |
59 changes: 59 additions & 0 deletions
59
...n/action_pack/lib/opentelemetry/instrumentation/action_pack/handlers/action_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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Instrumentation | ||
module ActionPack | ||
module Handlers | ||
# Action Controller handler to handle the notification from Active Support | ||
class ActionController | ||
# @param config [Hash] of instrumentation options | ||
def initialize(config) | ||
@config = config | ||
end | ||
|
||
# Invoked by ActiveSupport::Notifications at the start of the instrumentation block | ||
# | ||
# @param _name [String] of the event (unused) | ||
# @param _id [String] of the event (unused) | ||
# @param payload [Hash] the payload passed as a method argument | ||
# @return [Hash] the payload passed as a method argument | ||
def start(_name, _id, payload) | ||
rack_span = OpenTelemetry::Instrumentation::Rack.current_span | ||
|
||
request = payload[:request] | ||
|
||
rack_span.name = "#{payload[:controller]}##{payload[:action]}" unless request.env['action_dispatch.exception'] | ||
|
||
attributes_to_append = { | ||
OpenTelemetry::SemanticConventions::Trace::CODE_NAMESPACE => String(payload[:controller]), | ||
OpenTelemetry::SemanticConventions::Trace::CODE_FUNCTION => String(payload[:action]) | ||
} | ||
|
||
attributes_to_append[OpenTelemetry::SemanticConventions::Trace::HTTP_TARGET] = request.filtered_path if request.filtered_path != request.fullpath | ||
|
||
rack_span.add_attributes(attributes_to_append) | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
|
||
# Invoked by ActiveSupport::Notifications at the end of the instrumentation block | ||
# | ||
# @param _name [String] of the event (unused) | ||
# @param _id [String] of the event (unused) | ||
# @param payload [Hash] the payload passed as a method argument | ||
# @return [Hash] the payload passed as a method argument | ||
def finish(_name, _id, payload) | ||
rack_span = OpenTelemetry::Instrumentation::Rack.current_span | ||
rack_span.record_exception(payload[:exception_object]) if payload[:exception_object] | ||
rescue StandardError => e | ||
OpenTelemetry.handle_error(exception: e) | ||
end | ||
end | ||
end | ||
end | ||
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
40 changes: 0 additions & 40 deletions
40
...ion_pack/lib/opentelemetry/instrumentation/action_pack/patches/action_controller/metal.rb
This file was deleted.
Oops, something went wrong.
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
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
34 changes: 34 additions & 0 deletions
34
instrumentation/action_pack/test/opentelemetry/instrumentation/action_pack/handlers_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,34 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
|
||
require_relative '../../../../lib/opentelemetry/instrumentation/action_pack' | ||
|
||
describe 'OpenTelemetry::Instrumentation::ActionPack::Handlers' do | ||
let(:instrumentation) { OpenTelemetry::Instrumentation::ActionPack::Instrumentation.instance } | ||
let(:config) { {} } | ||
|
||
before do | ||
OpenTelemetry::Instrumentation::ActionPack::Handlers.unsubscribe | ||
instrumentation.instance_variable_set(:@config, config) | ||
instrumentation.instance_variable_set(:@installed, false) | ||
|
||
instrumentation.install(config) | ||
end | ||
|
||
it 'success subscribe the notification' do | ||
subscriptions = OpenTelemetry::Instrumentation::ActionPack::Handlers.instance_variable_get(:@subscriptions) | ||
_(subscriptions.count).must_equal 1 | ||
_(subscriptions[0].pattern).must_equal 'process_action.action_controller' | ||
end | ||
|
||
it 'success unsubscribe the notification' do | ||
OpenTelemetry::Instrumentation::ActionPack::Handlers.unsubscribe | ||
subscriptions = OpenTelemetry::Instrumentation::ActionPack::Handlers.instance_variable_get(:@subscriptions) | ||
_(subscriptions).must_be_nil | ||
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.