Skip to content
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
27 changes: 27 additions & 0 deletions .chloggen/ottl-weekday-converter.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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. filelogreceiver)
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Introduce Weekday() converter function"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [38126]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# 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: []
17 changes: 17 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ Available Converters:
- [UnixSeconds](#unixseconds)
- [UserAgent](#useragent)
- [UUID](#UUID)
- [Weekday](#weekday)
- [Year](#year)

### Base64Decode (Deprecated)
Expand Down Expand Up @@ -2277,6 +2278,22 @@ results in

The `UUID` function generates a v4 uuid string.

### Weekday

`Weekday(value)`

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).

`value` is a `time.Time`. If `value` is another type, an error is returned.

The returned type is `int64`.

The returned range is 0-6 (Sun-Sat)

Examples:

- `Weekday(Now())`

### Year

`Year(value)`
Expand Down
39 changes: 39 additions & 0 deletions pkg/ottl/ottlfuncs/func_weekday.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"

import (
"context"
"fmt"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

type WeekdayArguments[K any] struct {
Time ottl.TimeGetter[K]
}

func NewWeekdayFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("Weekday", &WeekdayArguments[K]{}, createWeekdayFunction[K])
}

func createWeekdayFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*WeekdayArguments[K])

if !ok {
return nil, fmt.Errorf("WeekdayFactory args must be of type *WeekdayArguments[K]")
}

return Weekday(args.Time)
}

func Weekday[K any](time ottl.TimeGetter[K]) (ottl.ExprFunc[K], error) {
return func(ctx context.Context, tCtx K) (any, error) {
t, err := time.Get(ctx, tCtx)
if err != nil {
return nil, err
}
return int64(t.Weekday()), nil
}, nil
}
108 changes: 108 additions & 0 deletions pkg/ottl/ottlfuncs/func_weekday_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package ottlfuncs

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

func Test_Weekday(t *testing.T) {
tests := []struct {
name string
time ottl.TimeGetter[any]
expected int64
}{
{
name: "Mon",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 24, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 1,
},
{
name: "Tue",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 25, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 2,
},
{
name: "Wed",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 26, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 3,
},
{
name: "Thu",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 27, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 4,
},
{
name: "Fri",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 28, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 5,
},
{
name: "Sat",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 22, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 6,
},
{
name: "Sun",
time: &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return time.Date(2025, time.February, 23, 15, 4, 5, 0, time.UTC), nil
},
},
expected: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := Weekday(tt.time)
assert.NoError(t, err)
result, err := exprFunc(nil, nil)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

func Test_Weekday_Error(t *testing.T) {
var getter ottl.TimeGetter[any] = &ottl.StandardTimeGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return "not a time", nil
},
}
exprFunc, err := Weekday(getter)
assert.NoError(t, err)
result, err := exprFunc(context.Background(), nil)
assert.Nil(t, result)
assert.Error(t, err)
}
1 change: 1 addition & 0 deletions pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func converters[K any]() []ottl.Factory[K] {
NewUnixSecondsFactory[K](),
NewUUIDFactory[K](),
NewURLFactory[K](),
NewWeekdayFactory[K](),
NewUserAgentFactory[K](),
NewAppendFactory[K](),
NewYearFactory[K](),
Expand Down