Class: NewRelicMetricsParser

Inherits:
BaseMetricsParser show all
Defined in:
lib/fluent/plugin/newrelic_metrics_parser.rb

Instance Method Summary collapse

Methods inherited from BaseMetricsParser

#clean_data, #emit_parsed_metrics, #normalise_data, #serialize_data

Constructor Details

#initialize(metric_prefix, router, tag) ⇒ NewRelicMetricsParser

Returns a new instance of NewRelicMetricsParser.



6
7
8
9
10
# File 'lib/fluent/plugin/newrelic_metrics_parser.rb', line 6

def initialize(metric_prefix, router, tag)
  @metric_prefix = metric_prefix
  @router = router
  @tag = tag
end

Instance Method Details

#format_data(cleaned_data = [], prefix = '', separator = '') ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/newrelic_metrics_parser.rb', line 12

def format_data(cleaned_data = [], prefix = '', separator = '')
  hash_data_array = []
  data_hash = {}
  data_array = []
  puts cleaned_data
  cleaned_data.each do |interim_data|
    metrics_hash = {}
    if interim_data =~ /{/ && interim_data =~ /}/
      attributes = {}
      metric_name, additional_dims, metric_val_and_time = interim_data.match(/(.*){(.*)}(.*)/i).captures
      additional_dims.split("\",").each do |interim_data|
        puts interim_data
        pair_data = interim_data.gsub("\"", "").gsub("{", "").gsub("}", "")
        interim_data_value = pair_data.split("=", 2)[1]
        interim_data_key = pair_data.split("=", 2)[0]
        attributes[interim_data_key] = interim_data_value
      end
      if metric_val_and_time =~ / /
        metrics_hash['name'] = prefix + separator + metric_name
        metrics_hash['value'] =
            metric_val_and_time.strip.split[0] =~ /^\S*\.\S*$/ ? metric_val_and_time.strip.split[0].to_f : metric_val_and_time.strip.split[0].to_i
        metrics_hash['timestamp'] = metric_val_and_time.strip.split[1].to_i
        metrics_hash['attributes'] = attributes
      end
    else
      metrics_hash['name'], metrics_hash['value'], metrics_hash['timestamp'] = interim_data.split
      metrics_hash['name'] = prefix + separator + metrics_hash['name']
      metrics_hash['value'] =
          metrics_hash['value'] =~ /^\S*\.\S*$/ ? metrics_hash['value'].to_f : metrics_hash['value'].to_i
      metrics_hash['timestamp'] = metrics_hash['timestamp'].to_i
    end
    data_array << metrics_hash
  end
  data_hash["metrics"] = data_array
  hash_data_array.push(data_hash)
  hash_data_array
end