@@ -32,21 +32,21 @@ def is_number?(string)
3232 end
3333
3434 def sanitize_pod_name ( k8s_metadata )
35- # Strip out dynamic bits from pod name.
36- # NOTE: Kubernetes deployments append a template hash.
37- # At the moment this can be in 3 different forms:
38- # 1) pre-1.8: numeric in pod_template_hash and pod_parts[-2]
39- # 2) 1.8-1.11: numeric in pod_template_hash, hash in pod_parts[-2]
40- # 3) post-1.11: hash in pod_template_hash and pod_parts[-2]
41-
42- pod_parts = k8s_metadata [ :pod ] . split ( "-" )
43- pod_template_hash = k8s_metadata [ :"label:pod-template-hash" ]
44- if ( pod_template_hash == pod_parts [ -2 ] ||
45- to_hash ( pod_template_hash ) == pod_parts [ -2 ] )
46- k8s_metadata [ :pod_name ] = pod_parts [ 0 ..-3 ] . join ( "-" )
47- else
48- k8s_metadata [ :pod_name ] = pod_parts [ 0 ..-2 ] . join ( "-" )
49- end
35+ # Strip out dynamic bits from pod name.
36+ # NOTE: Kubernetes deployments append a template hash.
37+ # At the moment this can be in 3 different forms:
38+ # 1) pre-1.8: numeric in pod_template_hash and pod_parts[-2]
39+ # 2) 1.8-1.11: numeric in pod_template_hash, hash in pod_parts[-2]
40+ # 3) post-1.11: hash in pod_template_hash and pod_parts[-2]
41+
42+ pod_parts = k8s_metadata [ :pod ] . split ( "-" )
43+ pod_template_hash = k8s_metadata [ :"label:pod-template-hash" ]
44+ if ( pod_template_hash == pod_parts [ -2 ] ||
45+ to_hash ( pod_template_hash ) == pod_parts [ -2 ] )
46+ k8s_metadata [ :pod_name ] = pod_parts [ 0 ..-3 ] . join ( "-" )
47+ else
48+ k8s_metadata [ :pod_name ] = pod_parts [ 0 ..-2 ] . join ( "-" )
49+ end
5050 end
5151
5252 def to_hash ( pod_template_hash )
@@ -60,6 +60,7 @@ def filter(tag, time, record)
6060 # Set the sumo metadata fields
6161 sumo_metadata = record [ "_sumo_metadata" ] || { }
6262 record [ "_sumo_metadata" ] = sumo_metadata
63+ log_fields = { }
6364 sumo_metadata [ :log_format ] = @log_format
6465 sumo_metadata [ :host ] = @source_host if @source_host
6566 sumo_metadata [ :source ] = @source_name if @source_name
@@ -102,17 +103,20 @@ def filter(tag, time, record)
102103 # Clone kubernetes hash so we don't override the cache
103104 kubernetes = record [ "kubernetes" ] . clone
104105 k8s_metadata = {
105- :namespace => kubernetes [ "namespace_name" ] ,
106- :pod => kubernetes [ "pod_name" ] ,
107- :pod_id => kubernetes [ 'pod_id' ] ,
108- :container => kubernetes [ "container_name" ] ,
109- :source_host => kubernetes [ "host" ] ,
106+ :namespace => kubernetes [ "namespace_name" ] ,
107+ :pod => kubernetes [ "pod_name" ] ,
108+ :pod_id => kubernetes [ 'pod_id' ] ,
109+ :container => kubernetes [ "container_name" ] ,
110+ :source_host => kubernetes [ "host" ] ,
110111 }
111112
112113
113114 if kubernetes . has_key? "labels"
114115 kubernetes [ "labels" ] . each { |k , v | k8s_metadata [ "label:#{ k } " . to_sym ] = v }
115116 end
117+ if kubernetes . has_key? "namespace_labels"
118+ kubernetes [ "namespace_labels" ] . each { |k , v | k8s_metadata [ "namespace_label:#{ k } " . to_sym ] = v }
119+ end
116120 k8s_metadata . default = "undefined"
117121
118122 annotations = kubernetes . fetch ( "annotations" , { } )
@@ -183,6 +187,7 @@ def filter(tag, time, record)
183187 record [ "kubernetes" ] . delete ( "pod_id" )
184188 record [ "kubernetes" ] . delete ( "namespace_id" )
185189 record [ "kubernetes" ] . delete ( "labels" )
190+ record [ "kubernetes" ] . delete ( "namespace_labels" )
186191 record [ "kubernetes" ] . delete ( "master_url" )
187192 record [ "kubernetes" ] . delete ( "annotations" )
188193 end
@@ -194,8 +199,34 @@ def filter(tag, time, record)
194199 end
195200 # Strip sumologic.com annotations
196201 kubernetes . delete ( "annotations" ) if annotations
202+
203+ if @log_format == "fields" and record . key? ( "docker" ) and not record . fetch ( "docker" ) . nil?
204+ record [ "docker" ] . each { |k , v | log_fields [ k ] = v }
205+ end
206+
207+ if @log_format == "fields" and record . key? ( "kubernetes" ) and not record . fetch ( "kubernetes" ) . nil?
208+ if kubernetes . has_key? "labels"
209+ kubernetes [ "labels" ] . each { |k , v | log_fields [ "pod_labels_#{ k } " . to_sym ] = v }
210+ end
211+ if kubernetes . has_key? "namespace_labels"
212+ kubernetes [ "namespace_labels" ] . each { |k , v | log_fields [ "namespace_labels_#{ k } " . to_sym ] = v }
213+ end
214+ log_fields [ "container" ] = kubernetes [ "container_name" ] unless kubernetes [ "container_name" ] . nil?
215+ log_fields [ "namespace" ] = kubernetes [ "namespace_name" ] unless kubernetes [ "namespace_name" ] . nil?
216+ log_fields [ "pod" ] = kubernetes [ "pod_name" ] unless kubernetes [ "pod_name" ] . nil?
217+ log_fields [ "pod_id" ] = kubernetes [ "pod_id" ] unless kubernetes [ "pod_id" ] . nil?
218+ log_fields [ "host" ] = kubernetes [ "host" ] unless kubernetes [ "host" ] . nil?
219+ log_fields [ "master_url" ] = kubernetes [ "master_url" ] unless kubernetes [ "master_url" ] . nil?
220+ log_fields [ "namespace_id" ] = kubernetes [ "namespace_id" ] unless kubernetes [ "namespace_id" ] . nil?
221+ end
222+ end
223+
224+ if @log_format == "fields" and not log_fields . nil?
225+ sumo_metadata [ :fields ] = log_fields . map { |k , v | "#{ k } =#{ v } " } . join ( ',' )
226+ record . delete ( "docker" )
227+ record . delete ( "kubernetes" )
197228 end
198229 record
199230 end
200231 end
201- end
232+ end
0 commit comments