class Invoca::Metrics::Client

Constants

STATSD_DEFAULT_HOSTNAME
STATSD_DEFAULT_PORT
STATSD_METRICS_SEPARATOR

Attributes

cluster_name[R]
gauge_cache[R]
hostname[R]
port[R]
server_label[R]
service_name[R]
statsd_client[R]
sub_server_name[R]

Public Class Methods

metrics(statsd_host: Invoca::Metrics.default_client_config[:statsd_host], statsd_port: Invoca::Metrics.default_client_config[:statsd_port], cluster_name: Invoca::Metrics.default_client_config[:cluster_name], service_name: Invoca::Metrics.default_client_config[:service_name], server_name: Invoca::Metrics.default_client_config[:server_name], sub_server_name: Invoca::Metrics.default_client_config[:sub_server_name], namespace: nil) click to toggle source

Default values are required for backwards compatibility

# File lib/invoca/metrics/client.rb, line 17
def metrics(statsd_host:     Invoca::Metrics.default_client_config[:statsd_host],
            statsd_port:     Invoca::Metrics.default_client_config[:statsd_port],
            cluster_name:    Invoca::Metrics.default_client_config[:cluster_name],
            service_name:    Invoca::Metrics.default_client_config[:service_name],
            server_name:     Invoca::Metrics.default_client_config[:server_name],
            sub_server_name: Invoca::Metrics.default_client_config[:sub_server_name],
            namespace:       nil)
  config = {
    hostname:        statsd_host || STATSD_DEFAULT_HOSTNAME,
    port:            statsd_port || STATSD_DEFAULT_PORT,
    cluster_name:    cluster_name,
    service_name:    service_name,
    server_label:    server_name,
    sub_server_name: sub_server_name,
    namespace:       namespace
  }.freeze

  client_cache[config] ||= new(**config)
end
new(hostname:, port:, cluster_name: nil, service_name: nil, server_label: nil, sub_server_name: nil, namespace: nil) click to toggle source
# File lib/invoca/metrics/client.rb, line 51
def initialize(hostname:, port:, cluster_name: nil, service_name: nil, server_label: nil, sub_server_name: nil, namespace: nil)
  @hostname        = hostname
  @port            = port
  @cluster_name    = cluster_name
  @service_name    = service_name
  @server_label    = server_label
  @sub_server_name = sub_server_name

  @statsd_client = StatsdClient.new(@hostname, @port)
  @statsd_client.namespace = namespace || [@cluster_name, @service_name].compact.join(STATSD_METRICS_SEPARATOR).presence

  @gauge_cache = GaugeCache.register(gauge_cache_key, @statsd_client)
end
reset_cache() click to toggle source
# File lib/invoca/metrics/client.rb, line 37
def reset_cache
  @client_cache = {}
end

Private Class Methods

client_cache() click to toggle source
# File lib/invoca/metrics/client.rb, line 43
def client_cache
  @client_cache ||= {}
end

Public Instance Methods

batch(&block) click to toggle source
# File lib/invoca/metrics/client.rb, line 129
def batch(&block)
  statsd_client.batch do |batch|
    Metrics::Batch.new(self, batch).ensure_send(&block)
  end
end
count(name, value = 1) click to toggle source
# File lib/invoca/metrics/client.rb, line 91
def count(name, value = 1)
  log_usage(name, :counter)
  if (args = normalized_metric_name_and_value(name, value, "counter"))
    statsd_client.count(*args)
  end
end
Also aliased as: counter
counter(name, value = 1)
Alias for: count
decrement(name) click to toggle source
# File lib/invoca/metrics/client.rb, line 104
def decrement(name)
  count(name, -1)
end
gauge(name, value) click to toggle source

This will store the gauge value passed in so that it is reported every GAUGE_REPORT_INTERVAL seconds and post the gauge at the same time to avoid delay in gauges being

# File lib/invoca/metrics/client.rb, line 83
def gauge(name, value)
  log_usage(name, :gauge)
  if (args = normalized_metric_name_and_value(name, value, "gauge"))
    gauge_cache.set(*args)
    statsd_client.gauge(*args)
  end
end
gauge_cache_key() click to toggle source
# File lib/invoca/metrics/client.rb, line 65
def gauge_cache_key
  [
    hostname,
    port,
    cluster_name,
    service_name,
    namespace,
    server_name,
    sub_server_name
  ].freeze
end
increment(name) click to toggle source
# File lib/invoca/metrics/client.rb, line 100
def increment(name)
  count(name, 1)
end
server_name() click to toggle source
# File lib/invoca/metrics/client.rb, line 77
def server_name # For backwards compatibility
  server_label
end
set(name, value) click to toggle source
# File lib/invoca/metrics/client.rb, line 108
def set(name, value)
  log_usage(name, :counter)
  if (args = normalized_metric_name_and_value(name, value, nil))
    statsd_client.set(*args)
  end
end
timer(name, milliseconds = nil, return_timing: false, &block) click to toggle source
# File lib/invoca/metrics/client.rb, line 115
def timer(name, milliseconds = nil, return_timing: false, &block)
  name.present? or raise ArgumentError, "Must specify a metric name."
  (!milliseconds.nil? ^ block_given?) or raise ArgumentError, "Must pass exactly one of milliseconds or block."
  name_and_type = [name, "timer", server_label].join(STATSD_METRICS_SEPARATOR)
  log_usage(name, :timer)

  if milliseconds.nil?
    result, block_time = time(name_and_type, &block)
    return_timing ? [result, block_time] : result
  else
    timing(name_and_type, milliseconds)
  end
end
transmit(message, extra_data = {}) click to toggle source

TODO: - implement transmit method

# File lib/invoca/metrics/client.rb, line 136
def transmit(message, extra_data = {})
  # TODO: - we need to wire up exception data to a monitoring service
end

Private Instance Methods

log_usage(metric_name, metric_type) click to toggle source
# File lib/invoca/metrics/client.rb, line 154
def log_usage(metric_name, metric_type)
  if Invoca::Metrics.graphite_usage_logging_enabled
    call_stack = caller_locations
    unless call_stack.any? { |l| /invoca\/metrics\/prometheus/.match?(l.to_s) }
      self.class.logger&.info(
        "Deprecated usage of grapihite metrics",
        invoca_metrics: {
          metric_name:   metric_name,
          metric_type:   metric_type,
          metric_source: call_stack.find { |l| !/invoca\/metrics/.match?(l.to_s) }.to_s
        }
      )
    end
  end
end
normalized_metric_name_and_value(name, value, stat_type) click to toggle source
# File lib/invoca/metrics/client.rb, line 144
def normalized_metric_name_and_value(name, value, stat_type)
  name.present? or raise ArgumentError, "Must specify a metric name."
  extended_name = [name, stat_type, @server_label, @sub_server_name].compact.join(STATSD_METRICS_SEPARATOR)
  if value
    [extended_name, value]
  else
    [extended_name]
  end
end