Class: Fluent::Plugin::ForwardedCMetricsParserFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_forwarded_cmetrics_parser.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/filter_forwarded_cmetrics_parser.rb', line 38

def configure(conf)
  super
  @labels_accessor = record_accessor_create(@cmetrics_labels_key)
  @fields_accessors = {}
  conf.elements(name: "fields").each do |e|
    e.each_pair{|k, _v|
      e.has_key?(k) # Suppress unused warnings.
      @fields_accessors[k] = record_accessor_create(k)
    }
  end
end

#filter_stream(tag, es) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fluent/plugin/filter_forwarded_cmetrics_parser.rb', line 98

def filter_stream(tag, es)
  new_es = Fluent::MultiEventStream.new
  es.each do |time, record|
    extra_fields = {}
    cmetrics = parse_cmetrics_hash(record)
    cmetrics.each do |metric|
      next if metric.empty?

      @fields_accessors.each do |key, accessor|
        extra_fields[key] = accessor.call(metric)
      end
      if @format_to_splunk_metric
        metric["name"], dims = format_to_splunk_style_with_dims(metric)
        if @dimensions_key
          metric[@dimensions_key] = dims
        else
          metric.merge!(dims)
        end
      end
      if @fields_accessors
        metric.merge!(extra_fields)
      end
      time = Time.at(metric.delete("timestamp"))
      new_es.add(Fluent::EventTime.new(time.to_i, time.nsec), metric)
    end
  end
  new_es
end

#format_to_splunk_style_with_dims(metric) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/plugin/filter_forwarded_cmetrics_parser.rb', line 85

def format_to_splunk_style_with_dims(metric)
  subsystem = metric.delete("subsystem")
  # labels will be treated as dimensions.
  dimensions = Hash.new(0)
  if labels = @labels_accessor.call(metric)
    labels.map {|k,v|
      dimensions[k] = v
    }
  end
  name = metric.delete("name")
  return [subsystem, name].compact.reject{|e| e.empty?}.join("."), dimensions
end

#parse_cmetrics_hash(record) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fluent/plugin/filter_forwarded_cmetrics_parser.rb', line 50

def parse_cmetrics_hash(record)
  cmetrics = []
  record.each do |payload|
    payload["metrics"].each do |metric|
      labels = []
      opts = metric["meta"]["opts"]
      unless metric["meta"]["labels"].empty?
        metric["meta"]["labels"].each do |k_label|
          labels << k_label
        end
      end
      metric["values"].each do |entry|
        cmetric = {
          "namespace" => opts["ns"],
          "subsystem" => opts["ss"],
          "name" => opts["name"],
          "value" => entry["value"],
          "description" => opts["desc"],
          "timestamp" => entry["ts"] / 1000000000.0
        }
        unless labels.empty?
          params = {}
          entry["labels"].each_with_index do |v_label, index|
            label = labels[index]
            params[label] = v_label
          end
          cmetric["labels"] = params
        end
        cmetrics << cmetric
      end
    end
  end
  cmetrics
end