Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[confmap] Support expansion for environment variables with time formats #11241

Merged
26 changes: 26 additions & 0 deletions .chloggen/support-time-envvar-expansion.yaml
Original file line number Diff line number Diff line change
@@ -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: enhancement

# 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: Allow using any YAML structure as a string when loading configuration including time.Time formats

# One or more tracking issues or pull requests related to the change
issues: [10659]

# (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: |
Previously, fields with time.Time formats could not be used as strings in configurations

# 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: []
7 changes: 7 additions & 0 deletions confmap/expand_test.go
Original file line number Diff line number Diff line change
@@ -185,6 +185,11 @@ func TestResolverExpandStringValues(t *testing.T) {
input: "test_${env:BOOL}",
output: "test_true",
},
{
name: "Timestamp",
input: "test_${env:TIMESTAMP}",
output: "test_2023-03-20T03:17:55.432328Z",
},
{
name: "MultipleSameMatches",
input: "test_${env:BOOL}_test_${env:BOOL}",
@@ -414,6 +419,8 @@ func newEnvProvider() ProviderFactory {
return NewRetrievedFromYAML([]byte("[localhost:3042]"))
case "env:HOST":
return NewRetrievedFromYAML([]byte("localhost"))
case "env:TIMESTAMP":
return NewRetrievedFromYAML([]byte("2023-03-20T03:17:55.432328Z"))
case "env:OS":
return NewRetrievedFromYAML([]byte("ubuntu"))
case "env:PR":
10 changes: 10 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
@@ -197,6 +197,16 @@ func TestStrictTypeCasting(t *testing.T) {
targetField: TargetFieldInlineString,
expected: "inline field with 2006-01-02T15:04:05Z07:00 expansion",
},
{
value: "2023-03-20T03:17:55.432328Z",
targetField: TargetFieldString,
expected: "2023-03-20T03:17:55.432328Z",
},
{
value: "2023-03-20T03:17:55.432328Z",
targetField: TargetFieldInlineString,
expected: "inline field with 2023-03-20T03:17:55.432328Z expansion",
},
// issue 10787
{
value: "true # comment with a ${env:hello.world} reference",
17 changes: 0 additions & 17 deletions confmap/provider.go
Original file line number Diff line number Diff line change
@@ -165,9 +165,6 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...RetrievedOption) (*Retrieved
// - []any;
// - map[string]any;
func NewRetrieved(rawConf any, opts ...RetrievedOption) (*Retrieved, error) {
if err := checkRawConfType(rawConf); err != nil {
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
return nil, err
}
set := retrievedSettings{}
for _, opt := range opts {
opt.apply(&set)
@@ -229,17 +226,3 @@ func (r *Retrieved) Close(ctx context.Context) error {

// CloseFunc a function equivalent to Retrieved.Close.
type CloseFunc func(context.Context) error

func checkRawConfType(rawConf any) error {
if rawConf == nil {
return nil
}
switch rawConf.(type) {
case int, int32, int64, float32, float64, bool, string, []any, map[string]any:
return nil
default:
return fmt.Errorf(
"unsupported type=%T for retrieved config,"+
" ensure that values are wrapped in quotes", rawConf)
}
}
10 changes: 5 additions & 5 deletions confmap/provider_test.go
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -31,11 +32,6 @@ func TestNewRetrievedWithOptions(t *testing.T) {
assert.Equal(t, want, ret.Close(context.Background()))
}

func TestNewRetrievedUnsupportedType(t *testing.T) {
_, err := NewRetrieved(errors.New("my error"))
require.Error(t, err)
}

func TestNewRetrievedFromYAML(t *testing.T) {
ret, err := NewRetrievedFromYAML([]byte{})
require.NoError(t, err)
@@ -103,6 +99,10 @@ func TestNewRetrievedFromYAMLString(t *testing.T) {
yaml: "123",
value: 123,
},
{
yaml: "2023-03-20T03:17:55.432328Z",
value: time.Date(2023, 3, 20, 3, 17, 55, 432328000, time.UTC),
},
{
yaml: "true",
value: true,
Loading