Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions lib/fluent/plugin/in_kubernetes_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -602,33 +602,34 @@ def emit_stats_metrics(metrics)
emit_stats_breakdown(metrics['stats']) unless metrics['stats'].nil?
end

# Make sure regex has only one capturing group
def grep_using_regex(metric, regex)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved field parsing to this function to improve readability

match = metric.match(regex)
return nil if match.nil?
match[1]
end

def emit_cadvisor_metrics(metrics)
metrics = metrics.split("\n")
metrics.each do |metric|
next unless metric.include? 'container_name='

next unless metric.match(/^((?!container_name="").)*$/) && metric[0] != '#'


next if metric[0] == '#' or not container_name = grep_using_regex(metric, /container(?:_name)?="([^"]*)"/)

metric_str, metric_val = metric.split(' ')
metric_val = metric_val.to_f if metric_val.is_a? String
first_occur = metric_str.index('{')
metric_name = metric_str[0..first_occur - 1]
pod_name = metric.match(/pod_name="\S*"/).to_s
pod_name = pod_name.split('"')[1]
image_name = metric.match(/image="\S*"/).to_s
image_name = image_name.split('"')[1]
namespace = metric.match(/namespace="\S*"/).to_s
namespace = namespace.split('"')[1]
pod_name = grep_using_regex(metric, /pod(?:_name)?="([^"]*)"/).to_s
image_name = grep_using_regex(metric, /image="([^"]*)"/).to_s
namespace = grep_using_regex(metric, /namespace="([^"]*)"/).to_s
metric_labels = { 'pod_name' => pod_name, 'image' => image_name, 'namespace' => namespace, 'value' => metric_val, 'node' => @node_name }
if metric =~ /^((?!container_name="POD").)*$/
if container_name=="POD"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regex /^((?!container_name="POD").)*$/ wan't working as expected. So made this change.

tag = 'pod'
tag = generate_tag("#{tag}#{metric_name.tr('_', '.')}")
tag = tag.gsub('container', '')
else
container_name = metric.match(/container_name="\S*"/).to_s
container_name = container_name.split('"')[1]
container_label = { 'container_name' => container_name }
metric_labels.merge(container_label)
metric_labels.merge!(container_label)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge method will return a new object. So changed it to merge! which will update the object itself

tag = generate_tag(metric_name.tr('_', '.').to_s)
end
router.emit tag, @scraped_at_cadvisor, metric_labels
Expand Down
25 changes: 12 additions & 13 deletions test/plugin/test_in_kubernetes_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
).freeze

setup do
stub_k8s_requests

return unless @@hash_map_test.empty?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test setup was called on every testcase execution and took too much time executing unit test

Fluent::Test.setup

@@parsed_unit_string = JSON.parse(get_unit_parsed_string)
Expand All @@ -45,8 +48,6 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
get_cadvisor_parsed_string = f.read
end.close

stub_k8s_requests

@@ca_driver = create_driver
@@ca_driver.run timeout: 20, expect_emits: 1, shutdown: true

Expand All @@ -56,8 +57,10 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
metrics = get_cadvisor_parsed_string.split("\n")
metrics.each do |metric|
next unless metric.include? 'container_name='
next unless metric[0] != '#'

next unless metric.match(/^((?!container_name="").)*$/) && metric[0] != '#'
container_name = metric.match(/container_name="\S*"/).to_s
container_name = container_name.split('"')[1]

metric_str, metric_val = metric.split(' ')
metric_val = metric_val.to_f if metric_val.is_a? String
Expand All @@ -70,13 +73,11 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
namespace = metric.match(/namespace="\S*"/).to_s
namespace = namespace.split('"')[1]
metric_labels = { 'pod_name' => pod_name, 'image' => image_name, 'namespace' => namespace, 'value' => metric_val, 'node' => @node_name }
if metric =~ /^((?!container_name="POD").)*$/
if container_name == 'POD'
tag = 'pod'
tag = generate_tag("#{tag}#{metric_name.tr('_', '.')}", @@driver.instance.tag)
tag = tag.gsub('container', '')
else
container_name = metric.match(/container_name="\S*"/).to_s
container_name = container_name.split('"')[1]
container_label = { 'container_name' => container_name }
metric_labels.merge(container_label)
tag = generate_tag(metric_name.tr('_', '.').to_s, @@driver.instance.tag)
Expand Down Expand Up @@ -252,11 +253,10 @@ def create_driver(conf = CONFIG)
assert_true @@hash_map_cadvisor.key?('kube.container.fs.read.seconds.total')
assert_equal @@hash_map_cadvisor['kube.container.fs.read.seconds.total'], @@hash_map_test['kube.container.fs.read.seconds.total'][2]['value']
end

# TODO: Current Test does not work - metric present in metrics_cadvisor.txt but not being parsed by connector in test/working in production

test 'Test - metrics cadvisor: container_fs_reads_bytes_total' do
assert_false @@hash_map_cadvisor.key?('kube.container.fs.reads.bytes.total')
# assert_equal @@hash_map_cadvisor['kube.container.fs.reads.bytes.total'], @@hash_map_test["kube.container.fs.reads.bytes.total"][2]["value"]
assert_true @@hash_map_cadvisor.key?('kube.container.fs.reads.bytes.total')
assert_equal @@hash_map_cadvisor['kube.container.fs.reads.bytes.total'], @@hash_map_test["kube.container.fs.reads.bytes.total"][2]["value"]
end

test 'Test - metrics cadvisor: container_fs_reads_merged_total' do
Expand Down Expand Up @@ -289,10 +289,9 @@ def create_driver(conf = CONFIG)
assert_equal @@hash_map_cadvisor['kube.container.fs.write.seconds.total'], @@hash_map_test['kube.container.fs.write.seconds.total'][2]['value']
end

# TODO: Current Test does not work - metric present in metrics_cadvisor.txt but not being parsed by connector in test/working in production
test 'Test - metrics cadvisor: container_fs_writes_bytes_total' do
assert_false @@hash_map_cadvisor.key?('kube.container.fs.writes.bytes.total')
# assert_equal @@hash_map_cadvisor['kube.container.fs.writes.bytes.total'], @@hash_map_test["kube.container.fs.writes.bytes.total"][2]["value"]
assert_true @@hash_map_cadvisor.key?('kube.container.fs.writes.bytes.total')
assert_equal @@hash_map_cadvisor['kube.container.fs.writes.bytes.total'], @@hash_map_test["kube.container.fs.writes.bytes.total"][2]["value"]
end

test 'Test - metrics cadvisor: container_fs_writes_merged_total' do
Expand Down