Skip to content

Instantly share code, notes, and snippets.

@kreynolds
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreynolds/a129f5bcb84a8cf21d79 to your computer and use it in GitHub Desktop.
Save kreynolds/a129f5bcb84a8cf21d79 to your computer and use it in GitHub Desktop.
ActiveSupport/Rails Stripe Instrumentation
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment