Class: Fluent::Plugin::BeaconOutput

Inherits:
Output
  • Object
show all
Defined in:
lib/fluent/plugin/out_f5_beacon.rb

Constant Summary collapse

DEFAULT_BUFFER_TYPE =
"memory"
FORMATTED_RESULT_FOR_INVALID_RECORD =
''.freeze

Instance Method Summary collapse

Constructor Details

#initializeBeaconOutput

Returns a new instance of BeaconOutput.



57
58
59
60
61
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 57

def initialize
  super
  @seq = 0
  @prev_timestamp = nil
end

Instance Method Details

#configure(conf) ⇒ Object

Raises:

  • (Fluent::ConfigError)


63
64
65
66
67
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 63

def configure(conf)
  compat_parameters_convert(conf, :buffer)
  super
  raise Fluent::ConfigError, "'tag' in chunk_keys is required." if not @chunk_key_tag
end

#create_request(payload) ⇒ Object



192
193
194
195
196
197
198
199
200
201
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 192

def create_request(payload)
  uri = URI.parse(@endpoint)

  req = Net::HTTP.const_get("Post").new(uri.request_uri)
  req['X-F5-Ingestion-Token'] = @token
  req['Content-Type'] = 'text/plain'
  req.body = payload

  return req, uri
end

#format(tag, time, record) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 76

def format(tag, time, record)
  # Remove nil/empty values
  record.delete_if { |k, v| v.nil? || v.to_s.empty? }

  if record.empty?
    log.warn "skip record '#{record}' in '#{tag}', because record has no values"
    FORMATTED_RESULT_FOR_INVALID_RECORD
  else
    [precision_time(time), record].to_msgpack
  end
end

#formatted_to_msgpack_binaryObject



92
93
94
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 92

def formatted_to_msgpack_binary
  true
end

#handle_payload(payload) ⇒ Object



187
188
189
190
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 187

def handle_payload(payload)
  req, uri = create_request(payload)
  send_request(req, uri)
end

#http_opts(uri) ⇒ Object

end send_request



228
229
230
231
232
233
234
235
236
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 228

def http_opts(uri)
  opts = {
      :use_ssl => true,
      :verify_mode => OpenSSL::SSL::VERIFY_PEER,
      :ssl_version => :TLSv1_2,
      :ciphers => ['AES128-GCM-SHA256', 'AES128-SHA256', 'AES256-GCM-SHA384', 'AES256-SHA256', 'ECDHE-RSA-AES128-GCM-SHA256', 'ECDHE-RSA-AES128-SHA256', 'ECDHE-RSA-AES256-GCM-SHA384', 'ECDHE-RSA-AES256-SHA384'],
  }
  opts
end

#multi_workers_ready?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 96

def multi_workers_ready?
  true
end

#precision_time(time) ⇒ Object



180
181
182
183
184
185
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 180

def precision_time(time)
  return if time.nil?

  # nsec is supported from v0.14
  time * (10 ** 9) + (time.is_a?(Integer) ? 0 : time.nsec)
end

#send_request(req, uri) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 203

def send_request(req, uri)
  res = nil

  log.debug { "sending data to Beacon" }
  log.trace { req.body }

  begin
    res = Net::HTTP.start(uri.host, uri.port, **http_opts(uri)) {|http| http.request(req) }

  rescue => e # rescue all StandardErrors
    # server didn't respond
    log.warn "Net::HTTP.#{req.method.capitalize} raises exception: #{e.class}, '#{e.message}'"
    raise e
  else
    unless res and res.is_a?(Net::HTTPSuccess)
      res_summary = if res
                      "#{res.code} #{res.message} #{res.body}"
                    else
                      "res=nil"
                    end
      log.warn "failed to #{req.method} #{uri} (#{res_summary})"
    end #end unless
  end # end begin
end

#serialize(points) ⇒ Object



174
175
176
177
178
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 174

def serialize(points)
  points.map do |point|
    InfluxDB::PointValue.new(point).dump
  end.join("\n".freeze)
end

#shutdownObject



88
89
90
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 88

def shutdown
  super
end

#startObject



69
70
71
72
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 69

def start
  super
  log.info "starting F5 Beacon plugin..."
end

#write(chunk) ⇒ Object



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 100

def write(chunk)
  points = []
  tag = chunk..tag
  chunk.msgpack_each do |time, record|
    timestamp = precision_time(record.delete(@time_key)) || time
    if tag_keys.empty? && !@auto_tags
      values = record
      tags = {}
    else
      values = {}
      tags = {}
      record.each_pair do |k, v|
        if (@auto_tags && v.is_a?(String)) || @tag_keys.include?(k)
          # If the tag value is not nil, empty, or a space, add the tag
          normalized_value = v.to_s.strip
          if normalized_value != ''
            tags[k] = normalized_value
          end
        else
          values[k] = v
        end
      end
    end
    if @sequence_tag
      if @prev_timestamp == timestamp
        @seq += 1
      else
        @seq = 0
      end
      tags[@sequence_tag] = @seq
      @prev_timestamp = timestamp
    end

    values.delete_if do |k, v|
      if v.is_a?(Array) || v.is_a?(Hash)
        log.warn "array/hash field '#{k}' discarded; consider using other Fluentd plugins to properly map it"
        true
      end
    end

    if values.empty?
      log.warn "skip record '#{record}', because one value is required"
      next
    end

    if @cast_number_to_float
      values.each do |key, value|
        if value.is_a?(Integer)
          values[key] = Float(value)
        end
      end
    end

    tags["beacon-fluent-source"] = @source_name

    point = {
        timestamp: timestamp,
        series: @measurement || tag,
        values: values,
        tags: tags,
    }
    points << point
  end

  if points.length > 0
    write_points(points)
  end
end

#write_points(points) ⇒ Object



169
170
171
172
# File 'lib/fluent/plugin/out_f5_beacon.rb', line 169

def write_points(points)
  payload = serialize(points)
  handle_payload(payload)
end