Created
July 23, 2014 04:31
-
-
Save be9/2034c5b6bfa229682008 to your computer and use it in GitHub Desktop.
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
if ENV['INSTRUMENT_CACHE_KEYS'] | |
class CacheKeyInstrumenter | |
include Singleton | |
def hit(payload) | |
record :hits, payload | |
end | |
def miss(payload) | |
record :misses, payload | |
end | |
private | |
def record(what, payload) | |
k = key_head(payload[:key]) | |
StatsD.increment("#{App.statsd_context}.cache.#{k}.#{what}") | |
end | |
def key_head(key) | |
key.split('/').first.gsub(/[#\.]/, '__') | |
end | |
end | |
######## | |
ActiveSupport::Notifications.subscribe('cache_fetch_hit.active_support') do |name, start, finish, id, payload| | |
CacheKeyInstrumenter.instance.hit(payload) | |
end | |
ActiveSupport::Notifications.subscribe('cache_generate.active_support') do |name, start, finish, id, payload| | |
CacheKeyInstrumenter.instance.miss(payload) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment