Skip to content

Commit 0787af5

Browse files
committed
Add Second converter
Signed-off-by: Romain Dauby <[email protected]>
1 parent 09b6d77 commit 0787af5

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

.chloggen/add-second-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: Add the `Second` converter to return the second component from the specified 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: [37042]
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
@@ -452,6 +452,7 @@ Available Converters:
452452
- [ParseSimplifiedXML](#parsesimplifiedxml)
453453
- [ParseXML](#parsexml)
454454
- [RemoveXML](#removexml)
455+
- [Second](#second)
455456
- [Seconds](#seconds)
456457
- [SHA1](#sha1)
457458
- [SHA256](#sha256)
@@ -1611,6 +1612,20 @@ Delete text from nodes that contain the word "sensitive"
16111612

16121613
- `RemoveXML(body, "//*[contains(text(), 'sensitive')]")`
16131614

1615+
### Second
1616+
1617+
`Second(value)`
1618+
1619+
The `Second` Converter returns the second component from the specified time using the Go stdlib [`time.Second` function](https://pkg.go.dev/time#Time.Second).
1620+
1621+
`value` is a `time.Time`. If `value` is another type, an error is returned.
1622+
1623+
The returned type is `int64`.
1624+
1625+
Examples:
1626+
1627+
- `Second(Now())`
1628+
16141629
### Seconds
16151630

16161631
`Seconds(value)`

pkg/ottl/ottlfuncs/func_second.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 SecondArguments[K any] struct {
14+
Time ottl.TimeGetter[K]
15+
}
16+
17+
func NewSecondFactory[K any]() ottl.Factory[K] {
18+
return ottl.NewFactory("Second", &SecondArguments[K]{}, createSecondFunction[K])
19+
}
20+
21+
func createSecondFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
22+
args, ok := oArgs.(*SecondArguments[K])
23+
24+
if !ok {
25+
return nil, fmt.Errorf("SecondFactory args must be of type *SecondArguments[K]")
26+
}
27+
28+
return Second(args.Time)
29+
}
30+
31+
func Second[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.Second()), nil
38+
}, nil
39+
}
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_Second(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: 5,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
exprFunc, err := Second(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_Second_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 := Second(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
@@ -76,6 +76,7 @@ func converters[K any]() []ottl.Factory[K] {
7676
NewParseSimplifiedXMLFactory[K](),
7777
NewParseXMLFactory[K](),
7878
NewRemoveXMLFactory[K](),
79+
NewSecondFactory[K](),
7980
NewSecondsFactory[K](),
8081
NewSHA1Factory[K](),
8182
NewSHA256Factory[K](),

0 commit comments

Comments
 (0)