Skip to content

Commit 7a7d61b

Browse files
odubajDTedmocosta
andauthored
[pkg/ottl] introduce Weekday converter function (#38264)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Introduce Weekday converter <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes #38126 --------- Signed-off-by: odubajDT <[email protected]> Co-authored-by: Edmo Vamerlatti Costa <[email protected]>
1 parent 8a5f4cc commit 7a7d61b

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed

.chloggen/ottl-weekday-converter.yaml

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: "Introduce Weekday() converter function"
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: [38126]
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: []

pkg/ottl/ottlfuncs/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ Available Converters:
480480
- [UnixSeconds](#unixseconds)
481481
- [UserAgent](#useragent)
482482
- [UUID](#UUID)
483+
- [Weekday](#weekday)
483484
- [Year](#year)
484485

485486
### Base64Decode (Deprecated)
@@ -2277,6 +2278,22 @@ results in
22772278
22782279
The `UUID` function generates a v4 uuid string.
22792280
2281+
### Weekday
2282+
2283+
`Weekday(value)`
2284+
2285+
The `Weekday` Converter returns the day of the week component from the specified time using the Go stdlib [`time.Weekday` function](https://pkg.go.dev/time#Time.Weekday).
2286+
2287+
`value` is a `time.Time`. If `value` is another type, an error is returned.
2288+
2289+
The returned type is `int64`.
2290+
2291+
The returned range is 0-6 (Sun-Sat)
2292+
2293+
Examples:
2294+
2295+
- `Weekday(Now())`
2296+
22802297
### Year
22812298
22822299
`Year(value)`

pkg/ottl/ottlfuncs/func_weekday.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
11+
)
12+
13+
type WeekdayArguments[K any] struct {
14+
Time ottl.TimeGetter[K]
15+
}
16+
17+
func NewWeekdayFactory[K any]() ottl.Factory[K] {
18+
return ottl.NewFactory("Weekday", &WeekdayArguments[K]{}, createWeekdayFunction[K])
19+
}
20+
21+
func createWeekdayFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
22+
args, ok := oArgs.(*WeekdayArguments[K])
23+
24+
if !ok {
25+
return nil, fmt.Errorf("WeekdayFactory args must be of type *WeekdayArguments[K]")
26+
}
27+
28+
return Weekday(args.Time)
29+
}
30+
31+
func Weekday[K any](time ottl.TimeGetter[K]) (ottl.ExprFunc[K], error) {
32+
return func(ctx context.Context, tCtx K) (any, error) {
33+
t, err := time.Get(ctx, tCtx)
34+
if err != nil {
35+
return nil, err
36+
}
37+
return int64(t.Weekday()), nil
38+
}, nil
39+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"context"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
13+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
14+
)
15+
16+
func Test_Weekday(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
time ottl.TimeGetter[any]
20+
expected int64
21+
}{
22+
{
23+
name: "Mon",
24+
time: &ottl.StandardTimeGetter[any]{
25+
Getter: func(_ context.Context, _ any) (any, error) {
26+
return time.Date(2025, time.February, 24, 15, 4, 5, 0, time.UTC), nil
27+
},
28+
},
29+
expected: 1,
30+
},
31+
{
32+
name: "Tue",
33+
time: &ottl.StandardTimeGetter[any]{
34+
Getter: func(_ context.Context, _ any) (any, error) {
35+
return time.Date(2025, time.February, 25, 15, 4, 5, 0, time.UTC), nil
36+
},
37+
},
38+
expected: 2,
39+
},
40+
{
41+
name: "Wed",
42+
time: &ottl.StandardTimeGetter[any]{
43+
Getter: func(_ context.Context, _ any) (any, error) {
44+
return time.Date(2025, time.February, 26, 15, 4, 5, 0, time.UTC), nil
45+
},
46+
},
47+
expected: 3,
48+
},
49+
{
50+
name: "Thu",
51+
time: &ottl.StandardTimeGetter[any]{
52+
Getter: func(_ context.Context, _ any) (any, error) {
53+
return time.Date(2025, time.February, 27, 15, 4, 5, 0, time.UTC), nil
54+
},
55+
},
56+
expected: 4,
57+
},
58+
{
59+
name: "Fri",
60+
time: &ottl.StandardTimeGetter[any]{
61+
Getter: func(_ context.Context, _ any) (any, error) {
62+
return time.Date(2025, time.February, 28, 15, 4, 5, 0, time.UTC), nil
63+
},
64+
},
65+
expected: 5,
66+
},
67+
{
68+
name: "Sat",
69+
time: &ottl.StandardTimeGetter[any]{
70+
Getter: func(_ context.Context, _ any) (any, error) {
71+
return time.Date(2025, time.February, 22, 15, 4, 5, 0, time.UTC), nil
72+
},
73+
},
74+
expected: 6,
75+
},
76+
{
77+
name: "Sun",
78+
time: &ottl.StandardTimeGetter[any]{
79+
Getter: func(_ context.Context, _ any) (any, error) {
80+
return time.Date(2025, time.February, 23, 15, 4, 5, 0, time.UTC), nil
81+
},
82+
},
83+
expected: 0,
84+
},
85+
}
86+
for _, tt := range tests {
87+
t.Run(tt.name, func(t *testing.T) {
88+
exprFunc, err := Weekday(tt.time)
89+
assert.NoError(t, err)
90+
result, err := exprFunc(nil, nil)
91+
assert.NoError(t, err)
92+
assert.Equal(t, tt.expected, result)
93+
})
94+
}
95+
}
96+
97+
func Test_Weekday_Error(t *testing.T) {
98+
var getter ottl.TimeGetter[any] = &ottl.StandardTimeGetter[any]{
99+
Getter: func(_ context.Context, _ any) (any, error) {
100+
return "not a time", nil
101+
},
102+
}
103+
exprFunc, err := Weekday(getter)
104+
assert.NoError(t, err)
105+
result, err := exprFunc(context.Background(), nil)
106+
assert.Nil(t, result)
107+
assert.Error(t, err)
108+
}

pkg/ottl/ottlfuncs/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func converters[K any]() []ottl.Factory[K] {
105105
NewUnixSecondsFactory[K](),
106106
NewUUIDFactory[K](),
107107
NewURLFactory[K](),
108+
NewWeekdayFactory[K](),
108109
NewUserAgentFactory[K](),
109110
NewAppendFactory[K](),
110111
NewYearFactory[K](),

0 commit comments

Comments
 (0)