diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..22d0d82f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vendor diff --git a/fluent-plugin-enhance-k8s-metadata/lib/fluent/plugin/filter_enhance_k8s_metadata.rb b/fluent-plugin-enhance-k8s-metadata/lib/fluent/plugin/filter_enhance_k8s_metadata.rb index ff581599..24fe34be 100644 --- a/fluent-plugin-enhance-k8s-metadata/lib/fluent/plugin/filter_enhance_k8s_metadata.rb +++ b/fluent-plugin-enhance-k8s-metadata/lib/fluent/plugin/filter_enhance_k8s_metadata.rb @@ -86,7 +86,7 @@ def decorate_record(record) service = @pods_to_services[pod_name] unless @pods_to_services.nil? metadata['service'] = {'service' => service.sort!.join('_')} if !(service.nil? || service.empty?) - if @data_type == 'metrics' && (record['node'].nil? || record['node'] == "") + if @data_type == 'metrics' && (record['node'].nil? || record['node'] == "") && !metadata['node'].nil? record['node'] = metadata['node'] end diff --git a/fluent-plugin-enhance-k8s-metadata/test/plugin/test_filter_enhance_k8s_metadata.rb b/fluent-plugin-enhance-k8s-metadata/test/plugin/test_filter_enhance_k8s_metadata.rb index 60cd544f..b386eb23 100644 --- a/fluent-plugin-enhance-k8s-metadata/test/plugin/test_filter_enhance_k8s_metadata.rb +++ b/fluent-plugin-enhance-k8s-metadata/test/plugin/test_filter_enhance_k8s_metadata.rb @@ -56,11 +56,25 @@ class EnhanceK8sMetadataFilterTest < Test::Unit::TestCase driver = create_driver(conf) input_record = get_test_record[2] - assert_nil input_record['node'] + assert_false input_record.key?('node') record = driver.filter('tag', 'time', input_record) assert_equal 'ip-172-20-62-242.us-west-1.compute.internal', record['node'] end + + test 'do not attach node metadata to metrics when not found' do + conf = %{ + kubernetes_url http://localhost:8080 + data_type metrics + } + driver = create_driver(conf) + + input_record = get_test_record[3] + assert_false input_record.key?('node') + record = driver.filter('tag', 'time', input_record) + + assert_false record.key?('node') + end end private diff --git a/fluent-plugin-enhance-k8s-metadata/test/resources/records.json b/fluent-plugin-enhance-k8s-metadata/test/resources/records.json index 33dd14aa..5590dca9 100644 --- a/fluent-plugin-enhance-k8s-metadata/test/resources/records.json +++ b/fluent-plugin-enhance-k8s-metadata/test/resources/records.json @@ -46,6 +46,10 @@ "@metric": "http_request_size_bytes_sum", "@timestamp": 1550862304339, "@value": 1619905.0 + }, + { + "pod": "nonexistent-pod", + "namespace": "non-exist" } ] -} \ No newline at end of file +}