Class: Fluent::Plugin::CMetricsSplunkMetricPayloadFormatter
- Inherits:
-
Formatter
- Object
- Formatter
- Fluent::Plugin::CMetricsSplunkMetricPayloadFormatter
- Includes:
- PluginHelper::Mixin
- Defined in:
- lib/fluent/plugin/formatter_cmetrics_splunk_metric_payload.rb
Instance Method Summary collapse
- #configure(conf) ⇒ Object
- #format(tag, time, record) ⇒ Object
-
#initialize ⇒ CMetricsSplunkMetricPayloadFormatter
constructor
A new instance of CMetricsSplunkMetricPayloadFormatter.
Constructor Details
#initialize ⇒ CMetricsSplunkMetricPayloadFormatter
Returns a new instance of CMetricsSplunkMetricPayloadFormatter.
53 54 55 56 |
# File 'lib/fluent/plugin/formatter_cmetrics_splunk_metric_payload.rb', line 53 def initialize super @default_host = Socket.gethostname end |
Instance Method Details
#configure(conf) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/fluent/plugin/formatter_cmetrics_splunk_metric_payload.rb', line 58 def configure(conf) super @cmetrics_name_accessor = record_accessor_create(@cmetrics_name_key) @cmetrics_value_accessor = record_accessor_create(@cmetrics_value_key) @cmetrics_dims_accessor = record_accessor_create(@cmetrics_dims_key) @host_key_accessor = record_accessor_create(@host_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 |
#format(tag, time, record) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fluent/plugin/formatter_cmetrics_splunk_metric_payload.rb', line 74 def format(tag, time, record) host = if host = @host_key_accessor.call(record) host else @default_host end extra_fields = {} payload = { host: host, # From the API reference # https://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTinput#services.2Fcollector time: time.to_f.to_s, event: 'metric', } payload[:index] = @index if @index payload[:source] = @source if @source payload[:sourcetype] = @sourcetype if @sourcetype fields = { "metric_name:#{@cmetrics_name_accessor.call(record)}" => @cmetrics_value_accessor.call(record) } if dims = @cmetrics_dims_accessor.call(record) fields.merge!(dims) end @fields_accessors.each do |key, accessor| if record = accessor.call(record) record.each do |k, v| if @only_use_last_field_keys extra_fields[k.split(".").last] = v else extra_fields[k] = v end end end fields.merge!(extra_fields) end payload.merge!(fields) Yajl.dump(payload) end |