Skip to content

Commit 7a3c35c

Browse files
authored
[confmap] Add strict type validation under a feature gate (#10400)
#### Description <!-- Issue number if applicable --> - Add `confmap.strictlyTypedInput` feature gate that introduces stricter type checks when resolving configuration - Make `confmap.NewRetrievedFromYAML` function public so that external providers have consistent behavior when resolving YAML - Adds `confmap.Retrieved.AsString` method to retrieve string representation for retrieved values #### Link to tracking issue Relates to #9854, updates #8565, #9532
1 parent 48af1b8 commit 7a3c35c

File tree

59 files changed

+541
-99
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+541
-99
lines changed

.chloggen/mx-psi_asstring.yaml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Adds `confmap.Retrieved.AsString` method that returns the configuration value as a string"
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9532]
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:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Adds `confmap.NewRetrievedFromYAML` helper to create `confmap.Retrieved` values from YAML bytes"
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9532]
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:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Adds alpha `confmap.strictlyTypedInput` feature gate that enables strict type checks during configuration resolution"
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [9532]
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: |
19+
When enabled, the configuration resolution system will:
20+
- Stop doing most kinds of implicit type casting when resolving configuration values
21+
- Use the original string representation of configuration values if the ${} syntax is used in inline position
22+
23+
# Optional: The change log or logs in which this entry should be included.
24+
# e.g. '[user]' or '[user, api]'
25+
# Include 'user' if the change is relevant to end users.
26+
# Include 'api' if there is a change to a library API.
27+
# Default: '[user]'
28+
change_logs: []

