Skip to content

Commit 766b001

Browse files
committed
fix minor things
Signed-off-by: odubajDT <ondrej.dubaj@dynatrace.com>
1 parent 71484cc commit 766b001

File tree

5 files changed

+26
-78
lines changed

5 files changed

+26
-78
lines changed

.chloggen/k8sattributes-semconv-gates.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ issues: [44693]
1616
# These lines will be padded with 2 spaces and then inserted directly into the document.
1717
# Use pipe (|) for multiline entries.
1818
subtext: |
19-
- Added `semconv.k8s.k8sattributes.enableStable` feature gate to enable stable semantic convention attributes (singular form: `k8s.<workload>.label.<key>` and `k8s.<workload>.annotation.<key>`)
20-
- Added `semconv.k8s.k8sattributes.disableLegacy` feature gate to disable legacy non-compliant attributes (plural form: `k8s.<workload>.labels.<key>` and `k8s.<workload>.annotations.<key>`)
19+
- Added `processor.k8sattributes.EmitV1K8sConventions` feature gate to enable stable semantic convention attributes (singular form: `k8s.<workload>.label.<key>` and `k8s.<workload>.annotation.<key>`)
20+
- Added `processor.k8sattributes.DontEmitV0K8sConventions` feature gate to disable legacy non-compliant attributes (plural form: `k8s.<workload>.labels.<key>` and `k8s.<workload>.annotations.<key>`)
2121
- Both feature gates are in `alpha` stage and disabled by default
2222
- The processor now validates that legacy attributes cannot be disabled without enabling stable attributes
2323
- Deprecated `k8sattr.labelsAnnotationsSingular.allow` feature gate in favor of the new semconv-compliant gates (will be removed in v0.150.0)
24-
- During migration period, both legacy and stable attributes can coexist when `enableStable` is enabled but `disableLegacy` is not
24+
- During migration period, both legacy and stable attributes can coexist when `EmitV1K8sConventions` is enabled but `DontEmitV0K8sConventions` is not
2525
2626
# If your change doesn't affect end users or the exported elements of any package,
2727
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

processor/k8sattributesprocessor/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ type ExtractConfig struct {
175175
type FieldExtractConfig struct {
176176
// TagName represents the name of the resource attribute that will be added to logs, metrics or spans.
177177
// When not specified, a default tag name will be used of the format:
178-
// - k8s.pod.annotations.<annotation key> (or k8s.pod.annotation.<annotation key> when k8sattr.labelsAnnotationsSingular.allow is enabled)
179-
// - k8s.pod.labels.<label key> (or k8s.pod.label.<label key> when k8sattr.labelsAnnotationsSingular.allow is enabled)
178+
// - k8s.pod.annotations.<annotation key> (or k8s.pod.annotation.<annotation key> when processor.k8sattributes.EmitV1K8sConventions is enabled)
179+
// - k8s.pod.labels.<label key> (or k8s.pod.label.<label key> when processor.k8sattributes.EmitV1K8sConventions is enabled)
180180
// For example, if tag_name is not specified and the key is git_sha,
181181
// then the attribute name will be `k8s.pod.annotations.git_sha` (or `k8s.pod.annotation.git_sha` with the feature gate).
182182
// When key_regex is present, tag_name supports back reference to both named capturing and positioned capturing.

processor/k8sattributesprocessor/internal/kube/kube.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,12 @@ func (r *FieldExtractionRule) extractFromMetadata(metadata, tags map[string]stri
372372
}
373373
}
374374
} else if v, ok := metadata[r.Key]; ok {
375-
tags[r.Name] = r.extractField(v)
375+
// Use formatter to determine attribute name if no custom name was specified
376+
name := r.Name
377+
if name == "" {
378+
name = fmt.Sprintf(formatter, r.Key)
379+
}
380+
tags[name] = r.extractField(v)
376381
}
377382
}
378383

processor/k8sattributesprocessor/options.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
package k8sattributesprocessor // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor"
55

66
import (
7-
"fmt"
87
"os"
98
"regexp"
10-
"strings"
119
"time"
1210

1311
conventions "go.opentelemetry.io/otel/semconv/v1.39.0"
@@ -270,18 +268,6 @@ func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.F
270268
a.From = kube.MetadataFromPod
271269
}
272270

