Skip to content

Instantly share code, notes, and snippets.

@be9
Created July 23, 2014 04:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save be9/2034c5b6bfa229682008 to your computer and use it in GitHub Desktop.
Save be9/2034c5b6bfa229682008 to your computer and use it in GitHub Desktop.
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