From e7aa7d2829a0a97862b47d8085d935e258110c80 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Fri, 10 Jan 2025 07:01:28 +0100 Subject: [PATCH] rename to FormatTime Signed-off-by: odubajDT --- .chloggen/ottl-timestamp.yaml | 2 +- pkg/ottl/e2e/e2e_test.go | 2 +- pkg/ottl/ottlfuncs/README.md | 10 +++++----- .../{func_timestamp.go => func_formattime.go} | 16 ++++++++-------- ...timestamp_test.go => func_formattime_test.go} | 6 +++--- pkg/ottl/ottlfuncs/functions.go | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) rename pkg/ottl/ottlfuncs/{func_timestamp.go => func_formattime.go} (58%) rename pkg/ottl/ottlfuncs/{func_timestamp_test.go => func_formattime_test.go} (97%) diff --git a/.chloggen/ottl-timestamp.yaml b/.chloggen/ottl-timestamp.yaml index dd862c673175..0586c1589142 100644 --- a/.chloggen/ottl-timestamp.yaml +++ b/.chloggen/ottl-timestamp.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: "Introduce new Timestamp() converter function." +note: "Introduce new FormatTime() converter function." # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. issues: [36870] diff --git a/pkg/ottl/e2e/e2e_test.go b/pkg/ottl/e2e/e2e_test.go index 175bd35f5b27..d552be6a8bd1 100644 --- a/pkg/ottl/e2e/e2e_test.go +++ b/pkg/ottl/e2e/e2e_test.go @@ -938,7 +938,7 @@ func Test_e2e_converters(t *testing.T) { }, }, { - statement: `set(attributes["time"],Timestamp(time, "%Y-%m-%d"))`, + statement: `set(attributes["time"], FormatTime(time, "%Y-%m-%d"))`, want: func(tCtx ottllog.TransformContext) { tCtx.GetLogRecord().Attributes().PutStr("time", "2020-02-11") }, diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 053715a26857..04b9d2b3abce 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -465,7 +465,7 @@ Available Converters: - [String](#string) - [Substring](#substring) - [Time](#time) -- [Timestamp](#timestamp) +- [FormatTime](#formattime) - [ToKeyValueString](#tokeyvaluestring) - [TraceID](#traceid) - [TruncateTime](#truncatetime) @@ -1962,11 +1962,11 @@ Examples: - `Time("mercoledì set 4 2024", "%A %h %e %Y", "", "it")` - `Time("Febrero 25 lunes, 2002, 02:03:04 p.m.", "%B %d %A, %Y, %r", "America/New_York", "es-ES")` -### Timestamp +### FormatTime -`Timestamp(time, format)` +`FormatTime(time, format)` -The `Timestamp` Converter takes a `time.Time` and converts it to a human readable string representations of the time according to the specidied format. +The `FormatTime` Converter takes a `time.Time` and converts it to a human readable string representations of the time according to the specidied format. `time` is `time.Time`. If `time` is another type an error is returned. `format` is a string. @@ -2015,7 +2015,7 @@ If `format` is nil, an error is returned. The parser used is the parser at [inte Examples: -- `Timestamp(Time("02/04/2023", "%m/%d/%Y"), "%A %h %e %Y")` +- `FormatTime(Time("02/04/2023", "%m/%d/%Y"), "%A %h %e %Y")` ### ToKeyValueString diff --git a/pkg/ottl/ottlfuncs/func_timestamp.go b/pkg/ottl/ottlfuncs/func_formattime.go similarity index 58% rename from pkg/ottl/ottlfuncs/func_timestamp.go rename to pkg/ottl/ottlfuncs/func_formattime.go index 425e9e165ece..61bd7b89dd7e 100644 --- a/pkg/ottl/ottlfuncs/func_timestamp.go +++ b/pkg/ottl/ottlfuncs/func_formattime.go @@ -11,26 +11,26 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) -type TimestampArguments[K any] struct { +type FormatTimeArguments[K any] struct { Time ottl.TimeGetter[K] Format string } -func NewTimestampFactory[K any]() ottl.Factory[K] { - return ottl.NewFactory("Timestamp", &TimestampArguments[K]{}, createTimestampFunction[K]) +func NewFormatTimeFactory[K any]() ottl.Factory[K] { + return ottl.NewFactory("FormatTime", &FormatTimeArguments[K]{}, createFormatTimeFunction[K]) } -func createTimestampFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { - args, ok := oArgs.(*TimestampArguments[K]) +func createFormatTimeFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { + args, ok := oArgs.(*FormatTimeArguments[K]) if !ok { - return nil, fmt.Errorf("TimestampFactory args must be of type *TimestampArguments[K]") + return nil, fmt.Errorf("FormatTimeFactory args must be of type *FormatTimeArguments[K]") } - return Timestamp(args.Time, args.Format) + return FormatTime(args.Time, args.Format) } -func Timestamp[K any](timeValue ottl.TimeGetter[K], format string) (ottl.ExprFunc[K], error) { +func FormatTime[K any](timeValue ottl.TimeGetter[K], format string) (ottl.ExprFunc[K], error) { if format == "" { return nil, fmt.Errorf("format cannot be nil") } diff --git a/pkg/ottl/ottlfuncs/func_timestamp_test.go b/pkg/ottl/ottlfuncs/func_formattime_test.go similarity index 97% rename from pkg/ottl/ottlfuncs/func_timestamp_test.go rename to pkg/ottl/ottlfuncs/func_formattime_test.go index b4b64e5616f1..f31094431345 100644 --- a/pkg/ottl/ottlfuncs/func_timestamp_test.go +++ b/pkg/ottl/ottlfuncs/func_formattime_test.go @@ -13,7 +13,7 @@ import ( "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) -func Test_Timestamp(t *testing.T) { +func Test_FormatTime(t *testing.T) { tests := []struct { name string time ottl.TimeGetter[any] @@ -79,7 +79,7 @@ func Test_Timestamp(t *testing.T) { expected: "July 31, 1993", }, { - name: "date with timestamp", + name: "date with FormatTime", time: &ottl.StandardTimeGetter[any]{ Getter: func(_ context.Context, _ any) (any, error) { return time.Date(2023, 3, 14, 17, 0o2, 59, 0, time.Local), nil @@ -151,7 +151,7 @@ func Test_Timestamp(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - exprFunc, err := Timestamp(tt.time, tt.format) + exprFunc, err := FormatTime(tt.time, tt.format) if tt.errorMsg != "" { assert.Contains(t, err.Error(), tt.errorMsg) } else { diff --git a/pkg/ottl/ottlfuncs/functions.go b/pkg/ottl/ottlfuncs/functions.go index da93d37c9366..4aa7ffee9e3a 100644 --- a/pkg/ottl/ottlfuncs/functions.go +++ b/pkg/ottl/ottlfuncs/functions.go @@ -89,7 +89,7 @@ func converters[K any]() []ottl.Factory[K] { NewStringFactory[K](), NewSubstringFactory[K](), NewTimeFactory[K](), - NewTimestampFactory[K](), + NewFormatTimeFactory[K](), NewTrimFactory[K](), NewToKeyValueStringFactory[K](), NewTruncateTimeFactory[K](),