Module: SafeMemoize::Adapters::StatsD

Defined in:
lib/safe_memoize/adapters/statsd.rb

Overview

Optional StatsD adapter.

Routes SafeMemoize lifecycle events to any StatsD-compatible client (any object that responds to +#increment+).

Configure via Configuration#statsd_client:

SafeMemoize.configure do |c| c.statsd_client = Datadog::Statsd.new end

Emitted metrics:

Metric Fires on
+safe_memoize.hit+ cache hit
+safe_memoize.miss+ cache miss
+safe_memoize.store+ value written
+safe_memoize.evict+ LRU eviction
+safe_memoize.expire+ TTL expiration

Every metric is tagged with +method:+ and +class:+. Client errors are rescued and warned rather than raised.

Constant Summary collapse

METRIC_NAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  on_hit: "safe_memoize.hit",
  on_miss: "safe_memoize.miss",
  on_evict: "safe_memoize.evict",
  on_expire: "safe_memoize.expire",
  on_store: "safe_memoize.store"
}.freeze

Class Method Summary collapse

Class Method Details

.dispatch(client, hook_type, cache_key, class_name) ⇒ void

This method returns an undefined value.

Dispatches a lifecycle event to the StatsD client.

Parameters:

  • client (Object)

    a StatsD-compatible client responding to +#increment+

  • hook_type (Symbol)

    one of the keys in METRIC_NAMES

  • cache_key (Array)

    the internal cache key (first element is the method name)

  • class_name (String, nil)


45
46
47
48
49
50
51
52
53
# File 'lib/safe_memoize/adapters/statsd.rb', line 45

def self.dispatch(client, hook_type, cache_key, class_name)
  metric = METRIC_NAMES[hook_type]
  return unless metric

  tags = ["method:#{cache_key[0]}", "class:#{class_name}"]
  client.increment(metric, tags: tags)
rescue => error
  warn "[SafeMemoize] StatsD dispatch error: #{error.message}"
end