Skip to content

Commit 587309e

Browse files
r0mdauevan-bradley
authored andcommitted
[pkg/ottl] Add Nanosecond converter (open-telemetry#37083)
Co-authored-by: Evan Bradley <[email protected]>
1 parent 9dbc3d6 commit 587309e

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
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: Add the `Nanosecond` converter to return the nanosecond 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
@@ -444,6 +444,7 @@ Available Converters:
444444
- [Minute](#minute)
445445
- [Minutes](#minutes)
446446
- [Month](#month)
447+
- [Nanosecond](#nanosecond)
447448
- [Nanoseconds](#nanoseconds)
448449
- [Now](#now)
449450
- [ParseCSV](#parsecsv)
@@ -1238,6 +1239,20 @@ Examples:
12381239

12391240
- `Month(Now())`
12401241

1242+
### Nanosecond
1243+
1244+
`Nanosecond(value)`
1245+
1246+
The `Nanosecond` Converter returns the nanosecond component from the specified time using the Go stdlib [`time.Nanosecond` function](https://pkg.go.dev/time#Time.Nanosecond).
1247+
1248+
`value` is a `time.Time`. If `value` is another type, an error is returned.
1249+
1250+
The returned type is `int64`.
1251+
1252+
Examples:
1253+
1254+
- `Nanosecond(Now())`
1255+
12411256
### Nanoseconds
12421257

12431258
`Nanoseconds(value)`

pkg/ottl/ottlfuncs/func_nanosecond.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 NanosecondArguments[K any] struct {
14+
Time ottl.TimeGetter[K]
15+
}
16+
17+
func NewNanosecondFactory[K any]() ottl.Factory[K] {
18+
return ottl.NewFactory("Nanosecond", &NanosecondArguments[K]{}, createNanosecondFunction[K])
19+
}
20+
21+
func createNanosecondFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
22+
args, ok := oArgs.(*NanosecondArguments[K])
23+
24+
if !ok {
25+
return nil, fmt.Errorf("NanosecondFactory args must be of type *NanosecondArguments[K]")
26+
}
27+
28+
return Nanosecond(args.Time)
29+
}
30+
31+
func Nanosecond[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.Nanosecond()), 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_Nanosecond(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, 197382465, time.UTC), nil
27+
},
28+
},
29+
expected: 197382465,
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
exprFunc, err := Nanosecond(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_Nanosecond_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 := Nanosecond(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
@@ -68,6 +68,7 @@ func converters[K any]() []ottl.Factory[K] {
6868
NewMinuteFactory[K](),
6969
NewMinutesFactory[K](),
7070
NewMonthFactory[K](),
71+
NewNanosecondFactory[K](),
7172
NewNanosecondsFactory[K](),
7273
NewNowFactory[K](),
7374
NewParseCSVFactory[K](),

0 commit comments

Comments
 (0)