-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
nodes.go
180 lines (157 loc) · 6.27 KB
/
nodes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package node // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/node"
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.18.0"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"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 (
// Keys for node metadata.
nodeCreationTime = "node.creation_timestamp"
)
// Transform transforms the node to remove the fields that we don't use to reduce RAM utilization.
// IMPORTANT: Make sure to update this function before using new node fields.
func Transform(node *corev1.Node) *corev1.Node {
newNode := &corev1.Node{
ObjectMeta: metadata.TransformObjectMeta(node.ObjectMeta),
Status: corev1.NodeStatus{
Allocatable: node.Status.Allocatable,
NodeInfo: corev1.NodeSystemInfo{
KubeletVersion: node.Status.NodeInfo.KubeletVersion,
KubeProxyVersion: node.Status.NodeInfo.KubeProxyVersion,
},
},
}
for _, c := range node.Status.Conditions {
newNode.Status.Conditions = append(newNode.Status.Conditions, corev1.NodeCondition{
Type: c.Type,
Status: c.Status,
})
}
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()
sm := rm.ScopeMetrics().AppendEmpty()
// Adding 'node condition type' metrics
for _, nodeConditionTypeValue := range nodeConditionTypesToReport {
v1NodeConditionTypeValue := corev1.NodeConditionType(nodeConditionTypeValue)
m := sm.Metrics().AppendEmpty()
m.SetName(getNodeConditionMetric(nodeConditionTypeValue))
m.SetDescription(fmt.Sprintf("%v condition status of the node (true=1, false=0, unknown=-1)", nodeConditionTypeValue))
m.SetUnit("")
g := m.SetEmptyGauge()
dp := g.DataPoints().AppendEmpty()
dp.SetIntValue(nodeConditionValue(node, v1NodeConditionTypeValue))
dp.SetTimestamp(ts)
}
// Adding 'node allocatable type' metrics
for _, nodeAllocatableTypeValue := range allocatableTypesToReport {
v1NodeAllocatableTypeValue := corev1.ResourceName(nodeAllocatableTypeValue)
quantity, ok := node.Status.Allocatable[v1NodeAllocatableTypeValue]
if !ok {
set.Logger.Debug(fmt.Errorf("allocatable type %v not found in node %v", nodeAllocatableTypeValue,
node.GetName()).Error())
continue
}
m := sm.Metrics().AppendEmpty()
m.SetName(getNodeAllocatableMetric(nodeAllocatableTypeValue))
m.SetDescription(fmt.Sprintf("Amount of %v allocatable on the node", nodeAllocatableTypeValue))
m.SetUnit(getNodeAllocatableUnit(v1NodeAllocatableTypeValue))
g := m.SetEmptyGauge()
dp := g.DataPoints().AppendEmpty()
setNodeAllocatableValue(dp, v1NodeAllocatableTypeValue, quantity)
dp.SetTimestamp(ts)
}
if sm.Metrics().Len() == 0 {
return pmetric.NewResourceMetrics()
}
// TODO: Generate a schema URL for the node metrics in the metadata package and use them here.
rm.SetSchemaUrl(conventions.SchemaURL)
sm.Scope().SetName("otelcol/k8sclusterreceiver")
sm.Scope().SetVersion(set.BuildInfo.Version)
rb.SetK8sNodeUID(string(node.UID))
rb.SetK8sNodeName(node.Name)
rb.SetK8sKubeletVersion(node.Status.NodeInfo.KubeletVersion)
rb.SetK8sKubeproxyVersion(node.Status.NodeInfo.KubeProxyVersion)
rb.Emit().MoveTo(rm.Resource())
return rm
}
var nodeConditionValues = map[corev1.ConditionStatus]int64{
corev1.ConditionTrue: 1,
corev1.ConditionFalse: 0,
corev1.ConditionUnknown: -1,
}
func nodeConditionValue(node *corev1.Node, condType corev1.NodeConditionType) int64 {
status := corev1.ConditionUnknown
for _, c := range node.Status.Conditions {
if c.Type == condType {
status = c.Status
break
}
}
return nodeConditionValues[status]
}
func GetMetadata(node *corev1.Node) map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata {
meta := maps.MergeStringMaps(map[string]string{}, node.Labels)
meta[conventions.AttributeK8SNodeName] = node.Name
meta[nodeCreationTime] = node.GetCreationTimestamp().Format(time.RFC3339)
nodeID := experimentalmetricmetadata.ResourceID(node.UID)
return map[experimentalmetricmetadata.ResourceID]*metadata.KubernetesMetadata{
nodeID: {
EntityType: "k8s.node",
ResourceIDKey: conventions.AttributeK8SNodeUID,
ResourceID: nodeID,
Metadata: meta,
},
}
}
func getNodeConditionMetric(nodeConditionTypeValue string) string {
return fmt.Sprintf("k8s.node.condition_%s", strcase.ToSnake(nodeConditionTypeValue))
}
func getNodeAllocatableUnit(res corev1.ResourceName) string {
switch res {
case corev1.ResourceCPU:
return "{cpu}"
case corev1.ResourceMemory, corev1.ResourceEphemeralStorage, corev1.ResourceStorage:
return "By"
case corev1.ResourcePods:
return "{pod}"
default:
return fmt.Sprintf("{%s}", string(res))
}
}
func setNodeAllocatableValue(dp pmetric.NumberDataPoint, res corev1.ResourceName, q resource.Quantity) {
switch res {
case corev1.ResourceCPU:
dp.SetDoubleValue(float64(q.MilliValue()) / 1000.0)
default:
dp.SetIntValue(q.Value())
}
}
func getNodeAllocatableMetric(nodeAllocatableTypeValue string) string {
return fmt.Sprintf("k8s.node.allocatable_%s", strcase.ToSnake(nodeAllocatableTypeValue))
}