273-
if name == "" && a.Key != "" {
274-
// name for KeyRegex case is set at extraction time/runtime, skipped here
275-
// Use singular form when stable attributes are enabled and legacy attributes are disabled
276-
fieldTypeName := fieldType
277-
enableStable := metadata.ProcessorK8sattributesEmitV1K8sConventionsFeatureGate.IsEnabled()
278-
disableLegacy := metadata.ProcessorK8sattributesDontEmitV0K8sConventionsFeatureGate.IsEnabled()
279-
if enableStable && disableLegacy {
280-
fieldTypeName = strings.TrimSuffix(fieldType, "s")
281-
}
282-
name = fmt.Sprintf("k8s.%v.%v.%v", a.From, fieldTypeName, a.Key)
283-
}
284-
285271
var keyRegex *regexp.Regexp
286272
var hasKeyRegexReference bool
287273
if a.KeyRegex != "" {

processor/k8sattributesprocessor/options_test.go

Lines changed: 15 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ import (
99

1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
12-
"go.opentelemetry.io/collector/featuregate"
1312
"k8s.io/apimachinery/pkg/selection"
1413

1514
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
1615
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/kube"
17-
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/metadata"
1816
)
1917

2018
func TestWithAPIConfig(t *testing.T) {
@@ -584,7 +582,7 @@ func Test_extractFieldRules(t *testing.T) {
584582
}},
585583
want: []kube.FieldExtractionRule{
586584
{
587-
Name: "k8s.pod.labels.key",
585+
Name: "", // Name is empty for dynamic resolution based on feature gates
588586
Key: "key",
589587
From: kube.MetadataFromPod,
590588
},
@@ -641,86 +639,57 @@ func Test_extractFieldRules(t *testing.T) {
641639

642640
func Test_extractFieldRules_FeatureGate(t *testing.T) {
643641
tests := []struct {
644-
name string
645-
fieldType string
646-
fields []FieldExtractConfig
647-
featureGateValue bool
648-
wantNamePattern string
642+
name string
643+
fieldType string
644+
fields []FieldExtractConfig
645+
wantNamePattern string
649646
}{
650647
{
651-
name: "labels plural when feature gate disabled",
648+
name: "labels without custom tag_name leaves Name empty for dynamic resolution",
652649
fieldType: "labels",
653650
fields: []FieldExtractConfig{
654651
{
655652
Key: "app",
656653
From: kube.MetadataFromPod,
657654
},
658655
},
659-
featureGateValue: false,
660-
wantNamePattern: "k8s.pod.labels.app",
656+
wantNamePattern: "",
661657
},
662658
{
663-
name: "labels singular when feature gate enabled",
664-
fieldType: "labels",
665-
fields: []FieldExtractConfig{
666-
{
667-
Key: "app",
668-
From: kube.MetadataFromPod,
669-
},
670-
},
671-
featureGateValue: true,
672-
wantNamePattern: "k8s.pod.label.app",
673-
},
674-
{
675-
name: "annotations plural when feature gate disabled",
659+
name: "annotations without custom tag_name leaves Name empty for dynamic resolution",
676660
fieldType: "annotations",
677661
fields: []FieldExtractConfig{
678662
{
679663
Key: "workload",
680664
From: kube.MetadataFromPod,
681665
},
682666
},
683-
featureGateValue: false,
684-
wantNamePattern: "k8s.pod.annotations.workload",
667+
wantNamePattern: "",
685668
},
686669
{
687-
name: "annotations singular when feature gate enabled",
688-
fieldType: "annotations",
689-
fields: []FieldExtractConfig{
690-
{
691-
Key: "workload",
692-
From: kube.MetadataFromPod,
693-
},
694-
},
695-
featureGateValue: true,
696-
wantNamePattern: "k8s.pod.annotation.workload",
697-
},
698-
{
699-
name: "namespace labels singular when feature gate enabled",
670+
name: "namespace labels without custom tag_name leaves Name empty",
700671
fieldType: "labels",
701672
fields: []FieldExtractConfig{
702673
{
703674
Key: "env",
704675
From: kube.MetadataFromNamespace,
705676
},
706677
},
707-
featureGateValue: true,
708-
wantNamePattern: "k8s.namespace.label.env",
678+
wantNamePattern: "",
709679
},
710680
{
711-
name: "node annotations singular when feature gate enabled",
681+
name: "node annotations without custom tag_name leaves Name empty",
712682
fieldType: "annotations",
713683
fields: []FieldExtractConfig{
714684
{
715685
Key: "zone",
716686
From: kube.MetadataFromNode,
717687
},
718688
},
719-
featureGateValue: true,
720-
wantNamePattern: "k8s.node.annotation.zone",
689+
wantNamePattern: "",
721690
},
722691
{
723-
name: "explicit tag name not affected by feature gate",
692+
name: "explicit tag name preserved regardless of feature gates",
724693
fieldType: "labels",
725694
fields: []FieldExtractConfig{
726695
{
@@ -729,24 +698,12 @@ func Test_extractFieldRules_FeatureGate(t *testing.T) {
729698
From: kube.MetadataFromPod,
730699
},
731700
},
732-
featureGateValue: true,
733-
wantNamePattern: "custom.tag.name",
701+
wantNamePattern: "custom.tag.name",
734702
},
735703
}
736704

737705
for _, tt := range tests {
738706
t.Run(tt.name, func(t *testing.T) {
739-
// Set feature gate state for stable and legacy attributes
740-
if tt.featureGateValue {
741-
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ProcessorK8sattributesEmitV1K8sConventionsFeatureGate.ID(), true))
742-
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ProcessorK8sattributesDontEmitV0K8sConventionsFeatureGate.ID(), true))
743-
}
744-
defer func() {
745-
// Reset to default
746-
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ProcessorK8sattributesEmitV1K8sConventionsFeatureGate.ID(), false))
747-
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ProcessorK8sattributesDontEmitV0K8sConventionsFeatureGate.ID(), false))
748-
}()
749-
750707
got, err := extractFieldRules(tt.fieldType, tt.fields...)
751708
require.NoError(t, err)
752709
require.Len(t, got, 1)

0 commit comments

Comments
 (0)