Skip to content

Commit 831984b

Browse files
authored
[pkg/ottl] Adapt time parsing error messages to include the ctime directives (#38425)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description This PR adapts the `Time` function to check for `time.ParseError` errors. If such an error is received by the function, it wraps it into a custom error type in order to include the provided ctime layout string instead of the go-native layout string. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #35176 <!--Describe what testing was performed and which tests were added.--> #### Testing Adapted the unit tests of the `Time()` function --------- Signed-off-by: Florian Bacher <[email protected]>
1 parent 0110fd6 commit 831984b

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Improve time parsing error messages by including the ctime directive instead of the go time layout
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [35176]
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+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,17 @@ func Validate(format string) error {
163163
}
164164
return nil
165165
}
166+
167+
// GetNativeSubstitutes analyzes the provided format string and returns a map where each
168+
// key is a Go native layout element (as used in time.Format) found in the format, and
169+
// each value is the corresponding ctime-like directive.
170+
func GetNativeSubstitutes(format string) map[string]string {
171+
nativeDirectives := map[string]string{}
172+
directives := ctimeRegexp.FindAllString(format, -1)
173+
for _, directive := range directives {
174+
if val, ok := ctimeSubstitutes[directive]; ok {
175+
nativeDirectives[val] = directive
176+
}
177+
}
178+
return nativeDirectives
179+
}

internal/coreinternal/timeutils/internal/ctimefmt/ctimefmt_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,41 @@ func TestValidate(t *testing.T) {
115115
})
116116
}
117117
}
118+
119+
func TestGetNativeSubstitutes(t *testing.T) {
120+
type args struct {
121+
format string
122+
}
123+
tests := []struct {
124+
name string
125+
args args
126+
want map[string]string
127+
}{
128+
{
129+
name: "get ctime directives",
130+
args: args{
131+
format: "%Y-%m-%d %H:%M:%S.%f",
132+
},
133+
want: map[string]string{"01": "%m", "02": "%d", "04": "%M", "05": "%S", "15": "%H", "2006": "%Y", "999999": "%f"},
134+
},
135+
{
136+
name: "format contains unsupported directive",
137+
args: args{
138+
format: "%C-%m-%d-%H-%M-%S.%L",
139+
},
140+
want: map[string]string{"01": "%m", "02": "%d", "04": "%M", "05": "%S", "15": "%H", "999": "%L"},
141+
},
142+
{
143+
name: "format contains Go layout elements",
144+
args: args{
145+
format: "2006-%m-%d",
146+
},
147+
want: map[string]string{"01": "%m", "02": "%d"},
148+
},
149+
}
150+
for _, tt := range tests {
151+
t.Run(tt.name, func(t *testing.T) {
152+
assert.Equalf(t, tt.want, GetNativeSubstitutes(tt.args.format), "GetNativeSubstitutes(%v)", tt.args.format)
153+
})
154+
}
155+
}

internal/coreinternal/timeutils/parser.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"regexp"
10+
"strconv"
1011
"strings"
1112
"time"
1213

@@ -178,5 +179,37 @@ func ValidateLocale(locale string) error {
178179
return fmt.Errorf("invalid locale '%s': %w", locale, err)
179180
}
180181

182+
// GetStrptimeNativeSubstitutes analyzes the provided format string and returns a map
183+
// where each key is a Go native layout element (as used in time.Format) found in the
184+
// format, and each value is the corresponding ctime-like directive.
185+
func GetStrptimeNativeSubstitutes(format string) map[string]string {
186+
return strptime.GetNativeSubstitutes(format)
187+
}
188+
189+
type strptimeParseErr struct {
190+
err *time.ParseError
191+
ctimeLayout string
192+
nativeSubstitutes map[string]string
193+
}
194+
195+
func (e *strptimeParseErr) Error() string {
196+
if e.err.Message == "" {
197+
layoutElem, ok := e.nativeSubstitutes[e.err.LayoutElem]
198+
if !ok {
199+
layoutElem = e.err.LayoutElem
200+
}
201+
return "parsing time " +
202+
strconv.Quote(e.err.Value) + " as " +
203+
strconv.Quote(e.ctimeLayout) + ": cannot parse " +
204+
strconv.Quote(e.err.ValueElem) + " as " +
205+
strconv.Quote(layoutElem)
206+
}
207+
return "parsing time " + strconv.Quote(e.err.Value) + e.err.Message
208+
}
209+
210+
func ToStrptimeParseError(err *time.ParseError, ctimeLayout string, nativeSubstitutes map[string]string) error {
211+
return &strptimeParseErr{err, ctimeLayout, nativeSubstitutes}
212+
}
213+
181214
// Allows tests to override with deterministic value
182215
var Now = time.Now

pkg/ottl/ottlfuncs/func_time.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func Time[K any](inputTime ottl.StringGetter[K], format string, location ottl.Op
6262
inputTimeLocale = &l
6363
}
6464

65+
ctimeSubstitutes := timeutils.GetStrptimeNativeSubstitutes(format)
66+
6567
return func(ctx context.Context, tCtx K) (any, error) {
6668
t, err := inputTime.Get(ctx, tCtx)
6769
if err != nil {
@@ -77,6 +79,10 @@ func Time[K any](inputTime ottl.StringGetter[K], format string, location ottl.Op
7779
timestamp, err = timeutils.ParseGotime(gotimeFormat, t, loc)
7880
}
7981
if err != nil {
82+
var timeErr *time.ParseError
83+
if errors.As(err, &timeErr) {
84+
return nil, timeutils.ToStrptimeParseError(timeErr, format, ctimeSubstitutes)
85+
}
8086
return nil, err
8187
}
8288
return timestamp, nil

pkg/ottl/ottlfuncs/func_time_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func Test_TimeError(t *testing.T) {
258258
},
259259
},
260260
format: "%Y/%m/%d",
261-
expectedError: "cannot parse",
261+
expectedError: `parsing time "11/11/11" as "%Y/%m/%d": cannot parse "11/11/11" as "%Y"`,
262262
},
263263
{
264264
name: "invalid RFC3339 with no time",

0 commit comments

Comments
 (0)