Skip to content

Commit 941624a

Browse files
committed
adapt extractFieldRules function
Signed-off-by: odubajDT <[email protected]>
1 parent 7f35bc4 commit 941624a

File tree

2 files changed

+32
-78
lines changed

2 files changed

+32
-78
lines changed

processor/k8sattributesprocessor/options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func withDeploymentNameFromReplicaSet(enabled bool) option {
238238
// withExtractLabels allows specifying options to control extraction of pod labels.
239239
func withExtractLabels(labels ...FieldExtractConfig) option {
240240
return func(p *kubernetesprocessor) error {
241-
labels, err := extractFieldRules("labels", labels...)
241+
labels, err := extractFieldRules(labels...)
242242
if err != nil {
243243
return err
244244
}
@@ -250,7 +250,7 @@ func withExtractLabels(labels ...FieldExtractConfig) option {
250250
// withExtractAnnotations allows specifying options to control extraction of pod annotations tags.
251251
func withExtractAnnotations(annotations ...FieldExtractConfig) option {
252252
return func(p *kubernetesprocessor) error {
253-
annotations, err := extractFieldRules("annotations", annotations...)
253+
annotations, err := extractFieldRules(annotations...)
254254
if err != nil {
255255
return err
256256
}
@@ -259,7 +259,7 @@ func withExtractAnnotations(annotations ...FieldExtractConfig) option {
259259
}
260260
}
261261

262-
func extractFieldRules(_ string, fields ...FieldExtractConfig) ([]kube.FieldExtractionRule, error) {
262+
func extractFieldRules(fields ...FieldExtractConfig) ([]kube.FieldExtractionRule, error) {
263263
var rules []kube.FieldExtractionRule
264264
for _, a := range fields {
265265
name := a.TagName

processor/k8sattributesprocessor/options_test.go

Lines changed: 29 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -562,58 +562,54 @@ func TestWithFilterFields(t *testing.T) {
562562
}
563563

564564
func Test_extractFieldRules(t *testing.T) {
565-
type args struct {
566-
fieldType string
567-
fields []FieldExtractConfig
568-
}
569565
tests := []struct {
570566
name string
571-
args args
567+
fields []FieldExtractConfig
572568
want []kube.FieldExtractionRule
573569
wantErr bool
574570
}{
575571
{
576-
name: "default",
577-
args: args{"labels", []FieldExtractConfig{
572+
name: "empty tag_name leaves Name empty for dynamic resolution",
573+
fields: []FieldExtractConfig{
578574
{
579575
Key: "key",
580576
From: kube.MetadataFromPod,
581577
},
582-
}},
578+
},
583579
want: []kube.FieldExtractionRule{
584580
{
585-
Name: "", // Name is empty for dynamic resolution based on feature gates
581+
Name: "",
586582
Key: "key",
587583
From: kube.MetadataFromPod,
588584
},
589585
},
590586
},
591587
{
592-
name: "basic",
593-
args: args{"field", []FieldExtractConfig{
588+
name: "explicit tag_name is preserved",
589+
fields: []FieldExtractConfig{
594590
{
595-
TagName: "name",
591+
TagName: "custom.name",
596592
Key: "key",
597593
From: kube.MetadataFromPod,
598594
},
599-
}},
595+
},
600596
want: []kube.FieldExtractionRule{
601597
{
602-
Name: "name",
598+
Name: "custom.name",
603599
Key: "key",
604600
From: kube.MetadataFromPod,
605601
},
606602
},
607603
},
608604
{
609-
name: "keyregex-capture-group",
610-
args: args{"labels", []FieldExtractConfig{
605+
name: "keyregex with capture groups",
606+
fields: []FieldExtractConfig{
611607
{
612608
TagName: "$0-$1-$2",
613609
KeyRegex: "(key)(.*)",
614610
From: kube.MetadataFromPod,
615611
},
616-
}},
612+
},
617613
want: []kube.FieldExtractionRule{
618614
{
619615
Name: "$0-$1-$2",
@@ -623,91 +619,49 @@ func Test_extractFieldRules(t *testing.T) {
623619
},
624620
},
625621
},
626-
}
627-
for _, tt := range tests {
628-
t.Run(tt.name, func(t *testing.T) {
629-
got, err := extractFieldRules(tt.args.fieldType, tt.args.fields...)
630-
if tt.wantErr {
631-
assert.Error(t, err)
632-
return
633-
}
634-
require.NoError(t, err)
635-
assert.Equal(t, tt.want, got)
636-
})
637-
}
638-
}
639-
640-
func Test_extractFieldRules_FeatureGate(t *testing.T) {
641-
tests := []struct {
642-
name string
643-
fieldType string
644-
fields []FieldExtractConfig
645-
wantNamePattern string
646-
}{
647622
{
648-
name: "labels without custom tag_name leaves Name empty for dynamic resolution",
649-
fieldType: "labels",
623+
name: "namespace metadata source",
650624
fields: []FieldExtractConfig{
651625
{
652-
Key: "app",
653-
From: kube.MetadataFromPod,
654-
},
655-
},
656-
wantNamePattern: "",
657-
},
658-
{
659-
name: "annotations without custom tag_name leaves Name empty for dynamic resolution",
660-
fieldType: "annotations",
661-
fields: []FieldExtractConfig{
662-
{
663-
Key: "workload",
664-
From: kube.MetadataFromPod,
626+
Key: "env",
627+
From: kube.MetadataFromNamespace,
665628
},
666629
},
667-
wantNamePattern: "",
668-
},
669-
{
670-
name: "namespace labels without custom tag_name leaves Name empty",
671-
fieldType: "labels",
672-
fields: []FieldExtractConfig{
630+
want: []kube.FieldExtractionRule{
673631
{
632+
Name: "",
674633
Key: "env",
675634
From: kube.MetadataFromNamespace,
676635
},
677636
},
678-
wantNamePattern: "",
679637
},
680638
{
681-
name: "node annotations without custom tag_name leaves Name empty",
682-
fieldType: "annotations",
639+
name: "node metadata source",
683640
fields: []FieldExtractConfig{
684641
{
685642
Key: "zone",
686643
From: kube.MetadataFromNode,
687644
},
688645
},
689-
wantNamePattern: "",
690-
},
691-
{
692-
name: "explicit tag name preserved regardless of feature gates",
693-
fieldType: "labels",
694-
fields: []FieldExtractConfig{
646+
want: []kube.FieldExtractionRule{
695647
{
696-
TagName: "custom.tag.name",
697-
Key: "app",
698-
From: kube.MetadataFromPod,
648+
Name: "",
649+
Key: "zone",
650+
From: kube.MetadataFromNode,
699651
},
700652
},
701-
wantNamePattern: "custom.tag.name",
702653
},
703654
}
704655

705656
for _, tt := range tests {
706657
t.Run(tt.name, func(t *testing.T) {
707-
got, err := extractFieldRules(tt.fieldType, tt.fields...)
658+
got, err := extractFieldRules(tt.fields...)
659+
if tt.wantErr {
660+
assert.Error(t, err)
661+
return
662+
}
708663
require.NoError(t, err)
709-
require.Len(t, got, 1)
710-
assert.Equal(t, tt.wantNamePattern, got[0].Name)
664+
assert.Equal(t, tt.want, got)
711665
})
712666
}
713667
}

0 commit comments

Comments
 (0)