Skip to content

Commit 53be17e

Browse files
theomagellanmx-psi
andauthored
Add new experimental toStringMapRaw method to xconfmap (open-telemetry#14480)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This experimental method allows for stringmap manipulations without destroying internal types like `ExpandedValue`. Destroying these internal values could lead to unrecoverable errors like having an env var fully numeric but expected as string. Using `ToStringMap` would permanently flatten the `ExpandedValue` into its `Value` type regardless of intent. <!--Describe what testing was performed and which tests were added.--> #### Testing Added tests. --------- Co-authored-by: Pablo Baeyens <pbaeyens31+github@gmail.com>
1 parent f5333c7 commit 53be17e

5 files changed

Lines changed: 74 additions & 1 deletion

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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. receiver/otlp)
7+
component: pkg/confmap
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add experimental `ToStringMapRaw` function to decode `confmap.Conf` into a string map without losing internal types
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14480]
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+
This method exposes the internal structure of a `confmap.Conf` which may change at any time without prior notice
20+
21+
# Optional: The change log or logs in which this entry should be included.
22+
# e.g. '[user]' or '[user, api]'
23+
# Include 'user' if the change is relevant to end users.
24+
# Include 'api' if there is a change to a library API.
25+
# Default: '[user]'
26+
change_logs: []

confmap/internal/conf.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ func (l *Conf) ToStringMap() map[string]any {
182182
return sanitize(l.toStringMapWithExpand()).(map[string]any)
183183
}
184184

185+
func ToStringMapRaw(conf *Conf) map[string]any {
186+
return conf.toStringMapWithExpand()
187+
}
188+
185189
func (l *Conf) unsanitizedGet(key string) any {
186190
return l.k.Get(key)
187191
}

confmap/internal/e2e/expand_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212
"github.com/stretchr/testify/require"
1313

1414
"go.opentelemetry.io/collector/confmap"
15+
"go.opentelemetry.io/collector/confmap/internal"
1516
"go.opentelemetry.io/collector/confmap/provider/envprovider"
1617
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
18+
"go.opentelemetry.io/collector/confmap/xconfmap"
1719
)
1820

1921
func Test_EscapedEnvVars_NoDefaultScheme(t *testing.T) {
@@ -111,3 +113,17 @@ func Test_EscapedEnvVars_DefaultScheme(t *testing.T) {
111113
m := cfgMap.ToStringMap()
112114
assert.Equal(t, expectedMap, m)
113115
}
116+
117+
func Test_RawConfMap(t *testing.T) {
118+
data := map[string]any{
119+
"value": internal.ExpandedValue{
120+
Value: 8080,
121+
Original: "8080",
122+
},
123+
}
124+
conf := confmap.NewFromStringMap(data)
125+
126+
rawData := xconfmap.ToStringMapRaw(conf)
127+
_, isPresentAndExpandedValue := rawData["value"].(internal.ExpandedValue)
128+
require.True(t, isPresentAndExpandedValue)
129+
}

confmap/internal/e2e/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
go.opentelemetry.io/collector/confmap v1.51.0
99
go.opentelemetry.io/collector/confmap/provider/envprovider v1.51.0
1010
go.opentelemetry.io/collector/confmap/provider/fileprovider v1.51.0
11+
go.opentelemetry.io/collector/confmap/xconfmap v0.145.0
1112
go.opentelemetry.io/collector/featuregate v1.51.0
1213
)
1314

@@ -22,7 +23,6 @@ require (
2223
github.com/mitchellh/copystructure v1.2.0 // indirect
2324
github.com/mitchellh/reflectwalk v1.0.2 // indirect
2425
github.com/pmezard/go-difflib v1.0.0 // indirect
25-
go.opentelemetry.io/collector/confmap/xconfmap v0.145.0 // indirect
2626
go.uber.org/multierr v1.11.0 // indirect
2727
go.uber.org/zap v1.27.1 // indirect
2828
go.yaml.in/yaml/v3 v3.0.4 // indirect

confmap/xconfmap/confmap.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package xconfmap // import "go.opentelemetry.io/collector/confmap/xconfmap"
5+
6+
import (
7+
"go.opentelemetry.io/collector/confmap"
8+
"go.opentelemetry.io/collector/confmap/internal"
9+
)
10+
11+
// ExpandedValue represents a configuration value that has been expanded from a template
12+
// (e.g., environment variable substitution). It contains both the parsed value and the
13+
// original string representation.
14+
//
15+
// This type is exposed to allow working with configuration values returned by ToStringMapRaw.
16+
type ExpandedValue = internal.ExpandedValue
17+
18+
// ToStringMapRaw returns the raw configuration map without sanitization.
19+
// This is an experimental API and may change or be removed in future versions.
20+
// The returned map may change at any time without prior notice.
21+
//
22+
// Unlike confmap.Conf.ToStringMap(), this function does not sanitize the map
23+
// by removing expandedValue references. This allows for configmap manipulation
24+
// without destroying internal types.
25+
func ToStringMapRaw(conf *confmap.Conf) map[string]any {
26+
return internal.ToStringMapRaw(conf)
27+
}

0 commit comments

Comments
 (0)