Skip to content

Commit

Permalink
[receiver/k8scluster] Add Node resource attributes (open-telemetry#30343
Browse files Browse the repository at this point in the history
)

**Description:**
I would like to use k8s cluster receiver to get more information about
Node, such as Container Runtime Version, OS version and OS description.

This is how it looks when using kind:
```
k8s_node_allocatable_cpu_{container_runtime="containerd", container_runtime_version="1.6.9", k8s_kubelet_version="v1.25.3", k8s_node_name="kind-control-plane", k8s_node_uid="29b8a758-ac58-45e7-9091-b9ed2e285be9", os_description="Ubuntu 22.04.1 LTS", os_version="6.6.10-arch1-1"}

```

**Link to tracking Issue:**

open-telemetry#30342
  • Loading branch information
povilasv authored and cparkins committed Feb 1, 2024
1 parent 8401215 commit 66676fd
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 7 deletions.
27 changes: 27 additions & 0 deletions .chloggen/k8scluster_add-resource-attributes.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 new disabled os.version, os.description, k8s.container_runtime.version resource attributes"

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

# (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]
4 changes: 4 additions & 0 deletions receiver/k8sclusterreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4
| container.id | The container id. | Any Str | true |
| container.image.name | The container image name | Any Str | true |
| container.image.tag | The container image tag | Any Str | true |
| container.runtime | The container runtime used by Kubernetes Node. | Any Str | false |
| container.runtime.version | The version of container runtime used by Kubernetes Node. | Any Str | false |
| k8s.container.name | The k8s container name | Any Str | true |
| k8s.cronjob.name | The k8s CronJob name | Any Str | true |
| k8s.cronjob.uid | The k8s CronJob uid. | Any Str | true |
Expand Down Expand Up @@ -455,3 +457,5 @@ Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4
| k8s.statefulset.uid | The k8s statefulset uid. | Any Str | true |
| openshift.clusterquota.name | The k8s ClusterResourceQuota name. | Any Str | true |
| openshift.clusterquota.uid | The k8s ClusterResourceQuota uid. | Any Str | true |
| os.description | The os description used by Kubernetes Node. | Any Str | false |
| os.version | The version of operating system used by Kubernetes Node. | Any Str | false |
16 changes: 16 additions & 0 deletions receiver/k8sclusterreceiver/internal/metadata/generated_config.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.

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.

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

16 changes: 16 additions & 0 deletions receiver/k8sclusterreceiver/internal/metadata/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ all_set:
enabled: true
container.image.tag:
enabled: true
container.runtime:
enabled: true
container.runtime.version:
enabled: true
k8s.container.name:
enabled: true
k8s.cronjob.name:
Expand Down Expand Up @@ -156,6 +160,10 @@ all_set:
enabled: true
openshift.clusterquota.uid:
enabled: true
os.description:
enabled: true
os.version:
enabled: true
none_set:
metrics:
k8s.container.cpu_limit:
Expand Down Expand Up @@ -253,6 +261,10 @@ none_set:
enabled: false
container.image.tag:
enabled: false
container.runtime:
enabled: false
container.runtime.version:
enabled: false
k8s.container.name:
enabled: false
k8s.cronjob.name:
Expand Down Expand Up @@ -313,3 +325,7 @@ none_set:
enabled: false
openshift.clusterquota.uid:
enabled: false
os.description:
enabled: false
os.version:
enabled: false
29 changes: 27 additions & 2 deletions receiver/k8sclusterreceiver/internal/node/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package node // import "github.com/open-telemetry/opentelemetry-collector-contri

import (
"fmt"
"strings"
"time"

"github.com/iancoleman/strcase"
Expand Down Expand Up @@ -34,8 +35,11 @@ func Transform(node *corev1.Node) *corev1.Node {
Status: corev1.NodeStatus{
Allocatable: node.Status.Allocatable,
NodeInfo: corev1.NodeSystemInfo{
KubeletVersion: node.Status.NodeInfo.KubeletVersion,
KubeProxyVersion: node.Status.NodeInfo.KubeProxyVersion,
KubeletVersion: node.Status.NodeInfo.KubeletVersion,
KubeProxyVersion: node.Status.NodeInfo.KubeProxyVersion,
KernelVersion: node.Status.NodeInfo.KernelVersion,
ContainerRuntimeVersion: node.Status.NodeInfo.ContainerRuntimeVersion,
OSImage: node.Status.NodeInfo.OSImage,
},
},
}
Expand Down Expand Up @@ -111,6 +115,17 @@ func CustomMetrics(set receiver.CreateSettings, rb *metadata.ResourceBuilder, no
rb.SetK8sNodeName(node.Name)
rb.SetK8sKubeletVersion(node.Status.NodeInfo.KubeletVersion)
rb.SetK8sKubeproxyVersion(node.Status.NodeInfo.KubeProxyVersion)
rb.SetOsVersion(node.Status.NodeInfo.KernelVersion)

runtime, version := getContainerRuntimeInfo(node.Status.NodeInfo.ContainerRuntimeVersion)
if runtime != "" {
rb.SetContainerRuntime(runtime)
}
if version != "" {
rb.SetContainerRuntimeVersion(version)
}

rb.SetOsDescription(node.Status.NodeInfo.OSImage)
rb.Emit().MoveTo(rm.Resource())
return rm
}
Expand Down Expand Up @@ -149,6 +164,16 @@ func GetMetadata(node *corev1.Node) map[experimentalmetricmetadata.ResourceID]*m
}
}

func getContainerRuntimeInfo(rawInfo string) (runtime string, version string) {
// Kubelet reports container runtime version in the following format:
// <runtime-name>://<version>
parts := strings.Split(rawInfo, "://")

if len(parts) == 2 {
return parts[0], parts[1]
}
return "", ""
}
func getNodeConditionMetric(nodeConditionTypeValue string) string {
return fmt.Sprintf("k8s.node.condition_%s", strcase.ToSnake(nodeConditionTypeValue))
}
Expand Down
Loading

0 comments on commit 66676fd

Please sign in to comment.