From e7338d2105b6afc5c927f1a6e80c9606d7d9af4c Mon Sep 17 00:00:00 2001 From: keval Date: Sun, 28 Jul 2024 23:18:43 +0530 Subject: [PATCH] Added K8s Container Waiting Reason --- receiver/k8sclusterreceiver/documentation.md | 1 + .../k8sclusterreceiver/generated_package_test.go | 1 - .../internal/container/containers.go | 3 +++ .../internal/metadata/generated_config.go | 4 ++++ .../internal/metadata/generated_config_test.go | 4 ++++ .../internal/metadata/generated_metrics.go | 6 ++++++ .../internal/metadata/generated_metrics_test.go | 1 + .../internal/metadata/generated_resource.go | 7 +++++++ .../internal/metadata/generated_resource_test.go | 10 ++++++++-- .../internal/metadata/testdata/config.yaml | 12 ++++++++++++ receiver/k8sclusterreceiver/metadata.yaml | 5 +++++ 11 files changed, 51 insertions(+), 3 deletions(-) diff --git a/receiver/k8sclusterreceiver/documentation.md b/receiver/k8sclusterreceiver/documentation.md index 16d6a28b2174..ab47ca17091c 100644 --- a/receiver/k8sclusterreceiver/documentation.md +++ b/receiver/k8sclusterreceiver/documentation.md @@ -524,6 +524,7 @@ Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4 | k8s.clusterrolebinding.type | The type of the Cluster Role Binding. | Any Str | true | | k8s.clusterrolebinding.uid | The UID of the Cluster Role Binding. | Any Str | true | | k8s.container.name | The k8s container name | Any Str | true | +| k8s.container.status.current_waiting_reason | Current waiting reason of the Container. | Any Str | true | | k8s.container.status.last_terminated_reason | Last terminated reason of a container. | Any Str | true | | k8s.cronjob.name | The k8s CronJob name | Any Str | true | | k8s.cronjob.start_time | The start time of the Cronjob. | Any Str | true | diff --git a/receiver/k8sclusterreceiver/generated_package_test.go b/receiver/k8sclusterreceiver/generated_package_test.go index 35feefb66e47..90473c0aeb3f 100644 --- a/receiver/k8sclusterreceiver/generated_package_test.go +++ b/receiver/k8sclusterreceiver/generated_package_test.go @@ -3,7 +3,6 @@ package k8sclusterreceiver import ( - "os" "testing" ) diff --git a/receiver/k8sclusterreceiver/internal/container/containers.go b/receiver/k8sclusterreceiver/internal/container/containers.go index 368f4c73c410..23c4d6d78711 100644 --- a/receiver/k8sclusterreceiver/internal/container/containers.go +++ b/receiver/k8sclusterreceiver/internal/container/containers.go @@ -73,6 +73,9 @@ func RecordSpecMetrics(logger *zap.Logger, mb *imetadata.MetricsBuilder, c corev if cs.LastTerminationState.Terminated != nil { rb.SetK8sContainerStatusLastTerminatedReason(cs.LastTerminationState.Terminated.Reason) } + if cs.State.Waiting != nil { + rb.SetK8sContainerStatusCurrentWaitingReason(cs.State.Waiting.Reason) + } break } } diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_config.go b/receiver/k8sclusterreceiver/internal/metadata/generated_config.go index 2971d4dcbc0a..3fffeae0adf2 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_config.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_config.go @@ -301,6 +301,7 @@ type ResourceAttributesConfig struct { K8sClusterrolebindingType ResourceAttributeConfig `mapstructure:"k8s.clusterrolebinding.type"` K8sClusterrolebindingUID ResourceAttributeConfig `mapstructure:"k8s.clusterrolebinding.uid"` K8sContainerName ResourceAttributeConfig `mapstructure:"k8s.container.name"` + K8sContainerStatusCurrentWaitingReason ResourceAttributeConfig `mapstructure:"k8s.container.status.current_waiting_reason"` K8sContainerStatusLastTerminatedReason ResourceAttributeConfig `mapstructure:"k8s.container.status.last_terminated_reason"` K8sCronjobName ResourceAttributeConfig `mapstructure:"k8s.cronjob.name"` K8sCronjobStartTime ResourceAttributeConfig `mapstructure:"k8s.cronjob.start_time"` @@ -479,6 +480,9 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig { K8sContainerName: ResourceAttributeConfig{ Enabled: true, }, + K8sContainerStatusCurrentWaitingReason: ResourceAttributeConfig{ + Enabled: true, + }, K8sContainerStatusLastTerminatedReason: ResourceAttributeConfig{ Enabled: true, }, diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_config_test.go b/receiver/k8sclusterreceiver/internal/metadata/generated_config_test.go index fe8262f856d4..58b9c70c0baa 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_config_test.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_config_test.go @@ -103,6 +103,7 @@ func TestMetricsBuilderConfig(t *testing.T) { K8sClusterrolebindingType: ResourceAttributeConfig{Enabled: true}, K8sClusterrolebindingUID: ResourceAttributeConfig{Enabled: true}, K8sContainerName: ResourceAttributeConfig{Enabled: true}, + K8sContainerStatusCurrentWaitingReason: ResourceAttributeConfig{Enabled: true}, K8sContainerStatusLastTerminatedReason: ResourceAttributeConfig{Enabled: true}, K8sCronjobName: ResourceAttributeConfig{Enabled: true}, K8sCronjobStartTime: ResourceAttributeConfig{Enabled: true}, @@ -296,6 +297,7 @@ func TestMetricsBuilderConfig(t *testing.T) { K8sClusterrolebindingType: ResourceAttributeConfig{Enabled: false}, K8sClusterrolebindingUID: ResourceAttributeConfig{Enabled: false}, K8sContainerName: ResourceAttributeConfig{Enabled: false}, + K8sContainerStatusCurrentWaitingReason: ResourceAttributeConfig{Enabled: false}, K8sContainerStatusLastTerminatedReason: ResourceAttributeConfig{Enabled: false}, K8sCronjobName: ResourceAttributeConfig{Enabled: false}, K8sCronjobStartTime: ResourceAttributeConfig{Enabled: false}, @@ -462,6 +464,7 @@ func TestResourceAttributesConfig(t *testing.T) { K8sClusterrolebindingType: ResourceAttributeConfig{Enabled: true}, K8sClusterrolebindingUID: ResourceAttributeConfig{Enabled: true}, K8sContainerName: ResourceAttributeConfig{Enabled: true}, + K8sContainerStatusCurrentWaitingReason: ResourceAttributeConfig{Enabled: true}, K8sContainerStatusLastTerminatedReason: ResourceAttributeConfig{Enabled: true}, K8sCronjobName: ResourceAttributeConfig{Enabled: true}, K8sCronjobStartTime: ResourceAttributeConfig{Enabled: true}, @@ -597,6 +600,7 @@ func TestResourceAttributesConfig(t *testing.T) { K8sClusterrolebindingType: ResourceAttributeConfig{Enabled: false}, K8sClusterrolebindingUID: ResourceAttributeConfig{Enabled: false}, K8sContainerName: ResourceAttributeConfig{Enabled: false}, + K8sContainerStatusCurrentWaitingReason: ResourceAttributeConfig{Enabled: false}, K8sContainerStatusLastTerminatedReason: ResourceAttributeConfig{Enabled: false}, K8sCronjobName: ResourceAttributeConfig{Enabled: false}, K8sCronjobStartTime: ResourceAttributeConfig{Enabled: false}, diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go b/receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go index 2f8c3a55639e..db040eb1f311 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go @@ -2946,6 +2946,12 @@ func NewMetricsBuilder(mbc MetricsBuilderConfig, settings receiver.Settings, opt if mbc.ResourceAttributes.K8sContainerName.MetricsExclude != nil { mb.resourceAttributeExcludeFilter["k8s.container.name"] = filter.CreateFilter(mbc.ResourceAttributes.K8sContainerName.MetricsExclude) } + if mbc.ResourceAttributes.K8sContainerStatusCurrentWaitingReason.MetricsInclude != nil { + mb.resourceAttributeIncludeFilter["k8s.container.status.current_waiting_reason"] = filter.CreateFilter(mbc.ResourceAttributes.K8sContainerStatusCurrentWaitingReason.MetricsInclude) + } + if mbc.ResourceAttributes.K8sContainerStatusCurrentWaitingReason.MetricsExclude != nil { + mb.resourceAttributeExcludeFilter["k8s.container.status.current_waiting_reason"] = filter.CreateFilter(mbc.ResourceAttributes.K8sContainerStatusCurrentWaitingReason.MetricsExclude) + } if mbc.ResourceAttributes.K8sContainerStatusLastTerminatedReason.MetricsInclude != nil { mb.resourceAttributeIncludeFilter["k8s.container.status.last_terminated_reason"] = filter.CreateFilter(mbc.ResourceAttributes.K8sContainerStatusLastTerminatedReason.MetricsInclude) } diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_metrics_test.go b/receiver/k8sclusterreceiver/internal/metadata/generated_metrics_test.go index 55902595cb77..4a953bf40a3b 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_metrics_test.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_metrics_test.go @@ -305,6 +305,7 @@ func TestMetricsBuilder(t *testing.T) { rb.SetK8sClusterrolebindingType("k8s.clusterrolebinding.type-val") rb.SetK8sClusterrolebindingUID("k8s.clusterrolebinding.uid-val") rb.SetK8sContainerName("k8s.container.name-val") + rb.SetK8sContainerStatusCurrentWaitingReason("k8s.container.status.current_waiting_reason-val") rb.SetK8sContainerStatusLastTerminatedReason("k8s.container.status.last_terminated_reason-val") rb.SetK8sCronjobName("k8s.cronjob.name-val") rb.SetK8sCronjobStartTime("k8s.cronjob.start_time-val") diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_resource.go b/receiver/k8sclusterreceiver/internal/metadata/generated_resource.go index 1a75602b66c1..902ddf7bdfbe 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_resource.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_resource.go @@ -175,6 +175,13 @@ func (rb *ResourceBuilder) SetK8sContainerName(val string) { } } +// SetK8sContainerStatusCurrentWaitingReason sets provided value as "k8s.container.status.current_waiting_reason" attribute. +func (rb *ResourceBuilder) SetK8sContainerStatusCurrentWaitingReason(val string) { + if rb.config.K8sContainerStatusCurrentWaitingReason.Enabled { + rb.res.Attributes().PutStr("k8s.container.status.current_waiting_reason", val) + } +} + // SetK8sContainerStatusLastTerminatedReason sets provided value as "k8s.container.status.last_terminated_reason" attribute. func (rb *ResourceBuilder) SetK8sContainerStatusLastTerminatedReason(val string) { if rb.config.K8sContainerStatusLastTerminatedReason.Enabled { diff --git a/receiver/k8sclusterreceiver/internal/metadata/generated_resource_test.go b/receiver/k8sclusterreceiver/internal/metadata/generated_resource_test.go index c794c86fee21..7373fdb8ad20 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/generated_resource_test.go +++ b/receiver/k8sclusterreceiver/internal/metadata/generated_resource_test.go @@ -35,6 +35,7 @@ func TestResourceBuilder(t *testing.T) { rb.SetK8sClusterrolebindingType("k8s.clusterrolebinding.type-val") rb.SetK8sClusterrolebindingUID("k8s.clusterrolebinding.uid-val") rb.SetK8sContainerName("k8s.container.name-val") + rb.SetK8sContainerStatusCurrentWaitingReason("k8s.container.status.current_waiting_reason-val") rb.SetK8sContainerStatusLastTerminatedReason("k8s.container.status.last_terminated_reason-val") rb.SetK8sCronjobName("k8s.cronjob.name-val") rb.SetK8sCronjobStartTime("k8s.cronjob.start_time-val") @@ -149,9 +150,9 @@ func TestResourceBuilder(t *testing.T) { switch test { case "default": - assert.Equal(t, 124, res.Attributes().Len()) + assert.Equal(t, 125, res.Attributes().Len()) case "all_set": - assert.Equal(t, 130, res.Attributes().Len()) + assert.Equal(t, 131, res.Attributes().Len()) case "none_set": assert.Equal(t, 0, res.Attributes().Len()) return @@ -269,6 +270,11 @@ func TestResourceBuilder(t *testing.T) { if ok { assert.EqualValues(t, "k8s.container.name-val", val.Str()) } + val, ok = res.Attributes().Get("k8s.container.status.current_waiting_reason") + assert.True(t, ok) + if ok { + assert.EqualValues(t, "k8s.container.status.current_waiting_reason-val", val.Str()) + } val, ok = res.Attributes().Get("k8s.container.status.last_terminated_reason") assert.True(t, ok) if ok { diff --git a/receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml b/receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml index 4c2b5a4b8aaa..08506631b7d2 100644 --- a/receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml +++ b/receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml @@ -154,6 +154,8 @@ all_set: enabled: true k8s.container.name: enabled: true + k8s.container.status.current_waiting_reason: + enabled: true k8s.container.status.last_terminated_reason: enabled: true k8s.cronjob.name: @@ -525,6 +527,8 @@ none_set: enabled: false k8s.container.name: enabled: false + k8s.container.status.current_waiting_reason: + enabled: false k8s.container.status.last_terminated_reason: enabled: false k8s.cronjob.name: @@ -831,6 +835,10 @@ filter_set_include: enabled: true metrics_include: - regexp: ".*" + k8s.container.status.current_waiting_reason: + enabled: true + metrics_include: + - regexp: ".*" k8s.container.status.last_terminated_reason: enabled: true metrics_include: @@ -1353,6 +1361,10 @@ filter_set_exclude: enabled: true metrics_exclude: - strict: "k8s.container.name-val" + k8s.container.status.current_waiting_reason: + enabled: true + metrics_exclude: + - strict: "k8s.container.status.current_waiting_reason-val" k8s.container.status.last_terminated_reason: enabled: true metrics_exclude: diff --git a/receiver/k8sclusterreceiver/metadata.yaml b/receiver/k8sclusterreceiver/metadata.yaml index 063b5a38cff6..cc1efd2c45c4 100644 --- a/receiver/k8sclusterreceiver/metadata.yaml +++ b/receiver/k8sclusterreceiver/metadata.yaml @@ -624,6 +624,11 @@ resource_attributes: type: string enabled: true + k8s.container.status.current_waiting_reason: + description: Current waiting reason of the Container. + type: string + enabled: true + k8s.daemonset.start_time: description: "The start time of the Daemonset." enabled: true