lib/fluent/plugin/out_kubernetes.rb in fluent-plugin-kubernetes-0.3.0 vs lib/fluent/plugin/out_kubernetes.rb in fluent-plugin-kubernetes-0.3.1
- old
+ new
@@ -14,12 +14,10 @@
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
-require 'open3'
-
class Fluent::KubernetesOutput < Fluent::Output
Fluent::Plugin.register_output('kubernetes', self)
config_param :container_id, :string
config_param :tag, :string
@@ -53,37 +51,49 @@
str.gsub(/\$\{tag_parts\[(\d+)\]\}/) { |m| tag_parts[$1.to_i] }
end
def enrich_record(tag, record)
- if @container_id
+ id = interpolate(tag, @container_id)
+ if !id.empty?
+ record['container_id'] = id
+ record = enrich_container_data(id, record)
+ record = merge_json_log(record)
+ end
+ record
+ end
+
+ def enrich_container_data(id, record)
+ container = Docker::Container.get(id)
+ if container
+ container_name = container.json['Name']
+ if container_name
+ record["container_name"] = container_name[1..-1] if container_name[0] == '/'
+ regex = Regexp.new(@kubernetes_pod_regex)
+ match = container_name.match(regex)
+ if match
+ pod_container_name, pod_name, pod_namespace =
+ match.captures
+ record["pod_namespace"] = pod_namespace
+ record["pod"] = pod_name
+ record["pod_container"] = pod_container_name
+ end
+ end
+ end
+ record
+ end
+
+ def merge_json_log(record)
+ if record.has_key?('log')
log = record['log'].strip
if log[0].eql?('{') && log[-1].eql?('}')
begin
parsed_log = JSON.parse(log)
record = record.merge(parsed_log)
unless parsed_log.has_key?('log')
record.delete('log')
end
rescue JSON::ParserError
- end
- end
- id = interpolate(tag, @container_id)
- record['container_id'] = id
- container = Docker::Container.get(id)
- if container
- container_name = container.json['Name']
- if container_name
- record["container_name"] = container_name[1..-1]
- regex = Regexp.new(@kubernetes_pod_regex)
- match = container_name.match(regex)
- if match
- pod_container_name, pod_name, pod_namespace =
- match.captures
- record["pod_namespace"] = pod_namespace
- record["pod"] = pod_name
- record["pod_container"] = pod_container_name
- end
end
end
end
record
end