Skip to content

Commit

Permalink
[processor/k8sattributesprocessor] support key_regex match full lengt…
Browse files Browse the repository at this point in the history
…h value (#13381)
  • Loading branch information
erenming authored Sep 7, 2022
1 parent 213d9c6 commit bc9e9ba
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
6 changes: 3 additions & 3 deletions processor/k8sattributesprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ type ExtractConfig struct {
// extract:
// labels:
//
// - name: $1
// - tag_name: $$1
// key_regex: kubernetes.io/(.*)
//
// this will add the `component` and `version` tags to the spans or metrics.
Expand All @@ -144,11 +144,11 @@ type ExtractConfig struct {
// extract:
// annotations:
//
// - name: git.sha
// - tag_name: git.sha
// key: kubernetes.io/change-cause
// regex: GIT_SHA=(?P<value>\w+)
//
// - name: ci.build
// - tag_name: ci.build
// key: kubernetes.io/change-cause
// regex: JENKINS=(?P<value>[\w]+)
//
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
// - tag_name: l1 # extracts value of label from namespaces with key `label1` and inserts it as a tag with key `l1`
// key: label1
// from: namespace
// - tag_name: l2 # extracts value of label from pods with key `label1` with regexp and inserts it as a tag with key `l2`
// - tag_name: l2 # extracts value of label from pods with key `label2` with regexp and inserts it as a tag with key `l2`
// key: label2
// regex: field=(?P<value>.+)
// from: pod
Expand Down
27 changes: 19 additions & 8 deletions processor/k8sattributesprocessor/internal/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ func TestExtractionRules(t *testing.T) {
name: "all-labels",
rules: ExtractionRules{
Labels: []FieldExtractionRule{{
KeyRegex: regexp.MustCompile("la*"),
KeyRegex: regexp.MustCompile("^(?:la.*)$"),
From: MetadataFromPod,
},
},
Expand All @@ -658,7 +658,7 @@ func TestExtractionRules(t *testing.T) {
name: "all-annotations",
rules: ExtractionRules{
Annotations: []FieldExtractionRule{{
KeyRegex: regexp.MustCompile("an*"),
KeyRegex: regexp.MustCompile("^(?:an.*)$"),
From: MetadataFromPod,
},
},
Expand All @@ -667,12 +667,23 @@ func TestExtractionRules(t *testing.T) {
"k8s.pod.annotations.annotation1": "av1",
},
},
{
name: "all-annotations-not-match",
rules: ExtractionRules{
Annotations: []FieldExtractionRule{{
KeyRegex: regexp.MustCompile("^(?:an*)$"),
From: MetadataFromPod,
},
},
},
attributes: map[string]string{},
},
{
name: "captured-groups",
rules: ExtractionRules{
Annotations: []FieldExtractionRule{{
Name: "$1",
KeyRegex: regexp.MustCompile(`annotation(\d+)`),
KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`),
HasKeyRegexReference: true,
From: MetadataFromPod,
},
Expand All @@ -686,15 +697,15 @@ func TestExtractionRules(t *testing.T) {
name: "captured-groups-$0",
rules: ExtractionRules{
Annotations: []FieldExtractionRule{{
Name: "$0",
KeyRegex: regexp.MustCompile(`annotation(\d+)`),
Name: "prefix-$0",
KeyRegex: regexp.MustCompile(`^(?:annotation(\d+))$`),
HasKeyRegexReference: true,
From: MetadataFromPod,
},
},
},
attributes: map[string]string{
"annotation1": "av1",
"prefix-annotation1": "av1",
},
},
}
Expand Down Expand Up @@ -765,7 +776,7 @@ func TestNamespaceExtractionRules(t *testing.T) {
name: "all-labels",
rules: ExtractionRules{
Labels: []FieldExtractionRule{{
KeyRegex: regexp.MustCompile("la*"),
KeyRegex: regexp.MustCompile("^(?:la.*)$"),
From: MetadataFromNamespace,
},
},
Expand All @@ -778,7 +789,7 @@ func TestNamespaceExtractionRules(t *testing.T) {
name: "all-annotations",
rules: ExtractionRules{
Annotations: []FieldExtractionRule{{
KeyRegex: regexp.MustCompile("an*"),
KeyRegex: regexp.MustCompile("^(?:an.*)$"),
From: MetadataFromNamespace,
},
},
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/internal/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type FieldExtractionRule struct {
Name string
// Key is used to lookup k8s pod fields.
Key string
// KeyRegex is a regular expression used to extract a Key that matches the regex.
// KeyRegex is a regular expression(full length match) used to extract a Key that matches the regex.
KeyRegex *regexp.Regexp
HasKeyRegexReference bool
// Regex is a regular expression used to extract a sub-part of a field value.
Expand Down
2 changes: 1 addition & 1 deletion processor/k8sattributesprocessor/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func extractFieldRules(fieldType string, fields ...FieldExtractConfig) ([]kube.F
var hasKeyRegexReference bool
if a.KeyRegex != "" {
var err error
keyRegex, err = regexp.Compile(a.KeyRegex)
keyRegex, err = regexp.Compile("^(?:" + a.KeyRegex + ")$")
if err != nil {
return rules, err
}
Expand Down
10 changes: 5 additions & 5 deletions processor/k8sattributesprocessor/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestWithExtractAnnotations(t *testing.T) {
[]kube.FieldExtractionRule{
{
Name: "tag1",
KeyRegex: regexp.MustCompile("key*"),
KeyRegex: regexp.MustCompile("^(?:key*)$"),
From: kube.MetadataFromPod,
},
},
Expand All @@ -161,7 +161,7 @@ func TestWithExtractAnnotations(t *testing.T) {
[]kube.FieldExtractionRule{
{
Name: "tag1",
KeyRegex: regexp.MustCompile("key*"),
KeyRegex: regexp.MustCompile("^(?:key*)$"),
From: kube.MetadataFromNamespace,
},
},
Expand Down Expand Up @@ -260,7 +260,7 @@ func TestWithExtractLabels(t *testing.T) {
[]kube.FieldExtractionRule{
{
Name: "tag1",
KeyRegex: regexp.MustCompile("key*"),
KeyRegex: regexp.MustCompile("^(?:key*)$"),
From: kube.MetadataFromPod,
},
},
Expand All @@ -278,7 +278,7 @@ func TestWithExtractLabels(t *testing.T) {
[]kube.FieldExtractionRule{
{
Name: "tag1",
KeyRegex: regexp.MustCompile("key*"),
KeyRegex: regexp.MustCompile("^(?:key*)$"),
From: kube.MetadataFromNamespace,
},
},
Expand Down Expand Up @@ -677,7 +677,7 @@ func Test_extractFieldRules(t *testing.T) {
[]kube.FieldExtractionRule{
{
Name: "$0-$1-$2",
KeyRegex: regexp.MustCompile("(key)(.*)"),
KeyRegex: regexp.MustCompile("^(?:(key)(.*))$"),
HasKeyRegexReference: true,
From: kube.MetadataFromPod,
},
Expand Down
16 changes: 16 additions & 0 deletions unreleased/support-full-regex-matching.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: k8sattributesprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Change the way how `key_regex` setting is handled. After this change, provided expressions are applied to the full length of attribute values.

# One or more tracking issues related to the change
issues: [9716]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

0 comments on commit bc9e9ba

Please sign in to comment.