Class: Fluent::BaritoK8sOutput

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

Constant Summary collapse

PLUGIN_NAME =
'barito_k8s'
LABEL_APP_SECRET =
'barito.applicationSecret'
LABEL_APP_GROUP_SECRET =
'barito.applicationGroupSecret'
LABEL_APP_NAME =
'barito.applicationName'
LABEL_PRODUCE_URL =
'barito.produceUrl'

Instance Method Summary collapse

Instance Method Details

#application_group_secret(params) ⇒ Object



81
82
83
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 81

def application_group_secret(params)
  params[LABEL_APP_GROUP_SECRET]
end

#application_name(params) ⇒ Object



85
86
87
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 85

def application_name(params)
  params[LABEL_APP_NAME]
end

#application_secret(params) ⇒ Object



77
78
79
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 77

def application_secret(params)
  params[LABEL_APP_SECRET]
end

#clean_attribute(record) ⇒ Object



89
90
91
92
93
94
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 89

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

#format(tag, time, record) ⇒ Object

Overide from BufferedOutput



23
24
25
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 23

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

#merge_log_attribute(record) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 96

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

#produce_url(params) ⇒ Object



73
74
75
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 73

def produce_url(params)
  params[LABEL_PRODUCE_URL]
end

#startObject

Overide from BufferedOutput



18
19
20
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 18

def start
  super
end

#write(chunk) ⇒ Object

Overide from BufferedOutput



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/fluent/plugin/out_barito_k8s.rb', line 28

def write(chunk)
  chunk.msgpack_each do |tag, time, record|

    # Kubernetes annotations
     = record['kubernetes']
    params = ['annotations']

    # Skip record if no annotations found
    next if params.nil?
    url = produce_url(params)
    app_secret = application_secret(params)
    app_group_secret = application_group_secret(params)
    app_name = application_name(params)

    next if url.nil?

    if app_secret.nil?
      next if app_group_secret.nil? or app_name.nil?
      header = {
        content_type: :json,
        'X-App-Group-Secret' => app_group_secret,
        'X-App-Name' => app_name
      }
    else
      header = {content_type: :json, 'X-App-Secret' => app_secret}
    end

    record = clean_attribute(record)
    transport = Fluent::Plugin::BaritoTransport.new(url, log)
    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']
    }

    transport.send(new_timber, header)
  end
end