From fc872adc8d8ad1e835da444a3f3f9b57eedf6dd5 Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Fri, 22 Mar 2024 10:39:28 -0600 Subject: [PATCH] feat(inputs.kubernetes): Add option to node metric name The k8s and kube_node plugins produce the same metric name that can conflict. Provide an option in the k8s plugin to change the name. fixes: #9451 --- plugins/inputs/kubernetes/README.md | 6 ++++ plugins/inputs/kubernetes/kubernetes.go | 36 +++++++++----------- plugins/inputs/kubernetes/kubernetes_test.go | 7 ++-- plugins/inputs/kubernetes/sample.conf | 6 ++++ 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/plugins/inputs/kubernetes/README.md b/plugins/inputs/kubernetes/README.md index cea24e14a2eda..fedb7bd7b159c 100644 --- a/plugins/inputs/kubernetes/README.md +++ b/plugins/inputs/kubernetes/README.md @@ -62,6 +62,12 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. ## OR # bearer_token_string = "abc_123" + ## Kubernetes Node Metric Name + ## The default Kubernetes node metric name (i.e. kubernetes_node) is the same + ## for the kubernetes and kube_inventory plugins. To avoid conflicts, set this + ## option to a different value. + # node_metric_name = "kubernetes_node" + ## Pod labels to be added as tags. An empty array for both include and ## exclude will include all labels. # label_include = [] diff --git a/plugins/inputs/kubernetes/kubernetes.go b/plugins/inputs/kubernetes/kubernetes.go index d2ca823cfdf23..ffa3cce68214b 100644 --- a/plugins/inputs/kubernetes/kubernetes.go +++ b/plugins/inputs/kubernetes/kubernetes.go @@ -29,25 +29,19 @@ var sampleConfig string // Kubernetes represents the config object for the plugin type Kubernetes struct { - URL string - - // Bearer Token authorization file path - BearerToken string `toml:"bearer_token"` - BearerTokenString string `toml:"bearer_token_string" deprecated:"1.24.0;use 'BearerToken' with a file instead"` - - LabelInclude []string `toml:"label_include"` - LabelExclude []string `toml:"label_exclude"` - - labelFilter filter.Filter - - // HTTP Timeout specified as a string - 3s, 1m, 1h - ResponseTimeout config.Duration + URL string `toml:"url"` + BearerToken string `toml:"bearer_token"` + BearerTokenString string `toml:"bearer_token_string" deprecated:"1.24.0;use 'BearerToken' with a file instead"` + NodeMetricName string `toml:"node_metric_name"` + LabelInclude []string `toml:"label_include"` + LabelExclude []string `toml:"label_exclude"` + ResponseTimeout config.Duration `toml:"response_timeout"` + Log telegraf.Logger `toml:"-"` tls.ClientConfig - Log telegraf.Logger `toml:"-"` - - httpClient *http.Client + labelFilter filter.Filter + httpClient *http.Client } const ( @@ -83,6 +77,10 @@ func (k *Kubernetes) Init() error { k.InsecureSkipVerify = true } + if k.NodeMetricName == "" { + k.NodeMetricName = "kubernetes_node" + } + return nil } @@ -169,7 +167,7 @@ func (k *Kubernetes) gatherSummary(baseURL string, acc telegraf.Accumulator) err return err } buildSystemContainerMetrics(summaryMetrics, acc) - buildNodeMetrics(summaryMetrics, acc) + buildNodeMetrics(summaryMetrics, acc, k.NodeMetricName) buildPodMetrics(summaryMetrics, podInfos, k.labelFilter, acc) return nil } @@ -196,7 +194,7 @@ func buildSystemContainerMetrics(summaryMetrics *SummaryMetrics, acc telegraf.Ac } } -func buildNodeMetrics(summaryMetrics *SummaryMetrics, acc telegraf.Accumulator) { +func buildNodeMetrics(summaryMetrics *SummaryMetrics, acc telegraf.Accumulator, metricName string) { tags := map[string]string{ "node_name": summaryMetrics.Node.NodeName, } @@ -219,7 +217,7 @@ func buildNodeMetrics(summaryMetrics *SummaryMetrics, acc telegraf.Accumulator) fields["runtime_image_fs_available_bytes"] = summaryMetrics.Node.Runtime.ImageFileSystem.AvailableBytes fields["runtime_image_fs_capacity_bytes"] = summaryMetrics.Node.Runtime.ImageFileSystem.CapacityBytes fields["runtime_image_fs_used_bytes"] = summaryMetrics.Node.Runtime.ImageFileSystem.UsedBytes - acc.AddFields("kubernetes_node", fields, tags) + acc.AddFields(metricName, fields, tags) } func (k *Kubernetes) gatherPodInfo(baseURL string) ([]Item, error) { diff --git a/plugins/inputs/kubernetes/kubernetes_test.go b/plugins/inputs/kubernetes/kubernetes_test.go index 864905448780d..0dbfae191f393 100644 --- a/plugins/inputs/kubernetes/kubernetes_test.go +++ b/plugins/inputs/kubernetes/kubernetes_test.go @@ -2,11 +2,11 @@ package kubernetes import ( "fmt" - "github.com/influxdata/telegraf/filter" "net/http" "net/http/httptest" "testing" + "github.com/influxdata/telegraf/filter" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) @@ -29,8 +29,9 @@ func TestKubernetesStats(t *testing.T) { labelFilter, _ := filter.NewIncludeExcludeFilter([]string{"app", "superkey"}, nil) k := &Kubernetes{ - URL: ts.URL, - labelFilter: labelFilter, + URL: ts.URL, + labelFilter: labelFilter, + NodeMetricName: "kubernetes_node", } var acc testutil.Accumulator diff --git a/plugins/inputs/kubernetes/sample.conf b/plugins/inputs/kubernetes/sample.conf index a1d6b4a4d8c9f..c4962d6caeedd 100644 --- a/plugins/inputs/kubernetes/sample.conf +++ b/plugins/inputs/kubernetes/sample.conf @@ -14,6 +14,12 @@ ## OR # bearer_token_string = "abc_123" + ## Kubernetes Node Metric Name + ## The default Kubernetes node metric name (i.e. kubernetes_node) is the same + ## for the kubernetes and kube_inventory plugins. To avoid conflicts, set this + ## option to a different value. + # node_metric_name = "kubernetes_node" + ## Pod labels to be added as tags. An empty array for both include and ## exclude will include all labels. # label_include = []