Skip to content

Commit 39b6f37

Browse files
jinja2johnleslie
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 c5ca2cb commit 39b6f37

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.

receiver/k8sclusterreceiver/internal/metadata/generated_resource_test.go

Lines changed: 19 additions & 1 deletion
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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ all_set:
118118
enabled: true
119119
k8s.hpa.name:
120120
enabled: true
121+
k8s.hpa.scaletargetref.apiversion:
122+
enabled: true
123+
k8s.hpa.scaletargetref.kind:
124+
enabled: true
125+
k8s.hpa.scaletargetref.name:
126+
enabled: true
121127
k8s.hpa.uid:
122128
enabled: true
123129
k8s.job.name:
@@ -283,6 +289,12 @@ none_set:
283289
enabled: false
284290
k8s.hpa.name:
285291
enabled: false
292+
k8s.hpa.scaletargetref.apiversion:
293+
enabled: false
294+
k8s.hpa.scaletargetref.kind:
295+
enabled: false
296+
k8s.hpa.scaletargetref.name:
297+
enabled: false
286298
k8s.hpa.uid:
287299
enabled: false
288300
k8s.job.name:
@@ -387,6 +399,18 @@ filter_set_include:
387399
enabled: true
388400
metrics_include:
389401
- regexp: ".*"
402+
k8s.hpa.scaletargetref.apiversion:
403+
enabled: true
404+
metrics_include:
405+
- regexp: ".*"
406+
k8s.hpa.scaletargetref.kind:
407+
enabled: true
408+
metrics_include:
409+
- regexp: ".*"
410+
k8s.hpa.scaletargetref.name:
411+
enabled: true
412+
metrics_include:
413+
- regexp: ".*"
390414
k8s.hpa.uid:
391415
enabled: true
392416
metrics_include:
@@ -537,6 +561,18 @@ filter_set_exclude:
537561
enabled: true
538562
metrics_exclude:
539563
- strict: "k8s.hpa.name-val"
564+
k8s.hpa.scaletargetref.apiversion:
565+
enabled: true
566+
metrics_exclude:
567+
- strict: "k8s.hpa.scaletargetref.apiversion-val"
568+
k8s.hpa.scaletargetref.kind:
569+
enabled: true
570+
metrics_exclude:
571+
- strict: "k8s.hpa.scaletargetref.kind-val"
572+
k8s.hpa.scaletargetref.name:
573+
enabled: true
574+
metrics_exclude:
575+
- strict: "k8s.hpa.scaletargetref.name-val"
540576
k8s.hpa.uid:
541577
enabled: true
542578
metrics_exclude:

receiver/k8sclusterreceiver/internal/testutils/objects.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ func NewHPA(id string) *autoscalingv2.HorizontalPodAutoscaler {
3131
Spec: autoscalingv2.HorizontalPodAutoscalerSpec{
3232
MinReplicas: &minReplicas,
3333
MaxReplicas: 10,
34+
ScaleTargetRef: autoscalingv2.CrossVersionObjectReference{
35+
Kind: "Deployment",
36+
Name: "test-deployment",
37+
APIVersion: "apps/v1",
38+
},
3439
},
3540
}
3641
}

receiver/k8sclusterreceiver/metadata.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,21 @@ resource_attributes:
147147
type: string
148148
enabled: true
149149

150+
k8s.hpa.scaletargetref.kind:
151+
description: The kind of the target resource to scale for the HorizontalPodAutoscaler.
152+
type: string
153+
enabled: false
154+
155+
k8s.hpa.scaletargetref.name:
156+
description: The name of the target resource to scale for the HorizontalPodAutoscaler.
157+
type: string
158+
enabled: false
159+
160+
k8s.hpa.scaletargetref.apiversion:
161+
description: The API version of the target resource to scale for the HorizontalPodAutoscaler.
162+
type: string
163+
enabled: false
164+
150165
k8s.job.name:
151166
description: The k8s pod name.
152167
type: string
@@ -518,3 +533,4 @@ tests:
518533
skip_lifecycle: true
519534
goleak:
520535
skip: true
536+

0 commit comments

Comments
 (0)