Skip to content

Instantly share code, notes, and snippets.

@mikehale
Last active August 29, 2015 14:17
Show Gist options
  • Save mikehale/f3ba1dddc61ddcf80e2a to your computer and use it in GitHub Desktop.
Save mikehale/f3ba1dddc61ddcf80e2a to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "excon"
require "active_support"
require "active_support/subscriber"
# Translates excon instrumentation events into rails style with
# namespace last. Yuck!
class ExconToRailsInstrumentor
def self.instrument(name, datum, &block)
namespace, *event = name.split(".")
rails_name = [event, namespace].flatten.join(".")
ActiveSupport::Notifications.instrument(rails_name, datum, &block)
end
end
# Subscriber class that is compatible with `attach_to`.
module Excon
class Subscriber < ActiveSupport::Subscriber
def request(event)
puts event.name
end
def response(event)
puts event.name
end
end
end
# This effectively just sets up some callbacks that sort of look like this:
# * notifier.subscribe("request.excon", Proc { Excon::Subscriber.new.request(event) })
# * notifier.subscribe("response.excon", Proc { Excon::Subscriber.new.response(event) })
Excon::Subscriber.attach_to "excon"
Excon.get("https://google.com", :instrumentor => ExconToRailsInstrumentor)
# $ ./attach_to.rb
# request.excon
# response.excon
#!/usr/bin/env ruby
require "excon"
require "active_support"
# Subscribe to request notifications.
ActiveSupport::Notifications.subscribe(/excon.request/) do |name, started, finished, unique_id, data|
puts name
end
# Subscribe to response notifications.
ActiveSupport::Notifications.subscribe(/excon.response/) do |name, started, finished, unique_id, data|
puts name
end
# Register ActiveSupport::Notifications as the thing we call `instrument` on. The name passed as the
# first paramater to insturment is opque. It simply needs to match what we expect in our subscribe calls above.
Excon.get('http://google.com', :instrumentor => ActiveSupport::Notifications)
# $ ./instrument.rb
# excon.request
# excon.response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment