Skip to content

Commit d08b2fb

Browse files
authored
[pkg/ottl] Add Year converter (#33123)
**Description:** This adds in a `Year` converter that takes a `time.Time` and returns the int month component. Analogous to what we already have with `Hour` **Link to tracking Issue:** #33106 **Testing:** Added two unit tests based on the existing `Hour` ones **Documentation:** Added the new converter to the ottlfuncs README.md --------- Signed-off-by: sinkingpoint <[email protected]>
1 parent 612b588 commit d08b2fb

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

.chloggen/add-year-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: Adds a `Year` converter for extracting the int year component from a time.Time
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: [33106]
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ Available Converters:
421421
- [UnixNano](#unixnano)
422422
- [UnixSeconds](#unixseconds)
423423
- [UUID](#UUID)
424+
- [Year](#year)
424425

425426
### Base64Decode
426427

@@ -1298,6 +1299,20 @@ Examples:
12981299

12991300
The `UUID` function generates a v4 uuid string.
13001301

1302+
### Year
1303+
1304+
`Year(value)`
1305+
1306+
The `Year` Converter returns the year component from the specified time using the Go stdlib [`time.Year` function](https://pkg.go.dev/time#Time.Year).
1307+
1308+
`value` is a `time.Time`. If `value` is another type, an error is returned.
1309+
1310+
The returned type is `int64`.
1311+
1312+
Examples:
1313+
1314+
- `Year(Now())`
1315+
13011316
## Function syntax
13021317

13031318
Functions should be named and formatted according to the following standards.

pkg/ottl/ottlfuncs/func_year.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 YearArguments[K any] struct {
14+
Time ottl.TimeGetter[K]
15+
}
16+
17+
func NewYearFactory[K any]() ottl.Factory[K] {
18+
return ottl.NewFactory("Year", &YearArguments[K]{}, createYearFunction[K])
19+
}
20+
21+
func createYearFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
22+
args, ok := oArgs.(*YearArguments[K])
23+
24+
if !ok {
25+
return nil, fmt.Errorf("YearFactory args must be of type *YearArguments[K]")
26+
}
27+
28+
return Year(args.Time)
29+
}
30+
31+
func Year[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.Year()), nil
38+
}, nil
39+
}

pkg/ottl/ottlfuncs/func_year_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_Year(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
time ottl.TimeGetter[any]
20+
expected int64
21+
}{
22+
{
23+
name: "some time",
24+
time: &ottl.StandardTimeGetter[any]{
25+
Getter: func(_ context.Context, _ any) (any, error) {
26+
return time.Date(2006, time.January, 2, 15, 4, 5, 0, time.UTC), nil
27+
},
28+
},
29+
expected: 2006,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
exprFunc, err := Year(tt.time)
35+
assert.NoError(t, err)
36+
result, err := exprFunc(nil, nil)
37+
assert.NoError(t, err)
38+
assert.Equal(t, tt.expected, result)
39+
})
40+
}
41+
}
42+
43+
func Test_Year_Error(t *testing.T) {
44+
var getter ottl.TimeGetter[any] = &ottl.StandardTimeGetter[any]{
45+
Getter: func(_ context.Context, _ any) (any, error) {
46+
return "not a time", nil
47+
},
48+
}
49+
exprFunc, err := Year(getter)
50+
assert.NoError(t, err)
51+
result, err := exprFunc(context.Background(), nil)
52+
assert.Nil(t, result)
53+
assert.Error(t, err)
54+
}

pkg/ottl/ottlfuncs/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,6 @@ func converters[K any]() []ottl.Factory[K] {
7979
NewUnixNanoFactory[K](),
8080
NewUnixSecondsFactory[K](),
8181
NewUUIDFactory[K](),
82+
NewYearFactory[K](),
8283
}
8384
}

0 commit comments

Comments
 (0)