diff --git a/.chloggen/support-time-envvar-expansion.yaml b/.chloggen/support-time-envvar-expansion.yaml new file mode 100644 index 00000000000..c126c2fafee --- /dev/null +++ b/.chloggen/support-time-envvar-expansion.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: 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: [] diff --git a/confmap/expand_test.go b/confmap/expand_test.go index a9a3b49b850..d634564d5b6 100644 --- a/confmap/expand_test.go +++ b/confmap/expand_test.go @@ -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": diff --git a/confmap/internal/e2e/types_test.go b/confmap/internal/e2e/types_test.go index 6085ba0a438..6e335daaa6c 100644 --- a/confmap/internal/e2e/types_test.go +++ b/confmap/internal/e2e/types_test.go @@ -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", diff --git a/confmap/provider.go b/confmap/provider.go index daf508bf925..c462b9bb6fe 100644 --- a/confmap/provider.go +++ b/confmap/provider.go @@ -6,6 +6,7 @@ package confmap // import "go.opentelemetry.io/collector/confmap" import ( "context" "fmt" + "time" "go.uber.org/zap" "gopkg.in/yaml.v3" @@ -235,7 +236,7 @@ func checkRawConfType(rawConf any) error { return nil } switch rawConf.(type) { - case int, int32, int64, float32, float64, bool, string, []any, map[string]any: + case int, int32, int64, float32, float64, bool, string, []any, map[string]any, time.Time: return nil default: return fmt.Errorf( diff --git a/confmap/provider_test.go b/confmap/provider_test.go index e82ddf90355..3877222865c 100644 --- a/confmap/provider_test.go +++ b/confmap/provider_test.go @@ -7,6 +7,7 @@ import ( "context" "errors" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -103,6 +104,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,