-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1364 from appsignal/ownership-integration
Ownership integration
- Loading branch information
Showing
17 changed files
with
915 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
bump: patch | ||
type: add | ||
--- | ||
|
||
Add support for the [Ownership](https://github.com/ankane/ownership) gem, which is used to mark different segments of the application as owned by specific teams. | ||
|
||
The AppSignal sample will be tagged with the given owner: | ||
|
||
```ruby | ||
class OrdersController < ApplicationController | ||
owner :logistics | ||
# Transactions for requests handled by this controller will be tagged | ||
# in AppSignal with the "owner" tag set to "logistics" | ||
end | ||
``` | ||
|
||
If several owners are set within the same transaction, the last owner will take precedence. If an error is reported in the transaction, the owner tag will be set to the owner that was set when the error was raised. | ||
|
||
Set the [`ownership_set_namespace` configuration option](https://docs.appsignal.com/ruby/configuration/options.html#option-ownership_set_namespace) to `true` to also set the AppSignal sample's namespace to the owner. Note that doing so will cause existing performance and error incidents to be re-created with the new namespace. | ||
|
||
Set the [`instrument_ownership` configuration option](https://docs.appsignal.com/ruby/configuration/options.html#option-instrument_ownership) to `false` to disable the integration with the Ownership gem. |
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 |
---|---|---|
|
@@ -90,6 +90,7 @@ def self.report(key) | |
hanami | ||
hiredis | ||
mongo_ruby_driver | ||
ownership | ||
padrino | ||
passenger | ||
puma | ||
|
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,44 @@ | ||
# 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) | ||
|
||
# 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) | ||
|
||
# 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,51 @@ | ||
# 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) | ||
return if owner.nil? | ||
|
||
transaction.add_tags(:owner => owner) | ||
transaction.set_namespace(owner) if set_namespace? | ||
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 | ||
|
||
private | ||
|
||
def set_namespace? | ||
Appsignal.config && Appsignal.config[:ownership_set_namespace] | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.