Skip to content
This repository was archived by the owner on Sep 26, 2025. It is now read-only.

Commit 1c6ee72

Browse files
authored
updated emit_cadvisor_metrics (#103)
* updated emit_cadvisor_metrics
1 parent 65710ae commit 1c6ee72

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

lib/fluent/plugin/in_kubernetes_metrics.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -602,33 +602,34 @@ def emit_stats_metrics(metrics)
602602
emit_stats_breakdown(metrics['stats']) unless metrics['stats'].nil?
603603
end
604604

605+
# Make sure regex has only one capturing group
606+
def grep_using_regex(metric, regex)
607+
match = metric.match(regex)
608+
return nil if match.nil?
609+
match[1]
610+
end
611+
605612
def emit_cadvisor_metrics(metrics)
606613
metrics = metrics.split("\n")
607614
metrics.each do |metric|
608-
next unless metric.include? 'container_name='
609-
610-
next unless metric.match(/^((?!container_name="").)*$/) && metric[0] != '#'
611-
615+
616+
next if metric[0] == '#' or not container_name = grep_using_regex(metric, /container(?:_name)?="([^"]*)"/)
617+
612618
metric_str, metric_val = metric.split(' ')
613619
metric_val = metric_val.to_f if metric_val.is_a? String
614620
first_occur = metric_str.index('{')
615621
metric_name = metric_str[0..first_occur - 1]
616-
pod_name = metric.match(/pod_name="\S*"/).to_s
617-
pod_name = pod_name.split('"')[1]
618-
image_name = metric.match(/image="\S*"/).to_s
619-
image_name = image_name.split('"')[1]
620-
namespace = metric.match(/namespace="\S*"/).to_s
621-
namespace = namespace.split('"')[1]
622+
pod_name = grep_using_regex(metric, /pod(?:_name)?="([^"]*)"/).to_s
623+
image_name = grep_using_regex(metric, /image="([^"]*)"/).to_s
624+
namespace = grep_using_regex(metric, /namespace="([^"]*)"/).to_s
622625
metric_labels = { 'pod_name' => pod_name, 'image' => image_name, 'namespace' => namespace, 'value' => metric_val, 'node' => @node_name }
623-
if metric =~ /^((?!container_name="POD").)*$/
626+
if container_name=="POD"
624627
tag = 'pod'
625628
tag = generate_tag("#{tag}#{metric_name.tr('_', '.')}")
626629
tag = tag.gsub('container', '')
627630
else
628-
container_name = metric.match(/container_name="\S*"/).to_s
629-
container_name = container_name.split('"')[1]
630631
container_label = { 'container_name' => container_name }
631-
metric_labels.merge(container_label)
632+
metric_labels.merge!(container_label)
632633
tag = generate_tag(metric_name.tr('_', '.').to_s)
633634
end
634635
router.emit tag, @scraped_at_cadvisor, metric_labels

test/plugin/test_in_kubernetes_metrics.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
3535
).freeze
3636

3737
setup do
38+
stub_k8s_requests
39+
40+
return unless @@hash_map_test.empty?
3841
Fluent::Test.setup
3942

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

48-
stub_k8s_requests
49-
5051
@@ca_driver = create_driver
5152
@@ca_driver.run timeout: 20, expect_emits: 1, shutdown: true
5253

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

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

6265
metric_str, metric_val = metric.split(' ')
6366
metric_val = metric_val.to_f if metric_val.is_a? String
@@ -70,13 +73,11 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase
7073
namespace = metric.match(/namespace="\S*"/).to_s
7174
namespace = namespace.split('"')[1]
7275
metric_labels = { 'pod_name' => pod_name, 'image' => image_name, 'namespace' => namespace, 'value' => metric_val, 'node' => @node_name }
73-
if metric =~ /^((?!container_name="POD").)*$/
76+
if container_name == 'POD'
7477
tag = 'pod'
7578
tag = generate_tag("#{tag}#{metric_name.tr('_', '.')}", @@driver.instance.tag)
7679
tag = tag.gsub('container', '')
7780
else
78-
container_name = metric.match(/container_name="\S*"/).to_s
79-
container_name = container_name.split('"')[1]
8081
container_label = { 'container_name' => container_name }
8182
metric_labels.merge(container_label)
8283
tag = generate_tag(metric_name.tr('_', '.').to_s, @@driver.instance.tag)
@@ -252,11 +253,10 @@ def create_driver(conf = CONFIG)
252253
assert_true @@hash_map_cadvisor.key?('kube.container.fs.read.seconds.total')
253254
assert_equal @@hash_map_cadvisor['kube.container.fs.read.seconds.total'], @@hash_map_test['kube.container.fs.read.seconds.total'][2]['value']
254255
end
255-
256-
# TODO: Current Test does not work - metric present in metrics_cadvisor.txt but not being parsed by connector in test/working in production
256+
257257
test 'Test - metrics cadvisor: container_fs_reads_bytes_total' do
258-
assert_false @@hash_map_cadvisor.key?('kube.container.fs.reads.bytes.total')
259-
# assert_equal @@hash_map_cadvisor['kube.container.fs.reads.bytes.total'], @@hash_map_test["kube.container.fs.reads.bytes.total"][2]["value"]
258+
assert_true @@hash_map_cadvisor.key?('kube.container.fs.reads.bytes.total')
259+
assert_equal @@hash_map_cadvisor['kube.container.fs.reads.bytes.total'], @@hash_map_test["kube.container.fs.reads.bytes.total"][2]["value"]
260260
end
261261

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

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

298297
test 'Test - metrics cadvisor: container_fs_writes_merged_total' do

0 commit comments

Comments
 (0)