This repository was archived by the owner on Sep 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
updated emit_cadvisor_metrics #103
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| 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" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regex |
||
| 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) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| tag = generate_tag(metric_name.tr('_', '.').to_s) | ||
| end | ||
| router.emit tag, @scraped_at_cadvisor, metric_labels | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,9 @@ class KubernetesMetricsInputTest < Test::Unit::TestCase | |
| ).freeze | ||
|
|
||
| setup do | ||
| stub_k8s_requests | ||
|
|
||
| return unless @@hash_map_test.empty? | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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) | ||
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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