Class: Fluent::Plugin::SumoContainerOutput

Inherits:
Filter
  • Object
show all
Defined in:
lib/fluent/plugin/filter_kubernetes_sumologic.rb

Instance Method Summary collapse

Instance Method Details

#configure(conf) ⇒ Object



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

def configure(conf)
  super
end

#filter(tag, time, record) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/fluent/plugin/filter_kubernetes_sumologic.rb', line 59

def filter(tag, time, record)
  # Set the sumo metadata fields
   = record["_sumo_metadata"] || {}
  record["_sumo_metadata"] = 
  log_fields = {}
  [:log_format] = @log_format
  [:host] = @source_host if @source_host
  [:source] = @source_name if @source_name

  unless @source_category.nil?
    [:category] = @source_category.dup
    unless @source_category_prefix.nil?
      [:category].prepend(@source_category_prefix)
    end
  end

  if record.key?("_SYSTEMD_UNIT") and not record.fetch("_SYSTEMD_UNIT").nil?
    unless @exclude_unit_regex.empty?
      if Regexp.compile(@exclude_unit_regex).match(record["_SYSTEMD_UNIT"])
        return nil
      end
    end

    unless @exclude_facility_regex.empty?
      if Regexp.compile(@exclude_facility_regex).match(record["SYSLOG_FACILITY"])
        return nil
      end
    end

    unless @exclude_priority_regex.empty?
      if Regexp.compile(@exclude_priority_regex).match(record["PRIORITY"])
        return nil
      end
    end

    unless @exclude_host_regex.empty?
      if Regexp.compile(@exclude_host_regex).match(record["_HOSTNAME"])
        return nil
      end
    end
  end

  # Allow fields to be overridden by annotations
  if record.key?("kubernetes") and not record.fetch("kubernetes").nil?
    # Clone kubernetes hash so we don't override the cache
    kubernetes = record["kubernetes"].clone
     = {
        :namespace => kubernetes["namespace_name"],
        :pod => kubernetes["pod_name"],
        :pod_id => kubernetes['pod_id'],
        :container => kubernetes["container_name"],
        :source_host => kubernetes["host"],
    }


    if kubernetes.has_key? "labels"
      kubernetes["labels"].each { |k, v| ["label:#{k}".to_sym] = v }
    end
    if kubernetes.has_key? "namespace_labels"
      kubernetes["namespace_labels"].each { |k, v| ["namespace_label:#{k}".to_sym] = v }
    end
    .default = "undefined"

    annotations = kubernetes.fetch("annotations", {})
    if annotations["sumologic.com/include"] == "true"
      include = true
    else
      include = false
    end

    unless @exclude_namespace_regex.empty?
      if Regexp.compile(@exclude_namespace_regex).match([:namespace]) and not include
        return nil
      end
    end

    unless @exclude_pod_regex.empty?
      if Regexp.compile(@exclude_pod_regex).match([:pod]) and not include
        return nil
      end
    end

    unless @exclude_container_regex.empty?
      if Regexp.compile(@exclude_container_regex).match([:container]) and not include
        return nil
      end
    end

    unless @exclude_host_regex.empty?
      if Regexp.compile(@exclude_host_regex).match([:source_host]) and not include
        return nil
      end
    end

    sanitize_pod_name()

    if annotations["sumologic.com/exclude"] == "true"
      return nil
    end

    [:log_format] = annotations["sumologic.com/format"] if annotations["sumologic.com/format"]

    if annotations["sumologic.com/sourceHost"].nil?
      [:host] = [:host] % 
    else
      [:host] = annotations["sumologic.com/sourceHost"] % 
    end

    if annotations["sumologic.com/sourceName"].nil?
      [:source] = [:source] % 
    else
      [:source] = annotations["sumologic.com/sourceName"] % 
    end

    if annotations["sumologic.com/sourceCategory"].nil?
      [:category] = [:category] % 
    else
      [:category] = (annotations["sumologic.com/sourceCategory"] % ).prepend(@source_category_prefix)
    end
    [:category].gsub!("-", @source_category_replace_dash)

    # Strip kubernetes metadata from json if disabled
    if annotations["sumologic.com/kubernetes_meta"] == "false" || !@kubernetes_meta
      record.delete("docker")
      record.delete("kubernetes")
    end
    if annotations["sumologic.com/kubernetes_meta_reduce"] == "true" || annotations["sumologic.com/kubernetes_meta_reduce"].nil? && @kubernetes_meta_reduce == true
      record.delete("docker")
      record["kubernetes"].delete("pod_id")
      record["kubernetes"].delete("namespace_id")
      record["kubernetes"].delete("labels")
      record["kubernetes"].delete("namespace_labels")
      record["kubernetes"].delete("master_url")
      record["kubernetes"].delete("annotations")
    end
    if @add_stream == false
      record.delete("stream")
    end
    if @add_time == false
      record.delete("time")
    end
    # Strip sumologic.com annotations
    kubernetes.delete("annotations") if annotations

    if @log_format == "fields" and record.key?("docker") and not record.fetch("docker").nil?
      record["docker"].each {|k, v| log_fields[k] = v}
    end

    if @log_format == "fields" and record.key?("kubernetes") and not record.fetch("kubernetes").nil?
      if kubernetes.has_key? "labels"
        kubernetes["labels"].each { |k, v| log_fields["pod_labels_#{k}".to_sym] = v }
      end
      if kubernetes.has_key? "namespace_labels"
        kubernetes["namespace_labels"].each { |k, v| log_fields["namespace_labels_#{k}".to_sym] = v }
      end
      log_fields["container"] = kubernetes["container_name"] unless kubernetes["container_name"].nil?
      log_fields["namespace"] = kubernetes["namespace_name"] unless kubernetes["namespace_name"].nil?
      log_fields["pod"] = kubernetes["pod_name"] unless kubernetes["pod_name"].nil?
      ["pod_id", "host", "master_url", "namespace_id", "service", "deployment", "daemonset", "replicaset", "statefulset"].each do |key|
        log_fields[key] = kubernetes[key] unless kubernetes[key].nil?
      end
    end
  end

  if @log_format == "fields" and not log_fields.nil?
    [:fields] = log_fields.map{|k,v| "#{k}=#{v}"}.join(',')
    record.delete("docker")
    record.delete("kubernetes")
  end
  record
