Skip to content

Commit

Permalink
[receiver/k8s_cluster] Add back all other vendor-specific node condit…
Browse files Browse the repository at this point in the history
…ions (#23854)

**Description:**
Add back all other vendor-specific node conditions, and report them even
if missing, as well as all allocatable node metrics if present, to the
list of Kubernetes node metrics available, which went missing during the
pdata translation

**Link to tracking Issue:**
Fixes #23839

**Testing:**
Unit tests.

**Documentation:**
Not quite. Maybe a doc entry is required here. I haven't looked yet
where to place it.
  • Loading branch information
atoulme committed Jun 30, 2023
1 parent 46d03a0 commit 95f0a13
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# 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 back all other vendor-specific node conditions, and report them even if missing, as well as all allocatable node metrics if present, to the list of Kubernetes node metrics available, which went missing during the pdata translation

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

# (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:
1 change: 1 addition & 0 deletions receiver/k8sclusterreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/census-instrumentation/opencensus-proto v0.4.1
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/iancoleman/strcase v0.2.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.80.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.80.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.80.0
Expand Down
2 changes: 2 additions & 0 deletions receiver/k8sclusterreceiver/go.sum

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

40 changes: 31 additions & 9 deletions receiver/k8sclusterreceiver/internal/node/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"fmt"
"time"

"github.com/iancoleman/strcase"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/common/maps"
Expand Down Expand Up @@ -46,23 +46,30 @@ func Transform(node *corev1.Node) *corev1.Node {
func GetMetrics(set receiver.CreateSettings, node *corev1.Node, nodeConditionTypesToReport, allocatableTypesToReport []string) pmetric.Metrics {
mb := imetadata.NewMetricsBuilder(imetadata.DefaultMetricsBuilderConfig(), set)
ts := pcommon.NewTimestampFromTime(time.Now())
customMetrics := pmetric.NewMetricSlice()

// Adding 'node condition type' metrics
for _, nodeConditionTypeValue := range nodeConditionTypesToReport {
v1NodeConditionTypeValue := corev1.NodeConditionType(nodeConditionTypeValue)
v := nodeConditionValue(node, v1NodeConditionTypeValue)
switch v1NodeConditionTypeValue {
case corev1.NodeReady:
mb.RecordK8sNodeConditionReadyDataPoint(ts, nodeConditionValue(node, v1NodeConditionTypeValue))
mb.RecordK8sNodeConditionReadyDataPoint(ts, v)
case corev1.NodeMemoryPressure:
mb.RecordK8sNodeConditionMemoryPressureDataPoint(ts, nodeConditionValue(node, v1NodeConditionTypeValue))
mb.RecordK8sNodeConditionMemoryPressureDataPoint(ts, v)
case corev1.NodeDiskPressure:
mb.RecordK8sNodeConditionDiskPressureDataPoint(ts, nodeConditionValue(node, v1NodeConditionTypeValue))
mb.RecordK8sNodeConditionDiskPressureDataPoint(ts, v)
case corev1.NodeNetworkUnavailable:
mb.RecordK8sNodeConditionNetworkUnavailableDataPoint(ts, nodeConditionValue(node, v1NodeConditionTypeValue))
mb.RecordK8sNodeConditionNetworkUnavailableDataPoint(ts, v)
case corev1.NodePIDPressure:
mb.RecordK8sNodeConditionPidPressureDataPoint(ts, nodeConditionValue(node, v1NodeConditionTypeValue))
mb.RecordK8sNodeConditionPidPressureDataPoint(ts, v)
default:
set.Logger.Warn("unknown node condition type", zap.String("conditionType", nodeConditionTypeValue))
customMetric := customMetrics.AppendEmpty()
customMetric.SetName(getNodeConditionMetric(nodeConditionTypeValue))
g := customMetric.SetEmptyGauge()
dp := g.DataPoints().AppendEmpty()
dp.SetIntValue(v)
dp.SetTimestamp(ts)
}
}

Expand All @@ -89,10 +96,17 @@ func GetMetrics(set receiver.CreateSettings, node *corev1.Node, nodeConditionTyp
case corev1.ResourcePods:
mb.RecordK8sNodeAllocatablePodsDataPoint(ts, quantity.Value())
default:
set.Logger.Warn("unknown node condition type", zap.Any("conditionType", v1NodeAllocatableTypeValue))
customMetric := customMetrics.AppendEmpty()
customMetric.SetName(getNodeAllocatableMetric(nodeAllocatableTypeValue))
g := customMetric.SetEmptyGauge()
dp := g.DataPoints().AppendEmpty()
dp.SetIntValue(quantity.Value())
dp.SetTimestamp(ts)
}
}
return mb.Emit(imetadata.WithK8sNodeUID(string(node.UID)), imetadata.WithK8sNodeName(node.Name), imetadata.WithOpencensusResourcetype("k8s"))
m := mb.Emit(imetadata.WithK8sNodeUID(string(node.UID)), imetadata.WithK8sNodeName(node.Name), imetadata.WithOpencensusResourcetype("k8s"))
customMetrics.MoveAndAppendTo(m.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics())
return m

}

Expand Down Expand Up @@ -128,3 +142,11 @@ func GetMetadata(node *corev1.Node) map[experimentalmetricmetadata.ResourceID]*m
},
}
}

