Skip to content

Commit

Permalink
rename to FormatTime
Browse files Browse the repository at this point in the history
Signed-off-by: odubajDT <[email protected]>
  • Loading branch information
odubajDT committed Jan 10, 2025
1 parent 131155d commit aee7584
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .chloggen/ottl-timestamp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Available Converters:
- [String](#string)
- [Substring](#substring)
- [Time](#time)
- [Timestamp](#timestamp)
- [FormatTime](#formattime)
- [ToKeyValueString](#tokeyvaluestring)
- [TraceID](#traceid)
- [TruncateTime](#truncatetime)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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](),
Expand Down

0 comments on commit aee7584

Please sign in to comment.