Class: DatadogMetricsParser

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

Returns a new instance of DatadogMetricsParser.



6
7
8
9
10
# File 'lib/fluent/plugin/datadog_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
49
50
51
52
53
# File 'lib/fluent/plugin/datadog_metrics_parser.rb', line 12

def format_data(cleaned_data = [], prefix = '', separator = '')
  hash_data_array = []
  data_hash = {}
  data_array = []
  cleaned_data.each do |interim_data|
    metrics_hash = {}
    tags = []
    point = {}
    points = []
    if interim_data =~ /{/ && interim_data =~ /}/
      metric_name, additional_dims, metric_val_and_time = interim_data.match(/(.*){(.*)}(.*)/i).captures
      additional_dims.split("\",").each do |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]
        interim_data_tag = interim_data_key + ':' + interim_data_value
        tags << interim_data_tag
      end
      if metric_val_and_time =~ / /
        metrics_hash['metric'] = prefix + separator + metric_name
        metric_timestamp = metric_val_and_time.strip.split[1]
        point['timestamp'] = metric_timestamp[0,10].to_i if metric_timestamp != nil
        point['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
        points << point
        metrics_hash['points'] = points
        metrics_hash['tags'] = tags
      end
    else
      metrics_hash['metric'], metric_value, metric_timestamp = interim_data.split
      metrics_hash['metric'] = prefix + separator + metrics_hash['metric']
      point['value'] = metric_value =~ /^\S*\.\S*$/ ? metric_value.to_f : metric_value.to_i
      point['timestamp'] = metric_timestamp[0,10].to_i
      points << point
      metrics_hash['points'] = points
    end
    metrics_hash['type'] = 3
    data_array << metrics_hash
  end
  data_hash["series"] = data_array
  hash_data_array.push(data_hash)
  hash_data_array
end