class Sensu::Plugin::Metric::CLI::Generic

Public Instance Methods

output(metric = {}) click to toggle source

Outputs metrics using different metric formats

@param metric [Hash] the metric hash with keys below @note the metric could have these fields:

`metric_name`: Mandatory, name for the metric,
`value`: Mandatory, metric value
`type`: Optional, metric type- `c` for counter, `g` for gauge, `ms` for timer, `h` for histogram, `s` for set
`tags`: Optional, a Hash that includes all tags
`timestamp`: Optional, unix timestamp, eventually defaults to output's timestamp handling
`graphite_metric_path`: Optional, `metric_name` will be used if not provided.
`statsd_metric_name`: Optional, `metric_name` will be used if not provided.
`statsd_type`: Optional.
`dogstatsd_metric_name`: Optional, `statsd_metric_name` or `metric_name` will be used if not provided.
`dogstatsd_type`: Optional, `statsd_type` will be used if not provided.
`influxdb_measurement`: Optional, class name will be used if not provided.
`influxdb_field_key`: Optional, the `metric_name` will be used if not provided.

@return [String] formated metric data based on metric_format configuration.

# File lib/sensu-plugin/metric/cli.rb, line 157
def output(metric = {})
  return if metric.nil? ||
            metric.empty? ||
            metric[:value].nil?

  tags = metric[:tags] || []

  case config[:metric_format]
  when 'json'
    return if metric[:value].nil?
    json_obj = metric[:json_obj] || {
      metric_name: metric[:metric_name],
      value: metric[:value],
      tags: tags
    }
    to_json json_obj
  when 'graphite'
    graphite_metric_path = metric[:graphite_metric_path] ||
                           metric[:metric_name]
    to_graphite graphite_metric_path, metric[:value], metric[:timestamp]
  when 'statsd'
    statsd_metric_name = metric[:statsd_metric_name] ||
                         metric[:metric_name]
    to_statsd statsd_metric_name, metric[:value], metric[:statsd_type]
  when 'dogstatsd'
    dogstatsd_metric_name = metric[:dogstatsd_metric_name] ||
                            metric[:statsd_metric_name] ||
                            metric[:metric_name]
    dogstatsd_type = metric[:dogstatsd_type] || metric[:statsd_type]
    dogstatsd_tags = tags.map { |k, v| "#{k}:#{v}" }.join(',')
    to_dogstatsd dogstatsd_metric_name, metric[:value],
                 dogstatsd_type, dogstatsd_tags
  when 'influxdb'
    influxdb_measurement = metric[:influxdb_measurement] ||
                           self.class.name
    influxdb_field_key = metric[:influxdb_field_key] ||
                         metric[:metric_name]
    influxdb_field = "#{influxdb_field_key}=#{metric[:value]}"
    influxdb_tags = tags.map { |k, v| "#{k}=#{v}" }.join(',')
    to_influxdb influxdb_measurement, influxdb_field,
                influxdb_tags, metric[:timestamp]
  end
end