From b9ab09c4510311e5a05477b18710552ac3feb8b7 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Wed, 20 Sep 2023 17:25:00 -0400 Subject: [PATCH 1/5] feat(ottl): add Now() function Implement a new OTTL function that takes no inputs and returns the current time. --- pkg/ottl/ottlfuncs/README.md | 13 +++++++++++++ pkg/ottl/ottlfuncs/func_now.go | 25 +++++++++++++++++++++++++ pkg/ottl/ottlfuncs/func_now_test.go | 22 ++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 pkg/ottl/ottlfuncs/func_now.go create mode 100644 pkg/ottl/ottlfuncs/func_now_test.go diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 16fb3301da35..6667f9d945e8 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -291,6 +291,7 @@ Available Converters: - [Milliseconds](#milliseconds) - [Minutes](#minutes) - [Nanoseconds](#nanoseconds) +- [Now](#now) - [ParseJSON](#parsejson) - [Seconds](#seconds) - [SHA1](#sha1) @@ -598,6 +599,18 @@ Examples: - `Nanoseconds(Duration("1h"))` +### Now + +`Now()` + +The `Now` function returns the current time as represented by `time.Now()` in Go. + +The returned type is `time.Time`. + +Examples: + +- `UnixSeconds(Now())` + ### ParseJSON `ParseJSON(target)` diff --git a/pkg/ottl/ottlfuncs/func_now.go b/pkg/ottl/ottlfuncs/func_now.go new file mode 100644 index 000000000000..4829d57f767a --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_now.go @@ -0,0 +1,25 @@ +// 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" + "time" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" +) + +func now[K any]() (ottl.ExprFunc[K], error) { + return func(ctx context.Context, tCtx K) (interface{}, error) { + return time.Now(), nil + }, nil +} + +func createNowFunction[K any](_ ottl.FunctionContext, _ ottl.Arguments) (ottl.ExprFunc[K], error) { + return now[K]() +} + +func NewNowFactory[K any]() ottl.Factory[K] { + return ottl.NewFactory("Now", nil, createNowFunction[K]) +} diff --git a/pkg/ottl/ottlfuncs/func_now_test.go b/pkg/ottl/ottlfuncs/func_now_test.go new file mode 100644 index 000000000000..a96751201654 --- /dev/null +++ b/pkg/ottl/ottlfuncs/func_now_test.go @@ -0,0 +1,22 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package ottlfuncs + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func Test_Now(t *testing.T) { + exprFunc, err := now[interface{}]() + assert.NoError(t, err) + + value, err := exprFunc(nil, nil) + assert.NoError(t, err) + // There should be basically no difference between the value of time.Now() returned by the ottlfunc vs time.Now() run in the test. + n := time.Now() + assert.LessOrEqual(t, n.Sub(value.(time.Time)).Seconds(), 1.0) +} From 4c80c7b10c847a4dc446f9efcbec48ee2220287b Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Wed, 20 Sep 2023 17:32:50 -0400 Subject: [PATCH 2/5] doc(ottl): add changelog for Now() --- .chloggen/feat_ottl-now.yaml | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 .chloggen/feat_ottl-now.yaml diff --git a/.chloggen/feat_ottl-now.yaml b/.chloggen/feat_ottl-now.yaml new file mode 100755 index 000000000000..af10ddd63a3e --- /dev/null +++ b/.chloggen/feat_ottl-now.yaml @@ -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: "Add a Now() function to ottl that returns the current system time ()" + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [27038, 26507] + +# (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: ['user'] From 4ad0ad9a5716afe73e975d664dde2371f0edbf21 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Thu, 21 Sep 2023 12:10:34 -0400 Subject: [PATCH 3/5] fix(ottl): add Now factory Missed this in the original commit adding the function. --- pkg/ottl/ottlfuncs/functions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index 178517992ca9..736a49313605 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -50,6 +50,7 @@ func converters[K any]() []ottl.Factory[K] { NewMillisecondsFactory[K](), NewMinutesFactory[K](), NewNanosecondsFactory[K](), + NewNowFactory[K](), NewParseJSONFactory[K](), NewSecondsFactory[K](), NewSHA1Factory[K](), From caaec6a8ea802b95a6bfdeff0ae4e4b4d3967534 Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Thu, 21 Sep 2023 12:10:52 -0400 Subject: [PATCH 4/5] doc(ottlfunc): add additional Now example --- pkg/ottl/ottlfuncs/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 6667f9d945e8..eda7cb703f80 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -610,6 +610,7 @@ The returned type is `time.Time`. Examples: - `UnixSeconds(Now())` +- `set(start_time, Now())` ### ParseJSON From 4525148ccc64260244afe3fd7eae2282923d76ae Mon Sep 17 00:00:00 2001 From: Will Hegedus Date: Thu, 21 Sep 2023 12:11:16 -0400 Subject: [PATCH 5/5] doc(ottl): remove extraneous characters --- .chloggen/feat_ottl-now.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.chloggen/feat_ottl-now.yaml b/.chloggen/feat_ottl-now.yaml index af10ddd63a3e..1fbd1afe4c74 100755 --- a/.chloggen/feat_ottl-now.yaml +++ b/.chloggen/feat_ottl-now.yaml @@ -7,7 +7,7 @@ change_type: 'enhancement' component: 'pkg/ottl' # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). -note: "Add a Now() function to ottl that returns the current system time ()" +note: "Add a Now() function to ottl that returns the current system time" # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [27038, 26507]