end

#is_number?(string) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/fluent/plugin/filter_kubernetes_sumologic.rb', line 30

def is_number?(string)
  true if Float(string) rescue false
end

#sanitize_pod_name(k8s_metadata) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fluent/plugin/filter_kubernetes_sumologic.rb', line 34

def sanitize_pod_name()
  # Strip out dynamic bits from pod name.
  # NOTE: Kubernetes deployments append a template hash.
  # At the moment this can be in 3 different forms:
  #   1) pre-1.8: numeric in pod_template_hash and pod_parts[-2]
  #   2) 1.8-1.11: numeric in pod_template_hash, hash in pod_parts[-2]
  #   3) post-1.11: hash in pod_template_hash and pod_parts[-2]

  pod_parts = [:pod].split("-")
  pod_template_hash = [:"label:pod-template-hash"]
  if (pod_template_hash == pod_parts[-2] ||
      to_hash(pod_template_hash) == pod_parts[-2])
    [:pod_name] = pod_parts[0..-3].join("-")
  else
    [:pod_name] = pod_parts[0..-2].join("-")
  end
end

#to_hash(pod_template_hash) ⇒ Object



52
53
54
55
56
57
# File 'lib/fluent/plugin/filter_kubernetes_sumologic.rb', line 52

def to_hash(pod_template_hash)
  # Convert the pod_template_hash to an alphanumeric string using the same logic Kubernetes
  # uses at https://github.com/kubernetes/apimachinery/blob/18a5ff3097b4b189511742e39151a153ee16988b/pkg/util/rand/rand.go#L119
  alphanums = "bcdfghjklmnpqrstvwxz2456789"
  pod_template_hash.each_byte.map { |i| alphanums[i.to_i % alphanums.length] }.join("")
end