feat(inputs.kubernetes): Add option to node metric name (#15049)

This commit is contained in:
Joshua Powers 2024-03-25 06:36:44 -06:00 committed by GitHub
parent c7466b8835
commit 6e8e7e5c15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 22 deletions

View File

@ -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 = []

View File

@ -29,24 +29,18 @@ var sampleConfig string
// Kubernetes represents the config object for the plugin
type Kubernetes struct {
URL string
// Bearer Token authorization file path
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"`
labelFilter filter.Filter
// HTTP Timeout specified as a string - 3s, 1m, 1h
ResponseTimeout config.Duration
ResponseTimeout config.Duration `toml:"response_timeout"`
Log telegraf.Logger `toml:"-"`
tls.ClientConfig
Log telegraf.Logger `toml:"-"`
labelFilter filter.Filter
httpClient *http.Client
}
@ -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) {

View File

@ -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"
)
@ -31,6 +31,7 @@ func TestKubernetesStats(t *testing.T) {
k := &Kubernetes{
URL: ts.URL,
labelFilter: labelFilter,
NodeMetricName: "kubernetes_node",
}
var acc testutil.Accumulator

View File

@ -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 = []