Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/kubeletstats] Add k8s.container.cpu.node.utilization metric #32295

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/k8s_container_metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: 'enhancement'

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: kubeletstatsreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add k8s.container.cpu.node.utilization metric

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [27885]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
30 changes: 30 additions & 0 deletions receiver/kubeletstatsreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,36 @@ receivers:
- pod
```

### Collect k8s.container.cpu.node.utilization as ratio of total node's capacity

In order to calculate the `k8s.container.cpu.node.utilization` metric, the information of the node's capacity
must be retrieved from the k8s API. In this, the `k8s_api_config` needs to be set.
In addition, the node name must be identified properly. The `K8S_NODE_NAME` env var can be set using the
downward API inside the collector pod spec as follows:

```yaml
env:
- name: K8S_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
```
Then set `node` value to `${env:K8S_NODE_NAME}` in the receiver's configuration:

```yaml
receivers:
kubeletstats:
collection_interval: 10s
auth_type: 'serviceAccount'
endpoint: '${env:K8S_NODE_NAME}:10250'
node: '${env:K8S_NODE_NAME}'
k8s_api_config:
auth_type: serviceAccount
metrics:
k8s.container.cpu.node.utilization:
enabled: true
```

### Optional parameters

The following parameters can also be specified:
Expand Down
20 changes: 20 additions & 0 deletions receiver/kubeletstatsreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ type Config struct {
// Configuration of the Kubernetes API client.
K8sAPIConfig *k8sconfig.APIConfig `mapstructure:"k8s_api_config"`

// NodeName is the node name to limit the discovery of nodes.
// For example, node name can be set using the downward API inside the collector
// pod spec as follows:
//
// env:
// - name: K8S_NODE_NAME
// valueFrom:
// fieldRef:
// fieldPath: spec.nodeName
//
// Then set this value to ${env:K8S_NODE_NAME} in the configuration.
NodeName string `mapstructure:"node"`

// MetricsBuilderConfig allows customizing scraped metrics/attributes representation.
metadata.MetricsBuilderConfig `mapstructure:",squash"`
}
Expand Down Expand Up @@ -105,3 +118,10 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {

return nil
}

func (cfg *Config) Validate() error {
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
if cfg.Metrics.K8sContainerCPUNodeUtilization.Enabled && cfg.NodeName == "" {
return errors.New("for k8s.container.cpu.node.utilization node setting is required. Check the readme on how to set the required setting")
}
return nil
}
44 changes: 39 additions & 5 deletions receiver/kubeletstatsreceiver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestLoadConfig(t *testing.T) {
duration := 10 * time.Second

tests := []struct {
id component.ID
expected component.Config
expectedErr error
id component.ID
expected component.Config
expectedValidationErr string
}{
{
id: component.NewIDWithName(metadata.Type, "default"),
Expand Down Expand Up @@ -173,6 +173,34 @@ func TestLoadConfig(t *testing.T) {
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
},
},
{
id: component.NewIDWithName(metadata.Type, "container_cpu_node_utilization"),
expected: &Config{
ControllerConfig: scraperhelper.ControllerConfig{
CollectionInterval: duration,
InitialDelay: time.Second,
},
ClientConfig: kube.ClientConfig{
APIConfig: k8sconfig.APIConfig{
AuthType: "tls",
},
},
MetricGroupsToCollect: []kubelet.MetricGroup{
kubelet.ContainerMetricGroup,
kubelet.PodMetricGroup,
kubelet.NodeMetricGroup,
},
MetricsBuilderConfig: metadata.MetricsBuilderConfig{
Metrics: metadata.MetricsConfig{
K8sContainerCPUNodeUtilization: metadata.MetricConfig{
Enabled: true,
},
},
ResourceAttributes: metadata.DefaultResourceAttributesConfig(),
},
},
expectedValidationErr: "for k8s.container.cpu.node.utilization node setting is required. Check the readme on how to set the required setting",
},
}

for _, tt := range tests {
Expand All @@ -184,8 +212,14 @@ func TestLoadConfig(t *testing.T) {
require.NoError(t, err)
require.NoError(t, component.UnmarshalConfig(sub, cfg))

assert.NoError(t, component.ValidateConfig(cfg))
assert.Equal(t, tt.expected, cfg)
err = component.ValidateConfig(cfg)
if tt.expectedValidationErr != "" {
assert.EqualError(t, err, tt.expectedValidationErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, cfg)
}

})
}
}
Expand Down
8 changes: 8 additions & 0 deletions receiver/kubeletstatsreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,14 @@ The time since the container started
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |

### k8s.container.cpu.node.utilization

Container cpu utilization as a ratio of the node's capacity

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| 1 | Gauge | Double |

### k8s.container.cpu_limit_utilization

Container cpu utilization as a ratio of the container's limits
Expand Down
2 changes: 1 addition & 1 deletion receiver/kubeletstatsreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func createMetricsReceiver(
return nil, err
}

scrp, err := newKubletScraper(rest, set, rOptions, cfg.MetricsBuilderConfig)
scrp, err := newKubletScraper(rest, set, rOptions, cfg.MetricsBuilderConfig, cfg.NodeName)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (a *metricDataAccumulator) nodeStats(s stats.NodeStats) {

currentTime := pcommon.NewTimestampFromTime(a.time)
addUptimeMetric(a.mbs.NodeMetricsBuilder, metadata.NodeUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeCPUMetrics, s.CPU, currentTime, resources{})
addCPUMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeCPUMetrics, s.CPU, currentTime, resources{}, 0)
addMemoryMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeMemoryMetrics, s.Memory, currentTime, resources{})
addFilesystemMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeFilesystemMetrics, s.Fs, currentTime)
addNetworkMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeNetworkMetrics, s.Network, currentTime)
Expand All @@ -76,7 +76,7 @@ func (a *metricDataAccumulator) podStats(s stats.PodStats) {

currentTime := pcommon.NewTimestampFromTime(a.time)
addUptimeMetric(a.mbs.PodMetricsBuilder, metadata.PodUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.PodMetricsBuilder, metadata.PodCPUMetrics, s.CPU, currentTime, a.metadata.podResources[s.PodRef.UID])
addCPUMetrics(a.mbs.PodMetricsBuilder, metadata.PodCPUMetrics, s.CPU, currentTime, a.metadata.podResources[s.PodRef.UID], 0)
addMemoryMetrics(a.mbs.PodMetricsBuilder, metadata.PodMemoryMetrics, s.Memory, currentTime, a.metadata.podResources[s.PodRef.UID])
addFilesystemMetrics(a.mbs.PodMetricsBuilder, metadata.PodFilesystemMetrics, s.EphemeralStorage, currentTime)
addNetworkMetrics(a.mbs.PodMetricsBuilder, metadata.PodNetworkMetrics, s.Network, currentTime)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (a *metricDataAccumulator) containerStats(sPod stats.PodStats, s stats.Cont
currentTime := pcommon.NewTimestampFromTime(a.time)
resourceKey := sPod.PodRef.UID + s.Name
addUptimeMetric(a.mbs.ContainerMetricsBuilder, metadata.ContainerUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime, a.metadata.containerResources[resourceKey])
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime, a.metadata.containerResources[resourceKey], a.metadata.cpuNodeLimit)
addMemoryMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerMemoryMetrics, s.Memory, currentTime, a.metadata.containerResources[resourceKey])
addFilesystemMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerFilesystemMetrics, s.Rootfs, currentTime)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestMetadataErrorCases(t *testing.T) {
},
},
},
}, nil),
}, NodeLimits{}, nil),
testScenario: func(acc metricDataAccumulator) {
now := metav1.Now()
podStats := stats.PodStats{
Expand All @@ -79,7 +79,7 @@ func TestMetadataErrorCases(t *testing.T) {
metricGroupsToCollect: map[MetricGroup]bool{
VolumeMetricGroup: true,
},
metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, nil),
metadata: NewMetadata([]MetadataLabel{MetadataLabelVolumeType}, nil, NodeLimits{}, nil),
testScenario: func(acc metricDataAccumulator) {
podStats := stats.PodStats{
PodRef: stats.PodReference{
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestMetadataErrorCases(t *testing.T) {
},
},
},
}, nil),
}, NodeLimits{}, nil),
testScenario: func(acc metricDataAccumulator) {
podStats := stats.PodStats{
PodRef: stats.PodReference{
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestMetadataErrorCases(t *testing.T) {
},
},
},
}, nil),
}, NodeLimits{}, nil),
detailedPVCLabelsSetterOverride: func(*metadata.ResourceBuilder, string, string, string) error {
// Mock failure cases.
return errors.New("")
Expand Down
21 changes: 18 additions & 3 deletions receiver/kubeletstatsreceiver/internal/kubelet/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/metadata"
)

func addCPUMetrics(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources) {
func addCPUMetrics(
mb *metadata.MetricsBuilder,
cpuMetrics metadata.CPUMetrics,
s *stats.CPUStats,
currentTime pcommon.Timestamp,
r resources,
nodeCPULimit float64) {
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
if s == nil {
return
}
addCPUUsageMetric(mb, cpuMetrics, s, currentTime, r)
addCPUUsageMetric(mb, cpuMetrics, s, currentTime, r, nodeCPULimit)
addCPUTimeMetric(mb, cpuMetrics.Time, s, currentTime)
}

func addCPUUsageMetric(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources) {
func addCPUUsageMetric(
mb *metadata.MetricsBuilder,
cpuMetrics metadata.CPUMetrics,
s *stats.CPUStats,
currentTime pcommon.Timestamp,
r resources,
nodeCPULimit float64) {
if s.UsageNanoCores == nil {
return
}
value := float64(*s.UsageNanoCores) / 1_000_000_000
cpuMetrics.Utilization(mb, currentTime, value)
cpuMetrics.Usage(mb, currentTime, value)

if nodeCPULimit > 0 {
cpuMetrics.NodeUtilization(mb, currentTime, value/nodeCPULimit)
}
if r.cpuLimit > 0 {
cpuMetrics.LimitUtilization(mb, currentTime, value/r.cpuLimit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Metadata struct {
DetailedPVCResourceSetter func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error
podResources map[string]resources
containerResources map[string]resources
cpuNodeLimit float64
}

type resources struct {
Expand All @@ -61,6 +62,11 @@ type resources struct {
memoryLimit int64
}

type NodeLimits struct {
Name string
CPUNanoCoresLimit float64
}

func getContainerResources(r *v1.ResourceRequirements) resources {
if r == nil {
return resources{}
Expand All @@ -74,14 +80,15 @@ func getContainerResources(r *v1.ResourceRequirements) resources {
}
}

func NewMetadata(labels []MetadataLabel, podsMetadata *v1.PodList,
func NewMetadata(labels []MetadataLabel, podsMetadata *v1.PodList, nodeResourceLimits NodeLimits,
detailedPVCResourceSetter func(rb *metadata.ResourceBuilder, volCacheID, volumeClaim, namespace string) error) Metadata {
m := Metadata{
Labels: getLabelsMap(labels),
PodsMetadata: podsMetadata,
DetailedPVCResourceSetter: detailedPVCResourceSetter,
podResources: make(map[string]resources),
containerResources: make(map[string]resources),
cpuNodeLimit: nodeResourceLimits.CPUNanoCoresLimit,
}

if podsMetadata != nil {
Expand Down
Loading