Class: SplunkMetricsParser

Inherits:
BaseMetricsParser show all
Defined in:
lib/fluent/plugin/splunk_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) ⇒ SplunkMetricsParser

Returns a new instance of SplunkMetricsParser.



6
7
8
9
10
# File 'lib/fluent/plugin/splunk_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
# File 'lib/fluent/plugin/splunk_metrics_parser.rb', line 12

def format_data(cleaned_data = [], prefix = '', separator = '')
  hash_data_array = []
  cleaned_data.each do |interim_data|
    hash_data_array << generate_hash_from_data(interim_data, prefix, separator)
  end
  hash_data_array
end

#generate_hash_from_data(data = '', prefix = '', separator = '') ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fluent/plugin/splunk_metrics_parser.rb', line 20

def generate_hash_from_data(data = '', prefix = '', separator = '')
  metrics_hash = {}
  if data =~ /{/ && data =~ /}/
    metric_name, additional_dims, metric_val_and_time = data.match(/(.*){(.*)}(.*)/i).captures
    if metric_val_and_time =~ / /
      metrics_hash['metric_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['time'] = metric_val_and_time.strip.split[1].to_i
      if additional_dims =~ /,/
        additional_dims.split(/,/).map do |interim_data|
          metrics_hash[interim_data.split(/=/)[0]] = interim_data.split(/=/)[1].gsub(/"/, '') if interim_data =~ /=/
        end
      end
    end
  else
    metrics_hash['metric_name'], metrics_hash['value'], metrics_hash['time'] = data.split
    metrics_hash['metric_name'] = prefix + separator + metrics_hash['metric_name']
    metrics_hash['value'] =
        metrics_hash['value'] =~ /^\S*\.\S*$/ ? metrics_hash['value'].to_f : metrics_hash['value'].to_i
    metrics_hash['time'] =  metrics_hash['time'].to_i
  end
  metrics_hash
end