Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/ottl] add duration converter function #23659

Merged
merged 9 commits into from
Jul 10, 2023
2 changes: 1 addition & 1 deletion .chloggen/feat_duration-func.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,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 new `Duration` converter to convert string to a Golang duration"
note: "Add new `Duration` converter to convert string to a Golang time.duration"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [22015]
Expand Down
28 changes: 28 additions & 0 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Available Converters:
- [Concat](#concat)
- [ConvertCase](#convertcase)
- [FNV](#fnv)
- [Duration](#duration)
- [Int](#int)
- [IsMap](#ismap)
- [IsMatch](#ismatch)
Expand All @@ -288,6 +289,7 @@ Available Converters:
- [SHA256](#sha256)
- [SpanID](#spanid)
- [Split](#split)
- [Time](#time)
- [TraceID](#traceid)
- [Substring](#substring)
- [UUID](#UUID)
Expand Down Expand Up @@ -335,6 +337,20 @@ Examples:

- `ConvertCase(metric.name, "snake")`

### Duration

fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
The `Duration` Converter taking a string representation of a duration and converts it to a Golang `time.duration`.
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved

`duration` is a string.

If either `duration` is nil or is in a format that cannot be converted to Golang `time.duration`, an error is returned.

Examples:

- `Duration("3s")`
- `Duration("333ms")`
- `Duration("1000000h")`

### FNV

`FNV(value)`
Expand Down Expand Up @@ -569,6 +585,18 @@ Examples:

- ```Split("A|B|C", "|")```

### Time

The `Time` Converter taking a string representation of a duration and converts it to a Golang `time.duration`.
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved

`time` is a string. `format` is a string.

If either `time` or `format` are nil, an error is returned. The parser used is the parser at [internal/coreinternal/parser](github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/timeutils). If the time and format do not follow the parsing rules used by this parser, an error is returned.

Examples:

- `Time("02/04/2023", "%m/%d/%Y")`

### TraceID

`TraceID(bytes)`
Expand Down
3 changes: 0 additions & 3 deletions pkg/ottl/ottlfuncs/func_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ func Duration[K any](duration ottl.StringGetter[K]) (ottl.ExprFunc[K], error) {
if err != nil {
return nil, err
}
// if d == "" {
// return nil, fmt.Errorf("duration cannot be nil")
// }
dur, err := time.ParseDuration(d)
if err != nil {
return nil, err
Expand Down
9 changes: 0 additions & 9 deletions pkg/ottl/ottlfuncs/func_duration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,6 @@ func Test_DurationError(t *testing.T) {
},
expectedError: "invalid duration",
},
{
name: "empty duration",
duration: &ottl.StandardStringGetter[interface{}]{
Getter: func(ctx context.Context, tCtx interface{}) (interface{}, error) {
return "unknown unit", nil
},
},
expectedError: "invalid duration",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down