Skip to content

Commit 1a8bdfc

Browse files
odubajDTcrobert-1evan-bradleyTylerHelmuth
authored
[processor/k8sattributes] Deprecate FieldExtractConfig.Regex config option (#33411)
**Link to tracking Issue:** #25128 --------- Signed-off-by: odubajDT <[email protected]> Co-authored-by: Curtis Robert <[email protected]> Co-authored-by: Evan Bradley <[email protected]> Co-authored-by: Tyler Helmuth <[email protected]>
1 parent 0393a45 commit 1a8bdfc

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed

.chloggen/chore_25128_regex.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: deprecation
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: k8sattributesprocessor
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Deprecate `extract.annotations.regex` and `extract.labels.regex` config fields in favor of the `ExtractPatterns` function in the transform processor. The `FieldExtractConfig.Regex` parameter will be removed in version v0.111.0.
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: [25128]
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: Deprecating of FieldExtractConfig.Regex parameter means that it is recommended to use the `ExtractPatterns` function from the transform processor instead. To convert your current configuration please check the `ExtractPatterns` function [documentation](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/ottlfuncs#extractpatterns). You should use the `pattern` parameter of `ExtractPatterns` instead of using the `FieldExtractConfig.Regex` parameter.
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]

processor/k8sattributesprocessor/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,51 @@ as tags.
332332

333333
By default, the `k8s.pod.start_time` uses [Time.MarshalText()](https://pkg.go.dev/time#Time.MarshalText) to format the
334334
timestamp value as an RFC3339 compliant timestamp.
335+
336+
## Feature Gate
337+
338+
### `k8sattr.fieldExtractConfigRegex.disallow`
339+
340+
The `k8sattr.fieldExtractConfigRegex.disallow` [feature gate](https://github.com/open-telemetry/opentelemetry-collector/blob/main/featuregate/README.md#collector-feature-gates) disallows the usage of the `extract.annotations.regex` and `extract.labels.regex` fields.
341+
The validation performed on the configuration will fail, if at least one of the parameters is set (non-empty) and `k8sattr.fieldExtractConfigRegex.disallow` is set to `true` (default `false`).
342+
343+
#### Example Usage
344+
345+
The following config with the feature gate set will lead to validation error:
346+
347+
`config.yaml`:
348+
349+
```yaml
350+
extract:
351+
labels:
352+
regex: <my-regex1>
353+
annotations:
354+
regex: <my-regex2>
355+
```
356+
357+
Run collector: `./otelcol --config config.yaml --feature-gates=k8sattr.fieldExtractConfigRegex.disallow`
358+
359+
#### Migration
360+
361+
Deprecation of the `extract.annotations.regex` and `extract.labels.regex` fields means that it is recommended to use the `ExtractPatterns` function from the transform processor instead. To convert your current configuration please check the `ExtractPatterns` function [documentation](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/pkg/ottl/ottlfuncs#extractpatterns). You should use the `pattern` parameter of `ExtractPatterns` instead of using the the `extract.annotations.regex` and `extract.labels.regex` fields.
362+
363+
##### Example
364+
365+
The following configuration of `k8sattributes processor`:
366+
367+
`config.yaml`:
368+
369+
```yaml
370+
annotations:
371+
- tag_name: a2 # extracts value of annotation with key `annotation2` with regexp and inserts it as a tag with key `a2`
372+
key: annotation2
373+
regex: field=(?P<value>.+)
374+
from: pod
375+
```
376+
377+
can be converted with the usage of `ExtractPatterns` function:
378+
379+
```yaml
380+
- set(cache["annotations"], ExtractPatterns(attributes["k8s.pod.annotations["annotation2"], "field=(?P<value>.+))")
381+
- set(k8s.pod.annotations["a2"], cache["annotations"]["value"])
382+
```

processor/k8sattributesprocessor/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@ import (
77
"fmt"
88
"regexp"
99

10+
"go.opentelemetry.io/collector/featuregate"
1011
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
1112

1213
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
1314
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/kube"
1415
)
1516

17+
var disallowFieldExtractConfigRegex = featuregate.GlobalRegistry().MustRegister(
18+
"k8sattr.fieldExtractConfigRegex.disallow",
19+
featuregate.StageAlpha,
20+
featuregate.WithRegisterDescription("When enabled, usage of the FieldExtractConfig.Regex field is disallowed"),
21+
featuregate.WithRegisterFromVersion("v0.106.0"),
22+
)
23+
1624
// Config defines configuration for k8s attributes processor.
1725
type Config struct {
1826
k8sconfig.APIConfig `mapstructure:",squash"`
@@ -63,6 +71,9 @@ func (cfg *Config) Validate() error {
6371
}
6472

6573
if f.Regex != "" {
74+
if disallowFieldExtractConfigRegex.IsEnabled() {
75+
return fmt.Errorf("the extract.annotations.regex and extract.labels.regex fields have been deprecated, please use the `ExtractPatterns` function in the transform processor instead")
76+
}
6677
r, err := regexp.Compile(f.Regex)
6778
if err != nil {
6879
return err
@@ -213,6 +224,8 @@ type FieldExtractConfig struct {
213224
// regex: JENKINS=(?P<value>[\w]+)
214225
//
215226
// this will add the `git.sha` and `ci.build` resource attributes.
227+
// Deprecated: [v0.106.0] Use the `ExtractPatterns` function in the transform processor instead.
228+
// More information about how to replace the regex field can be found in the k8sattributes processor readme.
216229
Regex string `mapstructure:"regex"`
217230

218231
// From represents the source of the labels/annotations.

processor/k8sattributesprocessor/config_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/stretchr/testify/require"
1212
"go.opentelemetry.io/collector/component"
1313
"go.opentelemetry.io/collector/confmap/confmaptest"
14+
"go.opentelemetry.io/collector/featuregate"
1415

1516
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig"
1617
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor/internal/kube"
@@ -21,8 +22,9 @@ func TestLoadConfig(t *testing.T) {
2122
t.Parallel()
2223

2324
tests := []struct {
24-
id component.ID
25-
expected component.Config
25+
id component.ID
26+
expected component.Config
27+
disallowRegex bool
2628
}{
2729
{
2830
id: component.NewID(metadata.Type),
@@ -127,9 +129,35 @@ func TestLoadConfig(t *testing.T) {
127129
},
128130
},
129131
},
132+
{
133+
id: component.NewIDWithName(metadata.Type, "deprecated-regex"),
134+
expected: &Config{
135+
APIConfig: k8sconfig.APIConfig{AuthType: k8sconfig.AuthTypeKubeConfig},
136+
Passthrough: false,
137+
Extract: ExtractConfig{
138+
Metadata: enabledAttributes(),
139+
Annotations: []FieldExtractConfig{
140+
{Regex: "field=(?P<value>.+)", From: "pod"},
141+
},
142+
Labels: []FieldExtractConfig{
143+
{Regex: "field=(?P<value>.+)", From: "pod"},
144+
},
145+
},
146+
Exclude: ExcludeConfig{
147+
Pods: []ExcludePodConfig{
148+
{Name: "jaeger-agent"},
149+
{Name: "jaeger-collector"},
150+
},
151+
},
152+
},
153+
},
130154
{
131155
id: component.NewIDWithName(metadata.Type, "too_many_sources"),
132156
},
157+
{
158+
id: component.NewIDWithName(metadata.Type, "deprecated-regex"),
159+
disallowRegex: true,
160+
},
133161
{
134162
id: component.NewIDWithName(metadata.Type, "bad_keys_labels"),
135163
},
@@ -176,6 +204,12 @@ func TestLoadConfig(t *testing.T) {
176204

177205
for _, tt := range tests {
178206
t.Run(tt.id.String(), func(t *testing.T) {
207+
if tt.disallowRegex {
208+
require.Nil(t, featuregate.GlobalRegistry().Set(disallowFieldExtractConfigRegex.ID(), true))
209+
t.Cleanup(func() {
210+
require.Nil(t, featuregate.GlobalRegistry().Set(disallowFieldExtractConfigRegex.ID(), false))
211+
})
212+
}
179213
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
180214
require.NoError(t, err)
181215

processor/k8sattributesprocessor/testdata/config.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ k8sattributes/too_many_sources:
106106
- from: connection
107107
name: ip
108108

109+
k8sattributes/deprecated-regex:
110+
passthrough: false
111+
auth_type: "kubeConfig"
112+
extract:
113+
labels:
114+
- regex: field=(?P<value>.+)
115+
from: pod
116+
annotations:
117+
- regex: field=(?P<value>.+)
118+
from: pod
119+
109120
k8sattributes/bad_keys_labels:
110121
extract:
111122
labels:

0 commit comments

Comments
 (0)