Skip to content

Commit

Permalink
[k8sclusterreceiver] add k8s.node.condition metric (#27838)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

Add new k8s.node.condition metric, so that we can deprecate
k8s.node.condition_* metrics.


**Link to tracking Issue:** <Issue number if applicable>


#27617

**Testing:** <Describe what testing was performed and which tests were
added.>

- added unit tests

**Documentation:** <Describe the documentation added.>

- added docs
  • Loading branch information
povilasv authored Oct 24, 2023
1 parent d50e094 commit 67fcaad
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/k8sclusterreceiver-add-k8s-node-condition.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: k8sclusterreceiver

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

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

# (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: [user]
14 changes: 14 additions & 0 deletions receiver/k8sclusterreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,20 @@ metrics:
enabled: true
```

### k8s.node.condition

The condition of a particular Node.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {condition} | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| condition | the name of Kubernetes Node condition. Example: Ready, Memory, PID, DiskPressure | Any Str |

### k8s.pod.status_reason

Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4 - Shutdown, 5 - UnexpectedAdmissionError, 6 - Unknown)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (dc *DataCollector) CollectMetricData(currentTime time.Time) pmetric.Metric
if crm.ScopeMetrics().Len() > 0 {
crm.MoveTo(customRMs.AppendEmpty())
}
node.RecordMetrics(dc.metricsBuilder, o.(*corev1.Node), ts)
})
dc.metadataStore.ForEach(gvk.Namespace, func(o any) {
namespace.RecordMetrics(dc.metricsBuilder, o.(*corev1.Namespace), ts)
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.

59 changes: 59 additions & 0 deletions receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go

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.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ all_set:
enabled: true
k8s.namespace.phase:
enabled: true
k8s.node.condition:
enabled: true
k8s.pod.phase:
enabled: true
k8s.pod.status_reason:
Expand Down Expand Up @@ -210,6 +212,8 @@ none_set:
enabled: false
k8s.namespace.phase:
enabled: false
k8s.node.condition:
enabled: false
k8s.pod.phase:
enabled: false
k8s.pod.status_reason:
Expand Down
14 changes: 14 additions & 0 deletions receiver/k8sclusterreceiver/internal/node/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/maps"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
)

const (
Expand Down Expand Up @@ -47,6 +48,19 @@ func Transform(node *corev1.Node) *corev1.Node {
return newNode
}

func RecordMetrics(mb *imetadata.MetricsBuilder, node *corev1.Node, ts pcommon.Timestamp) {
for _, c := range node.Status.Conditions {
mb.RecordK8sNodeConditionDataPoint(ts, nodeConditionValues[c.Status], string(c.Type))
}
rb := mb.NewResourceBuilder()
rb.SetK8sNodeUID(string(node.UID))
rb.SetK8sNodeName(node.Name)
rb.SetK8sKubeletVersion(node.Status.NodeInfo.KubeletVersion)
rb.SetK8sKubeproxyVersion(node.Status.NodeInfo.KubeProxyVersion)

mb.EmitForResource(imetadata.WithResource(rb.Emit()))
}

func CustomMetrics(set receiver.CreateSettings, rb *metadata.ResourceBuilder, node *corev1.Node, nodeConditionTypesToReport,
allocatableTypesToReport []string, ts pcommon.Timestamp) pmetric.ResourceMetrics {
rm := pmetric.NewResourceMetrics()
Expand Down
24 changes: 24 additions & 0 deletions receiver/k8sclusterreceiver/internal/node/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ func TestNodeConditionValue(t *testing.T) {
}
}

func TestNodeMetrics(t *testing.T) {
n := testutils.NewNode("1")

ts := pcommon.Timestamp(time.Now().UnixNano())
mbc := metadata.DefaultMetricsBuilderConfig()
mbc.Metrics.K8sNodeCondition.Enabled = true
mb := metadata.NewMetricsBuilder(mbc, receivertest.NewNopCreateSettings())
RecordMetrics(mb, n, ts)
m := mb.Emit()

expectedFile := filepath.Join("testdata", "expected_mdatagen.yaml")
expected, err := golden.ReadMetrics(expectedFile)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expected, m,
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
),
)
}

func TestTransform(t *testing.T) {
originalNode := &corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
resourceMetrics:
- resource:
attributes:
- key: k8s.node.name
value:
stringValue: test-node-1
- key: k8s.node.uid
value:
stringValue: test-node-1-uid
schemaUrl: https://opentelemetry.io/schemas/1.18.0
scopeMetrics:
- metrics:
- description: The condition of a particular Node.
gauge:
dataPoints:
- asInt: "1"
attributes:
- key: condition
value:
stringValue: "Ready"
- asInt: "0"
attributes:
- key: condition
value:
stringValue: "PIDPressure"
- asInt: "0"
attributes:
- key: condition
value:
stringValue: "DiskPressure"
- asInt: "0"
attributes:
- key: condition
value:
stringValue: "MemoryPressure"
- asInt: "0"
attributes:
- key: condition
value:
stringValue: "NetworkUnavailable"

name: k8s.node.condition
unit: "{condition}"

scope:
name: otelcol/k8sclusterreceiver
version: latest
15 changes: 13 additions & 2 deletions receiver/k8sclusterreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ attributes:
description: the name of the resource on which the quota is applied
type: string
enabled: true
condition:
description: "the name of Kubernetes Node condition. Example: Ready, Memory, PID, DiskPressure"
type: string
enabled: true

metrics:
k8s.container.cpu_request:
Expand Down Expand Up @@ -478,8 +482,15 @@ metrics:
attributes:
- k8s.namespace.name
- resource

# k8s.node.condition_* metrics (k8s.node.condition_ready, k8s.node.condition_memory_pressure, etc) are controlled
k8s.node.condition:
enabled: false
description: The condition of a particular Node.
unit: "{condition}"
gauge:
value_type: int
attributes:
- condition
# k8s.node.condition_* metrics (k8s.node.condition_ready, k8s.node.condition_memory_pressure, etc) are controlled
# by node_conditions_to_report config option. By default, only k8s.node.condition_ready is enabled.

# k8s.node.allocatable_* metrics (k8s.node.allocatable_cpu, k8s.node.allocatable_memory, etc) are controlled
Expand Down

0 comments on commit 67fcaad

Please sign in to comment.