Module: UnicornMetrics::Registry

Defined in:
lib/unicorn_metrics/registry.rb

Overview

UnicornMetrics::Registry is a container for Metrics

Constant Summary collapse

METRIC_TYPES =

Map metrics types to class names

{
  :counter          => 'Counter',
  :timer            => 'Timer',
  :response_counter => 'ResponseCounter',
  :request_counter  => 'RequestCounter',
  :request_timer    => 'RequestTimer'
}

Class Method Summary collapse

Class Method Details

.as_jsonHash

Returns default JSON representation of metrics.

Returns:

  • (Hash)

    default JSON representation of metrics



44
45
46
47
48
# File 'lib/unicorn_metrics/registry.rb', line 44

def as_json(*)
  metrics.inject({}) do |hash, (name, metric)|
    hash.merge(metric.as_json)
  end
end

.metricsHash

Return a hash of metrics that have been defined

Returns:

  • (Hash)

    a metric name to metric object



24
25
26
# File 'lib/unicorn_metrics/registry.rb', line 24

def metrics
  @metrics ||= {}
end

.register(type, name, *args) ⇒ Counter, ...

Register a new metric. Arguments are optional. See metric class definitions.

Parameters:

  • type (Symbol)

    underscored metric name

  • name (String)

    string representing the metric name

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/unicorn_metrics/registry.rb', line 33

def register(type, name, *args)
  type          = METRIC_TYPES.fetch(type) { raise "Invalid type: #{type}" }
  validate_name!(name)
  metric        = UnicornMetrics.const_get(type).new(name,*args)
  metrics[name] = metric
  define_getter(name)

  metric
end