Skip to content

Commit

Permalink
[confmap] Support expansion for environment variables with time forma…
Browse files Browse the repository at this point in the history
…ts (#11241)

#### Description

This addresses
#10659.
In #10800
we removed restrictions on the types that can be allowed if expanded but
in our case, this still fails because of the checks existing in
`provider.checkRawConfType()`

This adds `time.Time` as a type in the `provider.checkRawConfType()`
instead of removing it completely.

#### Link to tracking issue
Fixes
#10659

<!--Describe what testing was performed and which tests were added.-->
#### Testing
- added a test case to handle time formats in provider.go and expand.go
- added an e2e test case to handle time formats
  • Loading branch information
irisgve authored Sep 27, 2024
1 parent 5cc717d commit 431fd11
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
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

0 comments on commit 431fd11

Please sign in to comment.