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)
sumo_metadata = record["_sumo_metadata"] || {}
record["_sumo_metadata"] = sumo_metadata
log_fields = {}
sumo_metadata[:log_format] = @log_format
sumo_metadata[:host] = @source_host if @source_host
sumo_metadata[:source] = @source_name if @source_name
unless @source_category.nil?
sumo_metadata[:category] = @source_category.dup
unless @source_category_prefix.nil?
sumo_metadata[: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
if record.key?("kubernetes") and not record.fetch("kubernetes").nil?
kubernetes = record["kubernetes"].clone
k8s_metadata = {
: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| k8s_metadata["label:#{k}".to_sym] = v }
end
if kubernetes.has_key? "namespace_labels"
kubernetes["namespace_labels"].each { |k, v| k8s_metadata["namespace_label:#{k}".to_sym] = v }
end
k8s_metadata.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(k8s_metadata[:namespace]) and not include
return nil
end
end
unless @exclude_pod_regex.empty?
if Regexp.compile(@exclude_pod_regex).match(k8s_metadata[:pod]) and not include
return nil
end
end
unless @exclude_container_regex.empty?
if Regexp.compile(@exclude_container_regex).match(k8s_metadata[:container]) and not include
return nil
end
end
unless @exclude_host_regex.empty?
if Regexp.compile(@exclude_host_regex).match(k8s_metadata[:source_host]) and not include
return nil
end
end
sanitize_pod_name(k8s_metadata)
if annotations["sumologic.com/exclude"] == "true"
return nil
end
sumo_metadata[:log_format] = annotations["sumologic.com/format"] if annotations["sumologic.com/format"]
if annotations["sumologic.com/sourceHost"].nil?
sumo_metadata[:host] = sumo_metadata[:host] % k8s_metadata
else
sumo_metadata[:host] = annotations["sumologic.com/sourceHost"] % k8s_metadata
end
if annotations["sumologic.com/sourceName"].nil?
sumo_metadata[:source] = sumo_metadata[:source] % k8s_metadata
else
sumo_metadata[:source] = annotations["sumologic.com/sourceName"] % k8s_metadata
end
if annotations["sumologic.com/sourceCategory"].nil?
sumo_metadata[:category] = sumo_metadata[:category] % k8s_metadata
else
sumo_metadata[:category] = (annotations["sumologic.com/sourceCategory"] % k8s_metadata).prepend(@source_category_prefix)
end
sumo_metadata[:category].gsub!("-", @source_category_replace_dash)
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
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?
sumo_metadata[:fields] = log_fields.map{|k,v| "#{k}=#{v}"}.join(',')
record.delete("docker")
record.delete("kubernetes")
end
record
end
|