-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an instrumentation for the `ownership` gem. When a transaction is created, if an owner has been set in the gem, use the name of the owner as the namespace for the transaction. Do the same when an owner is set during an active transaction. If more than one owner is set during a transaction, the namespace will be that of the last owner that was set.
- Loading branch information
Showing
9 changed files
with
495 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'ownership' | ||
|
||
gemspec :path => '../' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
class Hooks | ||
# @api private | ||
class OwnershipHook < Appsignal::Hooks::Hook | ||
register :ownership | ||
|
||
def dependencies_present? | ||
defined?(::Ownership) && | ||
Gem::Version.new(::Ownership::VERSION) >= Gem::Version.new("0.2.0") && | ||
Appsignal.config && | ||
Appsignal.config[:instrument_ownership] | ||
end | ||
|
||
def install | ||
require "appsignal/integrations/ownership" | ||
|
||
# If a transaction is created in a code context that has an owner, | ||
# set the namespace of the transaction to the owner. | ||
Appsignal::Transaction.after_create << | ||
Appsignal::Integrations::OwnershipIntegrationHelper.method(:after_create) | ||
|
||
# Ensure the method is only added once. | ||
Appsignal::Transaction.after_create.uniq! | ||
|
||
# If an error was reported in a code context that has an owner, | ||
# set the namespace of the transaction to the owner. | ||
# In some circumstances, this will be more accurate than the last owner | ||
# that was set for the transaction, which is what would otherwise be | ||
# reported. | ||
Appsignal::Transaction.before_complete << | ||
Appsignal::Integrations::OwnershipIntegrationHelper.method(:before_complete) | ||
|
||
# Ensure the method is only added once. | ||
Appsignal::Transaction.before_complete.uniq! | ||
|
||
# If an owner is set in a code context that has an active transaction, | ||
# set the namespace of the transaction to the owner. | ||
unless ::Ownership.singleton_class.included_modules.include?( | ||
Appsignal::Integrations::OwnershipIntegration | ||
) | ||
::Ownership.singleton_class.prepend Appsignal::Integrations::OwnershipIntegration | ||
end | ||
|
||
Appsignal::Environment.report_enabled("ownership") | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Appsignal | ||
module Integrations | ||
# @api private | ||
module OwnershipIntegration | ||
# Implement the `around_change` logic by monkey-patching the reader, | ||
# instead of by using the `around_change=` writer. This allows customers | ||
# to use the `around_change=` writer in their own code without | ||
# accidentally overriding AppSignal's instrumentation. | ||
def around_change | ||
proc do |owner, block| | ||
OwnershipIntegrationHelper.set(Appsignal::Transaction.current, owner) | ||
|
||
original = super | ||
|
||
if original | ||
original.call(owner, block) | ||
else | ||
block.call | ||
end | ||
end | ||
end | ||
end | ||
|
||
module OwnershipIntegrationHelper | ||
class << self | ||
def set(transaction, owner) | ||
transaction.set_namespace(owner) unless owner.nil? | ||
end | ||
|
||
def after_create(transaction) | ||
set(transaction, ::Ownership.owner) | ||
end | ||
|
||
def before_complete(transaction, error) | ||
set(transaction, error.owner) if error.respond_to?(:owner) | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.