cmd/mdatagen/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ require (
3232
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
3333
github.com/gogo/protobuf v1.3.2 // indirect
3434
github.com/google/uuid v1.6.0 // indirect
35+
github.com/hashicorp/go-version v1.7.0 // indirect
3536
github.com/json-iterator/go v1.1.12 // indirect
3637
github.com/knadh/koanf/maps v0.1.1 // indirect
3738
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
@@ -45,6 +46,7 @@ require (
4546
github.com/prometheus/client_model v0.6.1 // indirect
4647
github.com/prometheus/common v0.54.0 // indirect
4748
github.com/prometheus/procfs v0.15.0 // indirect
49+
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
4850
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
4951
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
5052
go.uber.org/multierr v1.11.0 // indirect

cmd/mdatagen/go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/configauth/go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/davecgh/go-spew v1.1.1 // indirect
1515
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
1616
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/hashicorp/go-version v1.7.0 // indirect
1718
github.com/knadh/koanf/maps v0.1.1 // indirect
1819
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
1920
github.com/knadh/koanf/v2 v2.1.1 // indirect
@@ -22,6 +23,7 @@ require (
2223
github.com/pmezard/go-difflib v1.0.0 // indirect
2324
go.opentelemetry.io/collector/config/configtelemetry v0.102.1 // indirect
2425
go.opentelemetry.io/collector/confmap v0.102.1 // indirect
26+
go.opentelemetry.io/collector/featuregate v1.9.0 // indirect
2527
go.opentelemetry.io/collector/pdata v1.9.0 // indirect
2628
go.opentelemetry.io/otel v1.27.0 // indirect
2729
go.opentelemetry.io/otel/metric v1.27.0 // indirect
@@ -48,3 +50,5 @@ replace go.opentelemetry.io/collector/config/configtelemetry => ../configtelemet
4850
replace go.opentelemetry.io/collector/extension => ../../extension
4951

5052
replace go.opentelemetry.io/collector/extension/auth => ../../extension/auth
53+
54+
replace go.opentelemetry.io/collector/featuregate => ../../featuregate

config/configauth/go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

confmap/confmap.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/knadh/koanf/providers/confmap"
1717
"github.com/knadh/koanf/v2"
1818

19+
"go.opentelemetry.io/collector/confmap/internal"
1920
encoder "go.opentelemetry.io/collector/confmap/internal/mapstructure"
2021
)
2122

@@ -156,7 +157,7 @@ func decodeConfig(m *Conf, result any, errorUnused bool, skipTopLevelUnmarshaler
156157
ErrorUnused: errorUnused,
157158
Result: result,
158159
TagName: "mapstructure",
159-
WeaklyTypedInput: true,
160+
WeaklyTypedInput: !internal.StrictlyTypedInputGate.IsEnabled(),
160161
MatchName: caseSensitiveMatchName,
161162
DecodeHook: mapstructure.ComposeDecodeHookFunc(
162163
expandNilStructPointersHookFunc(),

confmap/expand.go

+23-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"regexp"
1212
"strconv"
1313
"strings"
14+
15+
"go.opentelemetry.io/collector/confmap/internal"
1416
)
1517

1618
// schemePattern defines the regexp pattern for scheme names.
@@ -111,7 +113,12 @@ func (mr *Resolver) findAndExpandURI(ctx context.Context, input string) (any, bo
111113
if uri == input {
112114
// If the value is a single URI, then the return value can be anything.
113115
// This is the case `foo: ${file:some_extra_config.yml}`.
114-
expanded, err := mr.expandURI(ctx, input)
116+
ret, err := mr.expandURI(ctx, input)
117+
if err != nil {
118+
return input, false, err
119+
}
120+
121+
expanded, err := ret.AsRaw()
115122
if err != nil {
116123
return input, false, err
117124
}
@@ -121,16 +128,27 @@ func (mr *Resolver) findAndExpandURI(ctx context.Context, input string) (any, bo
121128
if err != nil {
122129
return input, false, err
123130
}
124-
repl, err := toString(expanded)
131+
132+
var repl string
133+
if internal.StrictlyTypedInputGate.IsEnabled() {
134+
repl, err = expanded.AsString()
135+
} else {
136+
repl, err = toString(expanded)
137+
}
125138
if err != nil {
126139
return input, false, fmt.Errorf("expanding %v: %w", uri, err)
127140
}
128141
return strings.ReplaceAll(input, uri, repl), true, err
129142
}
130143

131144
// toString attempts to convert input to a string.
132-
func toString(input any) (string, error) {
145+
func toString(ret *Retrieved) (string, error) {
133146
// This list must be kept in sync with checkRawConfType.
147+
input, err := ret.AsRaw()
148+
if err != nil {
149+
return "", err
150+
}
151+
134152
val := reflect.ValueOf(input)
135153
switch val.Kind() {
136154
case reflect.String:
@@ -146,7 +164,7 @@ func toString(input any) (string, error) {
146164
}
147165
}
148166

149-
func (mr *Resolver) expandURI(ctx context.Context, input string) (any, error) {
167+
func (mr *Resolver) expandURI(ctx context.Context, input string) (*Retrieved, error) {
150168
// strip ${ and }
151169
uri := input[2 : len(input)-1]
152170

@@ -167,7 +185,7 @@ func (mr *Resolver) expandURI(ctx context.Context, input string) (any, error) {
167185
return nil, err
168186
}
169187
mr.closers = append(mr.closers, ret.Close)
170-
return ret.AsRaw()
188+
return ret, nil
171189
}
172190

173191
type location struct {

confmap/go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/knadh/koanf/providers/confmap v0.1.0
99
github.com/knadh/koanf/v2 v2.1.1
1010
github.com/stretchr/testify v1.9.0
11+
go.opentelemetry.io/collector/featuregate v1.9.0
1112
go.uber.org/goleak v1.3.0
1213
go.uber.org/multierr v1.11.0
1314
go.uber.org/zap v1.27.0
@@ -16,15 +17,15 @@ require (
1617

1718
require (
1819
github.com/davecgh/go-spew v1.1.1 // indirect
19-
github.com/kr/pretty v0.3.1 // indirect
20+
github.com/hashicorp/go-version v1.7.0 // indirect
2021
github.com/mitchellh/copystructure v1.2.0 // indirect
2122
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2223
github.com/pmezard/go-difflib v1.0.0 // indirect
23-
github.com/rogpeppe/go-internal v1.10.0 // indirect
24-
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
2524
)
2625

2726
retract (
2827
v0.76.0 // Depends on retracted pdata v1.0.0-rc10 module, use v0.76.1
2928
v0.69.0 // Release failed, use v0.69.1
3029
)
30+
31+
replace go.opentelemetry.io/collector/featuregate => ../featuregate

confmap/go.sum

+2-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

confmap/internal/e2e/go.mod

+4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ require (
77
go.opentelemetry.io/collector/confmap v0.102.1
88
go.opentelemetry.io/collector/confmap/provider/envprovider v0.102.1
99
go.opentelemetry.io/collector/confmap/provider/fileprovider v0.102.1
10+
go.opentelemetry.io/collector/featuregate v1.9.0
1011
)
1112

1213
require (
1314
github.com/davecgh/go-spew v1.1.1 // indirect
1415
github.com/go-viper/mapstructure/v2 v2.0.0 // indirect
16+
github.com/hashicorp/go-version v1.7.0 // indirect
1517
github.com/knadh/koanf/maps v0.1.1 // indirect
1618
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
1719
github.com/knadh/koanf/v2 v2.1.1 // indirect
@@ -28,3 +30,5 @@ replace go.opentelemetry.io/collector/confmap => ../../
2830
replace go.opentelemetry.io/collector/confmap/provider/fileprovider => ../../provider/fileprovider
2931

3032
replace go.opentelemetry.io/collector/confmap/provider/envprovider => ../../provider/envprovider
33+
34+
replace go.opentelemetry.io/collector/featuregate => ../../../featuregate

confmap/internal/e2e/go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)