# Define some unobtrusive instrumentation module Stripe module Instrumentation module Collection def all(*args) ActiveSupport::Notifications.instrument("stripe.#{collection_type}.list") { super } end def create(*args) ActiveSupport::Notifications.instrument("stripe.#{collection_type}.create") { super } end def retrieve(id, *args) ActiveSupport::Notifications.instrument("stripe.#{collection_type}.retrieve", id: id) { super } end def collection_type @_collection_type ||= self.url.split('/').last.singularize.capitalize end end module List def self.prepended(base); base.extend ClassMethods; end module ClassMethods def all(*args) ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.list") { super } end end end module Request def self.prepended(base); base.extend ClassMethods; end module ClassMethods def retrieve(id, *args) ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.retrieve", id: id) { super } end end end module Create def self.prepended(base); base.extend ClassMethods; end module ClassMethods def create(*args) ActiveSupport::Notifications.instrument("stripe.#{self.class_name.downcase}.create") { super } end end end module Update def save(*args) ActiveSupport::Notifications.instrument("stripe.#{self.class.class_name.downcase}.update", id: self['id']) { super } end end module Delete def delete(*args) ActiveSupport::Notifications.instrument("stripe.#{self.class.class_name.downcase}.delete", id: self['id']) { super } end end end end # Prepend the instrumentation if required into each APIResource Stripe::APIResource.descendants.each do |klass| klass.prepend Stripe::Instrumentation::Request if klass.ancestors.include?(Stripe::APIOperations::Request) klass.prepend Stripe::Instrumentation::List if klass.ancestors.include?(Stripe::APIOperations::List) klass.prepend Stripe::Instrumentation::Update if klass.ancestors.include?(Stripe::APIOperations::Update) klass.prepend Stripe::Instrumentation::Delete if klass.ancestors.include?(Stripe::APIOperations::Delete) klass.prepend Stripe::Instrumentation::Create if klass.ancestors.include?(Stripe::APIOperations::Create) end Stripe::ListObject.prepend Stripe::Instrumentation::Collection