Class: Fluent::BaritoDynamicAppBatchK8sOutput

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

Constant Summary collapse

PLUGIN_NAME =
'barito_dynamic_app_batch_k8s'

Instance Method Summary collapse

Instance Method Details

#clean_attribute(record) ⇒ Object



81
82
83
84
85
86
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 81

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

#expand_application_name_format(record) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 102

def expand_application_name_format(record)
  application_name = @application_name_format.dup

  # Regular expression to match placeholders like ${record["key1"]["key2"]}
  placeholder_regex = /\${record(\["[^"]*"\])+}/

  application_name.gsub!(placeholder_regex) do |placeholder|
    # Extract keys from the placeholder
    keys = placeholder.scan(/\["([^"]*)"\]/).flatten
    # Retrieve the value from the record hash
    value = get_nested_value(record, keys)
    value.to_s
  end

  application_name
end

#format(tag, time, record) ⇒ Object

Overide from BufferedOutput



25
26
27
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 25

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

#get_nested_value(record, keys) ⇒ Object

Retrieve nested value from record using array of keys



120
121
122
123
124
125
126
127
128
129
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 120

def get_nested_value(record, keys)
  keys.reduce(record) do |acc, key|
    if acc.is_a?(Hash) && acc.key?(key)
      acc[key]
    else
      # Key not found; return nil to stop further traversal
      return nil
    end
  end
end

#merge_log_attribute(record) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 88

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



20
21
22
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 20

def start
  super
end

#write(chunk) ⇒ Object

Overide from BufferedOutput



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
72
73
74
75
76
77
78
79
# File 'lib/fluent/plugin/out_barito_dynamic_app_batch_k8s.rb', line 30

def write(chunk)
  data = {}

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

    # generate application name
    if @application_name_format.nil?
      return
    end

    application_name = expand_application_name_format(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

    if data[application_name].nil?
      data[application_name] = { 'items' => [] }
    end
    data[application_name]['items'] << new_timber
  end

  data.each do |application_name, record|
    header = {
      content_type: :json,
      'X-App-Group-Secret' => @application_group_secret,
      'X-App-Name' => application_name
    }
    transport.send_compressed(record, header)
  end
end