func getNodeConditionMetric(nodeConditionTypeValue string) string {
return fmt.Sprintf("k8s.node.condition_%s", strcase.ToSnake(nodeConditionTypeValue))
}

func getNodeAllocatableMetric(nodeAllocatableTypeValue string) string {
return fmt.Sprintf("k8s.node.allocatable_%s", strcase.ToSnake(nodeAllocatableTypeValue))
}
21 changes: 20 additions & 1 deletion receiver/k8sclusterreceiver/internal/node/nodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@ import (

func TestNodeMetricsReportCPUMetrics(t *testing.T) {
n := testutils.NewNode("1")
m := GetMetrics(receivertest.NewNopCreateSettings(), n, []string{"Ready", "MemoryPressure", "DiskPressure", "NetworkUnavailable", "PIDPressure", "OutOfDisk"}, []string{"cpu", "memory", "ephemeral-storage", "storage", "pods", "hugepages-1Gi", "hugepages-2Mi"})
m := GetMetrics(receivertest.NewNopCreateSettings(), n,
[]string{
"Ready",
"MemoryPressure",
"DiskPressure",
"NetworkUnavailable",
"PIDPressure",
"OutOfDisk",
},
[]string{
"cpu",
"memory",
"ephemeral-storage",
"storage",
"pods",
"hugepages-1Gi",
"hugepages-2Mi",
"not-present",
},
)
expected, err := golden.ReadMetrics(filepath.Join("testdata", "expected.yaml"))
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expected, m,
Expand Down
12 changes: 12 additions & 0 deletions receiver/k8sclusterreceiver/internal/node/testdata/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ resourceMetrics:
- asInt: "1"
name: k8s.node.condition_ready
unit: "1"
- gauge:
dataPoints:
- asInt: "-1"
name: k8s.node.condition_out_of_disk
- gauge:
dataPoints:
- asInt: "2"
name: k8s.node.allocatable_hugepages_1_gi
- gauge:
dataPoints:
- asInt: "2048"
name: k8s.node.allocatable_hugepages_2_mi
scope:
name: otelcol/k8sclusterreceiver
version: latest
3 changes: 3 additions & 0 deletions receiver/k8sclusterreceiver/internal/testutils/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func NewNode(id string) *corev1.Node {
corev1.ResourceMemory: *resource.NewQuantity(456, resource.DecimalSI),
corev1.ResourceEphemeralStorage: *resource.NewQuantity(1234, resource.DecimalSI),
corev1.ResourcePods: *resource.NewQuantity(12, resource.DecimalSI),
"hugepages-1Gi": *resource.NewQuantity(2, resource.DecimalSI),
"hugepages-2Mi": *resource.NewQuantity(2048, resource.DecimalSI),
"hugepages-5Mi": *resource.NewQuantity(2048, resource.DecimalSI),
},
},
}
Expand Down

0 comments on commit 95f0a13

Please sign in to comment.