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 uptime metric for nodes, pods, and containers #25867

Merged
merged 4 commits into from
Aug 18, 2023
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/kubeletstats-uptime.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 a new `uptime` metric for nodes, pods, and containers to track how many seconds have passed since the object started

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

# (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: []
34 changes: 34 additions & 0 deletions receiver/kubeletstatsreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,40 @@ The inodes used by the filesystem. This may not equal inodes - free because file
| ---- | ----------- | ---------- |
| 1 | Gauge | Int |
## Optional Metrics
The following metrics are not emitted by default. Each of them can be enabled by applying the following configuration:
```yaml
metrics:
<metric_name>:
enabled: true
```
### container.uptime
The time since the container started
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
### k8s.node.uptime
The time since the node started
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
### k8s.pod.uptime
The time since the pod started
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| s | Sum | Int | Cumulative | true |
## Resource Attributes
| Name | Description | Values | Enabled |
Expand Down
11 changes: 11 additions & 0 deletions receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/metadata"
Expand Down Expand Up @@ -41,12 +42,20 @@ type metricDataAccumulator struct {
mbs *metadata.MetricsBuilders
}

func addUptimeMetric(mb *metadata.MetricsBuilder, uptimeMetric metadata.RecordIntDataPointFunc, startTime v1.Time, currentTime pcommon.Timestamp) {
if !startTime.IsZero() {
value := int64(time.Since(startTime.Time).Seconds())
uptimeMetric(mb, currentTime, value)
}
}

func (a *metricDataAccumulator) nodeStats(s stats.NodeStats) {
if !a.metricGroupsToCollect[NodeMetricGroup] {
return
}

currentTime := pcommon.NewTimestampFromTime(a.time)
addUptimeMetric(a.mbs.NodeMetricsBuilder, metadata.NodeUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeCPUMetrics, s.CPU, currentTime)
addMemoryMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeMemoryMetrics, s.Memory, currentTime)
addFilesystemMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeFilesystemMetrics, s.Fs, currentTime)
Expand All @@ -66,6 +75,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)
addMemoryMetrics(a.mbs.PodMetricsBuilder, metadata.PodMemoryMetrics, s.Memory, currentTime)
addFilesystemMetrics(a.mbs.PodMetricsBuilder, metadata.PodFilesystemMetrics, s.EphemeralStorage, currentTime)
Expand Down Expand Up @@ -98,6 +108,7 @@ func (a *metricDataAccumulator) containerStats(sPod stats.PodStats, s stats.Cont
}

currentTime := pcommon.NewTimestampFromTime(a.time)
addUptimeMetric(a.mbs.ContainerMetricsBuilder, metadata.ContainerUptimeMetrics.Uptime, s.StartTime, currentTime)
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime)
addMemoryMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerMemoryMetrics, s.Memory, currentTime)
addFilesystemMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerFilesystemMetrics, s.Rootfs, currentTime)
Expand Down
39 changes: 33 additions & 6 deletions receiver/kubeletstatsreceiver/internal/kubelet/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func requireResourceOk(t *testing.T, resource pcommon.Resource) {
}

func TestWorkingSetMem(t *testing.T) {
metrics := indexedFakeMetrics()
metrics := indexedFakeMetrics(fakeMetrics())
requireContains(t, metrics, "k8s.pod.memory.working_set")
requireContains(t, metrics, "container.memory.working_set")

Expand All @@ -115,7 +115,7 @@ func TestWorkingSetMem(t *testing.T) {
}

func TestPageFaults(t *testing.T) {
metrics := indexedFakeMetrics()
metrics := indexedFakeMetrics(fakeMetrics())
requireContains(t, metrics, "k8s.pod.memory.page_faults")
requireContains(t, metrics, "container.memory.page_faults")

Expand All @@ -125,7 +125,7 @@ func TestPageFaults(t *testing.T) {
}

func TestMajorPageFaults(t *testing.T) {
metrics := indexedFakeMetrics()
metrics := indexedFakeMetrics(fakeMetrics())
requireContains(t, metrics, "k8s.pod.memory.major_page_faults")
requireContains(t, metrics, "container.memory.major_page_faults")

Expand All @@ -134,8 +134,36 @@ func TestMajorPageFaults(t *testing.T) {
require.Equal(t, int64(12), value)
}

func TestUptime(t *testing.T) {
rc := &fakeRestClient{}
statsProvider := NewStatsProvider(rc)
summary, _ := statsProvider.StatsSummary()
mgs := map[MetricGroup]bool{
ContainerMetricGroup: true,
PodMetricGroup: true,
NodeMetricGroup: true,
}

cfg := metadata.DefaultMetricsBuilderConfig()
cfg.Metrics.K8sNodeUptime.Enabled = true
cfg.Metrics.K8sPodUptime.Enabled = true
cfg.Metrics.ContainerUptime.Enabled = true

mbs := &metadata.MetricsBuilders{
NodeMetricsBuilder: metadata.NewMetricsBuilder(cfg, receivertest.NewNopCreateSettings()),
PodMetricsBuilder: metadata.NewMetricsBuilder(cfg, receivertest.NewNopCreateSettings()),
ContainerMetricsBuilder: metadata.NewMetricsBuilder(cfg, receivertest.NewNopCreateSettings()),
}

metrics := indexedFakeMetrics(MetricsData(zap.NewNop(), summary, Metadata{}, mgs, mbs))

requireContains(t, metrics, "k8s.node.uptime")
requireContains(t, metrics, "k8s.pod.uptime")
requireContains(t, metrics, "container.uptime")
}

func TestEmitMetrics(t *testing.T) {
metrics := indexedFakeMetrics()
metrics := indexedFakeMetrics(fakeMetrics())
metricNames := []string{
"k8s.node.network.io",
"k8s.node.network.errors",
Expand All @@ -158,8 +186,7 @@ func requireContains(t *testing.T, metrics map[string][]pmetric.Metric, metricNa
require.True(t, found)
}

func indexedFakeMetrics() map[string][]pmetric.Metric {
mds := fakeMetrics()
func indexedFakeMetrics(mds []pmetric.Metrics) map[string][]pmetric.Metric {
metrics := make(map[string][]pmetric.Metric)
for _, md := range mds {
for i := 0; i < md.ResourceMetrics().Len(); i++ {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading