Class: Fluent::BaritoBatchK8sOutput

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_barito_batch_k8s.rb

Constant Summary collapse

PLUGIN_NAME =
'barito_batch_k8s'

Instance Method Summary collapse

Instance Method Details

#clean_attribute(record) ⇒ Object



78
79
80
81
82
83
# File 'lib/fluent/plugin/out_barito_batch_k8s.rb', line 78

def clean_attribute(record)
  # Delete kubernetes & docker field
  record.delete('kubernetes')
  record.delete('docker')
  record
end

#format(tag, time, record) ⇒ Object

Overide from BufferedOutput



26
27
28
# File 'lib/fluent/plugin/out_barito_batch_k8s.rb', line 26

def format(tag, time, record)
  [tag, time, record].to_msgpack
end

#merge_log_attribute(record) ⇒ Object



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

def merge_log_attribute(record)
  message_log = nil
  begin
    message_log = JSON.parse(record['log'])
  rescue
  end

  if !message_log.nil?
    return record.merge(message_log)
  end

  record
end

#startObject

Overide from BufferedOutput



21
22
23
# File 'lib/fluent/plugin/out_barito_batch_k8s.rb', line 21

def start
  super
end

#write(chunk) ⇒ Object

Overide from BufferedOutput



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
# File 'lib/fluent/plugin/out_barito_batch_k8s.rb', line 31

def write(chunk)
  data = {
    'items' => []
  }

  transport = Fluent::Plugin::BaritoTransport.new(@produce_url, log)
  chunk.msgpack_each do |tag, time, record|

    # Kubernetes annotations
     = record['kubernetes']

    record = clean_attribute(record)
    trail = Fluent::Plugin::ClientTrail.new(true)
    timber = Fluent::Plugin::TimberFactory::create_timber(tag, time, record, trail)
    new_timber = merge_log_attribute(timber)

    # Add kubernetes information
    new_timber['k8s_metadata'] = {
      'pod_name' => ['pod_name'],
      'namespace_name' => ['namespace_name'],
      'container_name' => ['container_name'],
      'host' => ['host'],
      'cluster_name' => @cluster_name
    }
    
    # Add extra labels from config_params
    unless @additional_labels.empty?
      new_timber['client_trail'].merge!(@additional_labels)
    end

    data['items'] << new_timber
  end

  if @application_secret.nil? or @application_secret.empty?
      return if @application_group_secret.nil? or @application_name.nil?
      header = {
        content_type: :json,
        'X-App-Group-Secret' => @application_group_secret,
        'X-App-Name' => @application_name
      }
  else
    header = {content_type: :json, 'X-App-Secret' => @application_secret}
  end

  transport.send_compressed(data, header)
end