Skip to content

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

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -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}",
Expand Down Expand Up @@ -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":
Expand Down
10 changes: 10 additions & 0 deletions confmap/internal/e2e/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion confmap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package confmap // import "go.opentelemetry.io/collector/confmap"
import (
"context"
"fmt"
"time"

"go.uber.org/zap"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions confmap/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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,
Expand Down
Loading