Skip to content

Commit 0c1c312

Browse files
wbh1jmsnll
authored andcommitted
[pkg/ottl] add Now() function (open-telemetry#27038)
Implement a new OTTL function that takes no inputs and returns the current time. **Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> Adds a simple OTTL function that returns the current system time. **Link to tracking Issue:** Closes open-telemetry#26507 **Testing:** Added tests to confirm that function returns no errors and returns a time that is consistent. **Documentation:** Updated OTTL Functions README.
1 parent 1413a09 commit 0c1c312

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

.chloggen/feat_ottl-now.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 a Now() function to ottl that returns the current system 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: [27038, 26507]
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: ['user']

pkg/ottl/ottlfuncs/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ Available Converters:
291291
- [Milliseconds](#milliseconds)
292292
- [Minutes](#minutes)
293293
- [Nanoseconds](#nanoseconds)
294+
- [Now](#now)
294295
- [ParseJSON](#parsejson)
295296
- [Seconds](#seconds)
296297
- [SHA1](#sha1)
@@ -598,6 +599,19 @@ Examples:
598599

599600
- `Nanoseconds(Duration("1h"))`
600601

602+
### Now
603+
604+
`Now()`
605+
606+
The `Now` function returns the current time as represented by `time.Now()` in Go.
607+
608+
The returned type is `time.Time`.
609+
610+
Examples:
611+
612+
- `UnixSeconds(Now())`
613+
- `set(start_time, Now())`
614+
601615
### ParseJSON
602616

603617
`ParseJSON(target)`

pkg/ottl/ottlfuncs/func_now.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
"time"
9+
10+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
11+
)
12+
13+
func now[K any]() (ottl.ExprFunc[K], error) {
14+
return func(ctx context.Context, tCtx K) (interface{}, error) {
15+
return time.Now(), nil
16+
}, nil
17+
}
18+
19+
func createNowFunction[K any](_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[K], error) {
20+
return now[K]()
21+
}
22+
23+
func NewNowFactory[K any]() ottl.Factory[K] {
24+
return ottl.NewFactory("Now", nil, createNowFunction[K])
25+
}

pkg/ottl/ottlfuncs/func_now_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func Test_Now(t *testing.T) {
14+
exprFunc, err := now[interface{}]()
15+
assert.NoError(t, err)
16+
17+
value, err := exprFunc(nil, nil)
18+
assert.NoError(t, err)
19+
// There should be basically no difference between the value of time.Now() returned by the ottlfunc vs time.Now() run in the test.
20+
n := time.Now()
21+
assert.LessOrEqual(t, n.Sub(value.(time.Time)).Seconds(), 1.0)
22+
}

pkg/ottl/ottlfuncs/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func converters[K any]() []ottl.Factory[K] {
5050
NewMillisecondsFactory[K](),
5151
NewMinutesFactory[K](),
5252
NewNanosecondsFactory[K](),
53+
NewNowFactory[K](),
5354
NewParseJSONFactory[K](),
5455
NewSecondsFactory[K](),
5556
NewSHA1Factory[K](),

0 commit comments

Comments
 (0)