Skip to content

Commit bc9e9ba

Browse files
authored
[processor/k8sattributesprocessor] support key_regex match full length value (#13381)
1 parent 213d9c6 commit bc9e9ba

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

processor/k8sattributesprocessor/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ type ExtractConfig struct {
123123
// extract:
124124
// labels:
125125
//
126-
// - name: $1
126+
// - tag_name: $$1
127127
// key_regex: kubernetes.io/(.*)
128128
//
129129
// this will add the `component` and `version` tags to the spans or metrics.
@@ -144,11 +144,11 @@ type ExtractConfig struct {
144144
// extract:
145145
// annotations:
146146
//
147-
// - name: git.sha
147+
// - tag_name: git.sha
148148
// key: kubernetes.io/change-cause
149149
// regex: GIT_SHA=(?P<value>\w+)
150150
//
151-
// - name: ci.build
151+
// - tag_name: ci.build
152152
// key: kubernetes.io/change-cause
153153
// regex: JENKINS=(?P<value>[\w]+)
154154
//

processor/k8sattributesprocessor/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
// - tag_name: l1 # extracts value of label from namespaces with key `label1` and inserts it as a tag with key `l1`
9292
// key: label1
9393
// from: namespace
94-
// - tag_name: l2 # extracts value of label from pods with key `label1` with regexp and inserts it as a tag with key `l2`
94+
// - tag_name: l2 # extracts value of label from pods with key `label2` with regexp and inserts it as a tag with key `l2`
9595
// key: label2
9696
// regex: field=(?P<value>.+)
9797
// from: pod

processor/k8sattributesprocessor/internal/kube/client_test.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ func TestExtractionRules(t *testing.T) {
644644
name: "all-labels",
645645
rules: ExtractionRules{
646646
Labels: []FieldExtractionRule{{
647-
KeyRegex: regexp.MustCompile("la*"),
647+
KeyRegex: regexp.MustCompile("^(?:la.*)$"),
648648
From: MetadataFromPod,
649649
},
650650
},
@@ -658,7 +658,7 @@ func TestExtractionRules(t *testing.T) {
658658
name: "all-annotations",
659659
rules: ExtractionRules{
660660
Annotations: []FieldExtractionRule{{
661-
KeyRegex: regexp.MustCompile("an*"),
661+
KeyRegex: regexp.MustCompile("^(?:an.*)$"),
662662
From: MetadataFromPod,
663663
},
664664
},
@@ -667,12 +667,23 @@ func TestExtractionRules(t *testing.T) {
667667
"k8s.pod.annotations.annotation1": "av1",
668668
},
669669
},
670+
{
671+
name: "all-annotations-not-match",
672+
rules: ExtractionRules{
673+
Annotations: []FieldExtractionRule{{
674+
KeyRegex: regexp.MustCompile("^(?:an*)$"),
675+
From: MetadataFromPod,
676+
},
677+
},
678+
},
679+
attributes: map[string]string{},
680+
},
670681
{
671682
name: "captured-groups",
672683
rules: ExtractionRules{
673684
Annotations: []FieldExtractionRule{{
674685
Name: "$1",
675-
KeyRegex: regexp.MustCompile(`annotation(\d+)`),
686+
KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`),
676687
HasKeyRegexReference: true,
677688
From: MetadataFromPod,
678689
},
@@ -686,15 +697,15 @@ func TestExtractionRules(t *testing.T) {
686697
name: "captured-groups-$0",
687698
rules: ExtractionRules{
688699
Annotations: []FieldExtractionRule{{
689-
Name: "$0",
690-
KeyRegex: regexp.MustCompile(`annotation(\d+)`),
700+
Name: "prefix-$0",
701+
KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`),
691702
HasKeyRegexReference: true,
692703
From: MetadataFromPod,
693704
},
694705
},
695706
},
696707
attributes: map[string]string{
697-
"annotation1": "av1",
708+
"prefix-annotation1": "av1",
698709
},
699710
},
700711
}
@@ -765,7 +776,7 @@ func TestNamespaceExtractionRules(t *testing.T) {
765776
name: "all-labels",
766777
rules: ExtractionRules{
767778
Labels: []FieldExtractionRule{{
768-
KeyRegex: regexp.MustCompile("la*"),
779+
KeyRegex: regexp.MustCompile("^(?:la.*)$"),
769780
From: MetadataFromNamespace,
770781
},
771782
},
@@ -778,7 +789,7 @@ func TestNamespaceExtractionRules(t *testing.T) {
778789
name: "all-annotations",
779790
rules: ExtractionRules{
780791
Annotations: []FieldExtractionRule{{
781-
KeyRegex: regexp.MustCompile("an*"),
792+
KeyRegex: regexp.MustCompile("^(?:an.*)$"),
782793
From: MetadataFromNamespace,
783794
},
784795
},

processor/k8sattributesprocessor/internal/kube/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ type FieldExtractionRule struct {
212212
Name string
213213
// Key is used to lookup k8s pod fields.
214214
Key string
215-
// KeyRegex is a regular expression used to extract a Key that matches the regex.
215+
// KeyRegex is a regular expression(full length match) used to extract a Key that matches the regex.
216216
KeyRegex *regexp.Regexp
217217
HasKeyRegexReference bool
218218
// Regex is a regular expression used to extract a sub-part of a field value.

processor/k8sattributesprocessor/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.F
199199
var hasKeyRegexReference bool
200200
if a.KeyRegex != "" {
201201
var err error
202-
keyRegex, err = regexp.Compile(a.KeyRegex)
202+
keyRegex, err = regexp.Compile("^(?:" + a.KeyRegex + ")$")
203203
if err != nil {
204204
return rules, err
205205
}

processor/k8sattributesprocessor/options_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestWithExtractAnnotations(t *testing.T) {
143143
[]kube.FieldExtractionRule{
144144
{
145145
Name: "tag1",
146-
KeyRegex: regexp.MustCompile("key*"),
146+
KeyRegex: regexp.MustCompile("^(?:key*)$"),
147147
From: kube.MetadataFromPod,
148148
},
149149
},
@@ -161,7 +161,7 @@ func TestWithExtractAnnotations(t *testing.T) {
161161
[]kube.FieldExtractionRule{
162162
{
163163
Name: "tag1",
164-
KeyRegex: regexp.MustCompile("key*"),
164+
KeyRegex: regexp.MustCompile("^(?:key*)$"),
165165
From: kube.MetadataFromNamespace,
166166
},
167167
},
@@ -260,7 +260,7 @@ func TestWithExtractLabels(t *testing.T) {
260260
[]kube.FieldExtractionRule{
261261
{
262262
Name: "tag1",
263-
KeyRegex: regexp.MustCompile("key*"),
263+
KeyRegex: regexp.MustCompile("^(?:key*)$"),
264264
From: kube.MetadataFromPod,
265265
},
266266
},
@@ -278,7 +278,7 @@ func TestWithExtractLabels(t *testing.T) {
278278
[]kube.FieldExtractionRule{
279279
{
280280
Name: "tag1",
281-
KeyRegex: regexp.MustCompile("key*"),
281+
KeyRegex: regexp.MustCompile("^(?:key*)$"),
282282
From: kube.MetadataFromNamespace,
283283
},
284284
},
@@ -677,7 +677,7 @@ func Test_extractFieldRules(t *testing.T) {
677677
[]kube.FieldExtractionRule{
678678
{
679679
Name: "$0-$1-$2",
680-
KeyRegex: regexp.MustCompile("(key)(.*)"),
680+
KeyRegex: regexp.MustCompile("^(?:(key)(.*))$"),
681681
HasKeyRegexReference: true,
682682
From: kube.MetadataFromPod,
683683
},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: breaking
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
5+
component: k8sattributesprocessor
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Change the way how `key_regex` setting is handled. After this change, provided expressions are applied to the full length of attribute values.
9+
10+
# One or more tracking issues related to the change
11+
issues: [9716]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

0 commit comments

Comments
 (0)