Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #110880 from yangjunmyfm192085/fixnegativevalue
Browse files Browse the repository at this point in the history
When metrics are counted, discard the wrong container StartTime metrics
  • Loading branch information
k8s-ci-robot committed Jul 15, 2022
2 parents b3057e7 + c71e3a7 commit 6382266
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/kubelet/metrics/collectors/resource_metrics.go
Expand Up @@ -160,7 +160,7 @@ func (rc *resourceMetricsCollector) collectNodeMemoryMetrics(ch chan<- metrics.M
}

func (rc *resourceMetricsCollector) collectContainerStartTime(ch chan<- metrics.Metric, pod summary.PodStats, s summary.ContainerStats) {
if s.StartTime.Unix() == 0 {
if s.StartTime.Unix() <= 0 {
return
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/kubelet/metrics/collectors/resource_metrics_test.go
Expand Up @@ -190,6 +190,45 @@ func TestCollectResourceMetrics(t *testing.T) {
container_start_time_seconds{container="container_b",namespace="namespace_a",pod="pod_a"} 1.6243961583020916e+09 1624396158302
`,
},
{
name: "arbitrary container metrics for negative StartTime",
summary: &statsapi.Summary{
Pods: []statsapi.PodStats{
{
PodRef: statsapi.PodReference{
Name: "pod_a",
Namespace: "namespace_a",
},
Containers: []statsapi.ContainerStats{
{
Name: "container_a",
StartTime: metav1.NewTime(time.Unix(0, -1624396278302091597)),
CPU: &statsapi.CPUStats{
Time: testTime,
UsageCoreNanoSeconds: uint64Ptr(10000000000),
},
Memory: &statsapi.MemoryStats{
Time: testTime,
WorkingSetBytes: uint64Ptr(1000),
},
},
},
},
},
},
summaryErr: nil,
expectedMetrics: `
# HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise
# TYPE scrape_error gauge
scrape_error 0
# HELP container_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the container in core-seconds
# TYPE container_cpu_usage_seconds_total counter
container_cpu_usage_seconds_total{container="container_a",namespace="namespace_a",pod="pod_a"} 10 1624396278302
# HELP container_memory_working_set_bytes [ALPHA] Current working set of the container in bytes
# TYPE container_memory_working_set_bytes gauge
container_memory_working_set_bytes{container="container_a",namespace="namespace_a",pod="pod_a"} 1000 1624396278302
`,
},
{
name: "nil container metrics",
summary: &statsapi.Summary{
Expand Down

0 comments on commit 6382266

Please sign in to comment.