Skip to content

Commit 74518cc

Browse files
jinja2dragonlord93
authored andcommitted
[receiver/k8scluster] add hpa.scaletargetref attrs (open-telemetry#40063)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Adds new resource attributes to `k8s.hpa`. These attributes track the target resource a given HPA is configured to scale. The attributes are - `k8s.hpa.scaletargetref.kind`, `k8s.hpa.scaletargetref.name`, and `k8s.hpa.scaletargetref.apiversion`. These attributes are disabled by default. PR for semantic-conventions is available [here](open-telemetry/semantic-conventions#2180). <!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes open-telemetry#38768 <!--Describe what testing was performed and which tests were added.--> #### Testing Added unit tests Tested manually in a k8s cluster <!--Describe the documentation added.--> #### Documentation Documentation updated with new attribute names <!--Please delete paragraphs that you did not use before submitting.-->
1 parent f0d7e71 commit 74518cc

14 files changed

+207
-1
lines changed

.chloggen/hpa-scaletarget-attrs.yaml

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: receiver/k8sclusterreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Added new resource attributes `k8s.hpa.scaletargetref.kind`, `k8s.hpa.scaletargetref.name`, and `k8s.hpa.scaletargetref.apiversion` to the `k8s.hpa` resource. These attributes are disabled by default.
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: [38768]
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ Current status reason of the pod (1 - Evicted, 2 - NodeAffinity, 3 - NodeLost, 4
436436
| k8s.deployment.name | The name of the Deployment. | Any Str | true |
437437
| k8s.deployment.uid | The UID of the Deployment. | Any Str | true |
438438
| k8s.hpa.name | The k8s hpa name. | Any Str | true |
439+
| k8s.hpa.scaletargetref.apiversion | The API version of the target resource to scale for the HorizontalPodAutoscaler. | Any Str | false |
440+
| k8s.hpa.scaletargetref.kind | The kind of the target resource to scale for the HorizontalPodAutoscaler. | Any Str | false |
441+
| k8s.hpa.scaletargetref.name | The name of the target resource to scale for the HorizontalPodAutoscaler. | Any Str | false |
439442
| k8s.hpa.uid | The k8s hpa uid. | Any Str | true |
440443
| k8s.job.name | The k8s pod name. | Any Str | true |
441444
| k8s.job.uid | The k8s job uid. | Any Str | true |

receiver/k8sclusterreceiver/internal/hpa/hpa.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func RecordMetrics(mb *metadata.MetricsBuilder, hpa *autoscalingv2.HorizontalPod
2020
rb.SetK8sHpaUID(string(hpa.UID))
2121
rb.SetK8sHpaName(hpa.Name)
2222
rb.SetK8sNamespaceName(hpa.Namespace)
23+
rb.SetK8sHpaScaletargetrefApiversion(hpa.Spec.ScaleTargetRef.APIVersion)
24+
rb.SetK8sHpaScaletargetrefKind(hpa.Spec.ScaleTargetRef.Kind)
25+
rb.SetK8sHpaScaletargetrefName(hpa.Spec.ScaleTargetRef.Name)
2326
mb.EmitForResource(metadata.WithResource(rb.Emit()))
2427
}
2528

receiver/k8sclusterreceiver/internal/hpa/hpa_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,32 @@ func TestHPAMetrics(t *testing.T) {
4646
testutils.AssertMetricInt(t, sms.Metrics().At(2), "k8s.hpa.max_replicas", pmetric.MetricTypeGauge, 10)
4747
testutils.AssertMetricInt(t, sms.Metrics().At(3), "k8s.hpa.min_replicas", pmetric.MetricTypeGauge, 2)
4848
}
49+
50+
func TestHPAResAttrs(t *testing.T) {
51+
hpa := testutils.NewHPA("1")
52+
53+
ts := pcommon.Timestamp(time.Now().UnixNano())
54+
55+
// Enable additional attributes
56+
cfg := metadata.DefaultMetricsBuilderConfig()
57+
cfg.ResourceAttributes.K8sHpaScaletargetrefKind.Enabled = true
58+
cfg.ResourceAttributes.K8sHpaScaletargetrefName.Enabled = true
59+
cfg.ResourceAttributes.K8sHpaScaletargetrefApiversion.Enabled = true
60+
61+
mb := metadata.NewMetricsBuilder(cfg, receivertest.NewNopSettings(metadata.Type))
62+
RecordMetrics(mb, hpa, ts)
63+
m := mb.Emit()
64+
65+
require.Equal(t, 1, m.ResourceMetrics().Len())
66+
rm := m.ResourceMetrics().At(0)
67+
assert.Equal(t,
68+
map[string]any{
69+
"k8s.hpa.uid": "test-hpa-1-uid",
70+
"k8s.hpa.name": "test-hpa-1",
71+
"k8s.namespace.name": "test-namespace",
72+
"k8s.hpa.scaletargetref.kind": "Deployment",
73+
"k8s.hpa.scaletargetref.name": "test-deployment",
74+
"k8s.hpa.scaletargetref.apiversion": "apps/v1",
75+
},
76+
rm.Resource().Attributes().AsRaw())
77+
}

receiver/k8sclusterreceiver/internal/metadata/generated_config.go

Lines changed: 12 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: 12 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_logs_test.go

Lines changed: 3 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: 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/generated_metrics_test.go

Lines changed: 3 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_resource.go

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

0 commit comments

Comments
 (0)