Skip to content

Commit

Permalink
feat(inputs.kubernetes): Add option to node metric name
Browse files Browse the repository at this point in the history
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: influxdata#9451
  • Loading branch information
powersj committed Mar 22, 2024
1 parent 4c1aa59 commit fc872ad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
6 changes: 6 additions & 0 deletions plugins/inputs/kubernetes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
36 changes: 17 additions & 19 deletions plugins/inputs/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -83,6 +77,10 @@ func (k *Kubernetes) Init() error {
k.InsecureSkipVerify = true
}

if k.NodeMetricName == "" {
k.NodeMetricName = "kubernetes_node"
}

return nil
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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,
}
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions plugins/inputs/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/kubernetes/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down

0 comments on commit fc872ad

Please sign in to comment.