From 86f36f45cc0e7a3906a1040a3248a5bb833092fa Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Fri, 12 Jul 2024 18:51:05 +0200 Subject: [PATCH 1/3] [confmap] Use original string value on expansion --- .chloggen/mx-psi_as-string.yaml | 26 ++++++++++++++++++++++++++ confmap/internal/e2e/types_test.go | 10 +++++----- confmap/provider.go | 10 ++++++++-- confmap/provider_test.go | 4 ++-- docs/rfcs/env-vars.md | 4 ++-- 5 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 .chloggen/mx-psi_as-string.yaml diff --git a/.chloggen/mx-psi_as-string.yaml b/.chloggen/mx-psi_as-string.yaml new file mode 100644 index 00000000000..d5c81570549 --- /dev/null +++ b/.chloggen/mx-psi_as-string.yaml @@ -0,0 +1,26 @@ +# Use this changelog template to create an entry for release notes. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: bug_fix + +# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) +component: confmap + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: When passing value to a string field through the `${:URI}` or `${URI}` syntax, the original raw string value is now preserved. + +# One or more tracking issues or pull requests related to the change +issues: [] + +# (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: | + - This change only applies under the `confmap.unifyEnvVarExpansion` feature gate. + +# Optional: The change log or logs in which this entry should be included. +# e.g. '[user]' or '[user, api]' +# Include 'user' if the change is relevant to end users. +# Include 'api' if there is a change to a library API. +# Default: '[user]' +change_logs: [] diff --git a/confmap/internal/e2e/types_test.go b/confmap/internal/e2e/types_test.go index e57d3f8af53..a77824fa6ff 100644 --- a/confmap/internal/e2e/types_test.go +++ b/confmap/internal/e2e/types_test.go @@ -228,27 +228,27 @@ func TestStrictTypeCasting(t *testing.T) { { value: "\"0123\"", targetField: TargetFieldString, - expected: "0123", + expected: "\"0123\"", }, { value: "\"0123\"", targetField: TargetFieldInt, - unmarshalErr: "'field' expected type 'int', got unconvertible type 'string', value: '0123'", + unmarshalErr: "'field' expected type 'int', got unconvertible type 'string', value: '\"0123\"'", }, { value: "\"0123\"", targetField: TargetFieldInlineString, - expected: "inline field with 0123 expansion", + expected: "inline field with \"0123\" expansion", }, { value: "!!str 0123", targetField: TargetFieldString, - expected: "0123", + expected: "!!str 0123", }, { value: "!!str 0123", targetField: TargetFieldInlineString, - expected: "inline field with 0123 expansion", + expected: "inline field with !!str 0123 expansion", }, { value: "t", diff --git a/confmap/provider.go b/confmap/provider.go index f774cca3834..b1fc7b759b3 100644 --- a/confmap/provider.go +++ b/confmap/provider.go @@ -7,6 +7,7 @@ import ( "context" "fmt" + "go.opentelemetry.io/collector/internal/featuregates" "go.uber.org/zap" "gopkg.in/yaml.v3" ) @@ -138,9 +139,14 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved return nil, err } - switch v := rawConf.(type) { + switch rawConf.(type) { case string: - opts = append(opts, withStringRepresentation(v)) + if featuregates.UseUnifiedEnvVarExpansionRules.IsEnabled() { + // If rawConf is a string, we override it with the raw yamlBytes. + // This is the original ${ENV} expansion behavior. + rawConf = string(yamlBytes) + } + opts = append(opts, withStringRepresentation(string(yamlBytes))) case int, int32, int64, float32, float64, bool: opts = append(opts, withStringRepresentation(string(yamlBytes))) } diff --git a/confmap/provider_test.go b/confmap/provider_test.go index e43b5608096..791b6d81f15 100644 --- a/confmap/provider_test.go +++ b/confmap/provider_test.go @@ -85,8 +85,8 @@ func TestNewRetrievedFromYAMLString(t *testing.T) { }, { yaml: "\"string\"", - value: "string", - altStrRepr: "string", + value: "\"string\"", + altStrRepr: "\"string\"", }, { yaml: "123", diff --git a/docs/rfcs/env-vars.md b/docs/rfcs/env-vars.md index a2709412788..7f17b933a52 100644 --- a/docs/rfcs/env-vars.md +++ b/docs/rfcs/env-vars.md @@ -216,7 +216,7 @@ loading a field with the braces syntax, `env` syntax. | `0123` | integer | 83 | 83 | 83 | n/a | | `0123` | string | 0123 | 83 | 0123 | 0123 | | `0xdeadbeef` | string | 0xdeadbeef | 3735928559 | 0xdeadbeef | 0xdeadbeef | -| `"0123"` | string | "0123" | 0123 | 0123 | 0123 | -| `!!str 0123` | string | !!str 0123 | 0123 | 0123 | 0123 | +| `"0123"` | string | "0123" | 0123 | "0123" | "0123" | +| `!!str 0123` | string | !!str 0123 | 0123 | !!str 0123 | !!str 0123 | | `t` | boolean | true | true | Error: mapping string to bool | n/a | | `23` | boolean | true | true | Error: mapping integer to bool | n/a | From ec4c6e8408f9fec3dd767253e2490db334b6825c Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Fri, 12 Jul 2024 19:00:10 +0200 Subject: [PATCH 2/3] Add issue number --- .chloggen/mx-psi_as-string.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/mx-psi_as-string.yaml b/.chloggen/mx-psi_as-string.yaml index d5c81570549..cc4c3b4cff1 100644 --- a/.chloggen/mx-psi_as-string.yaml +++ b/.chloggen/mx-psi_as-string.yaml @@ -10,7 +10,7 @@ component: confmap note: When passing value to a string field through the `${:URI}` or `${URI}` syntax, the original raw string value is now preserved. # One or more tracking issues or pull requests related to the change -issues: [] +issues: [10603] # (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. From 56e957797580d46c3197b54f834ee19fde2b7b74 Mon Sep 17 00:00:00 2001 From: Pablo Baeyens Date: Fri, 12 Jul 2024 19:04:42 +0200 Subject: [PATCH 3/3] Fix tests --- confmap/internal/e2e/types_test.go | 13 ++++--------- confmap/provider.go | 3 ++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/confmap/internal/e2e/types_test.go b/confmap/internal/e2e/types_test.go index a77824fa6ff..c5d38630c33 100644 --- a/confmap/internal/e2e/types_test.go +++ b/confmap/internal/e2e/types_test.go @@ -98,27 +98,22 @@ func TestTypeCasting(t *testing.T) { { value: "\"0123\"", targetField: TargetFieldString, - expected: "0123", - }, - { - value: "\"0123\"", - targetField: TargetFieldInt, - expected: 83, + expected: "\"0123\"", }, { value: "\"0123\"", targetField: TargetFieldInlineString, - expected: "inline field with 0123 expansion", + expected: "inline field with \"0123\" expansion", }, { value: "!!str 0123", targetField: TargetFieldString, - expected: "0123", + expected: "!!str 0123", }, { value: "!!str 0123", targetField: TargetFieldInlineString, - expected: "inline field with 0123 expansion", + expected: "inline field with !!str 0123 expansion", }, { value: "t", diff --git a/confmap/provider.go b/confmap/provider.go index b1fc7b759b3..ea94aec3f87 100644 --- a/confmap/provider.go +++ b/confmap/provider.go @@ -7,9 +7,10 @@ import ( "context" "fmt" - "go.opentelemetry.io/collector/internal/featuregates" "go.uber.org/zap" "gopkg.in/yaml.v3" + + "go.opentelemetry.io/collector/internal/featuregates" ) // ProviderSettings are the settings to initialize a Provider.