Skip to content

Commit 0950b8f

Browse files
povilasvsigilioso
authored andcommitted
[k8sclusterreceiver] add k8s.node.condition metric (open-telemetry#27838)
**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> open-telemetry#27617 **Testing:** <Describe what testing was performed and which tests were added.> - added unit tests **Documentation:** <Describe the documentation added.> - added docs
1 parent 87f4d4d commit 0950b8f

File tree

12 files changed

+227
-2
lines changed

12 files changed

+227
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: "enhancement"
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: k8sclusterreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "add k8s.node.condition metric"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [27617]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/k8sclusterreceiver/documentation.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,20 @@ metrics:
396396
enabled: true
397397
```
398398

399+
### k8s.node.condition
400+
401+
The condition of a particular Node.
402+
403+
| Unit | Metric Type | Value Type |
404+
| ---- | ----------- | ---------- |
405+
| {condition} | Gauge | Int |
406+
407+
#### Attributes
408+
409+
| Name | Description | Values |
410+
| ---- | ----------- | ------ |
411+
| condition | the name of Kubernetes Node condition. Example: Ready, Memory, PID, DiskPressure | Any Str |
412+
399413
### k8s.pod.status_reason
400414

401415
Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4 - Shutdown, 5 - UnexpectedAdmissionError, 6 - Unknown)

receiver/k8sclusterreceiver/internal/collection/collector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (dc *DataCollector) CollectMetricData(currentTime time.Time) pmetric.Metric
6969
if crm.ScopeMetrics().Len() > 0 {
7070
crm.MoveTo(customRMs.AppendEmpty())
7171
}
72+
node.RecordMetrics(dc.metricsBuilder, o.(*corev1.Node), ts)
7273
})
7374
dc.metadataStore.ForEach(gvk.Namespace, func(o any) {
7475
namespace.RecordMetrics(dc.metricsBuilder, o.(*corev1.Namespace), ts)

receiver/k8sclusterreceiver/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/k8sclusterreceiver/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/k8sclusterreceiver/internal/metadata/generated_metrics.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/k8sclusterreceiver/internal/metadata/generated_metrics_test.go

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ all_set:
5555
enabled: true
5656
k8s.namespace.phase:
5757
enabled: true
58+
k8s.node.condition:
59+
enabled: true
5860
k8s.pod.phase:
5961
enabled: true
6062
k8s.pod.status_reason:
@@ -210,6 +212,8 @@ none_set:
210212
enabled: false
211213
k8s.namespace.phase:
212214
enabled: false
215+
k8s.node.condition:
216+
enabled: false
213217
k8s.pod.phase:
214218
enabled: false
215219
k8s.pod.status_reason:

receiver/k8sclusterreceiver/internal/node/nodes.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/maps"
1919
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/experimentalmetricmetadata"
2020
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
21+
imetadata "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/metadata"
2122
)
2223

2324
const (
@@ -47,6 +48,19 @@ func Transform(node *corev1.Node) *corev1.Node {
4748
return newNode
4849
}
4950

51+
func RecordMetrics(mb *imetadata.MetricsBuilder, node *corev1.Node, ts pcommon.Timestamp) {
52+
for _, c := range node.Status.Conditions {
53+
mb.RecordK8sNodeConditionDataPoint(ts, nodeConditionValues[c.Status], string(c.Type))
54+
}
55+
rb := mb.NewResourceBuilder()
56+
rb.SetK8sNodeUID(string(node.UID))
57+
rb.SetK8sNodeName(node.Name)
58+
rb.SetK8sKubeletVersion(node.Status.NodeInfo.KubeletVersion)
59+
rb.SetK8sKubeproxyVersion(node.Status.NodeInfo.KubeProxyVersion)
60+
61+
mb.EmitForResource(imetadata.WithResource(rb.Emit()))
62+
}
63+
5064
func CustomMetrics(set receiver.CreateSettings, rb *metadata.ResourceBuilder, node *corev1.Node, nodeConditionTypesToReport,
5165
allocatableTypesToReport []string, ts pcommon.Timestamp) pmetric.ResourceMetrics {
5266
rm := pmetric.NewResourceMetrics()

receiver/k8sclusterreceiver/internal/node/nodes_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ func TestNodeConditionValue(t *testing.T) {
158158
}
159159
}
160160

161+
func TestNodeMetrics(t *testing.T) {
162+
n := testutils.NewNode("1")
163+
164+
ts := pcommon.Timestamp(time.Now().UnixNano())
165+
mbc := metadata.DefaultMetricsBuilderConfig()
166+
mbc.Metrics.K8sNodeCondition.Enabled = true
167+
mb := metadata.NewMetricsBuilder(mbc, receivertest.NewNopCreateSettings())
168+
RecordMetrics(mb, n, ts)
169+
m := mb.Emit()
170+
171+
expectedFile := filepath.Join("testdata", "expected_mdatagen.yaml")
172+
expected, err := golden.ReadMetrics(expectedFile)
173+
require.NoError(t, err)
174+
require.NoError(t, pmetrictest.CompareMetrics(expected, m,
175+
pmetrictest.IgnoreTimestamp(),
176+
pmetrictest.IgnoreStartTimestamp(),
177+
pmetrictest.IgnoreResourceMetricsOrder(),
178+
pmetrictest.IgnoreMetricsOrder(),
179+
pmetrictest.IgnoreScopeMetricsOrder(),
180+
pmetrictest.IgnoreMetricDataPointsOrder(),
181+
),
182+
)
183+
}
184+
161185
func TestTransform(t *testing.T) {
162186
originalNode := &corev1.Node{
163187
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)