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

feat: allow math expressions for time and duration #24453

Merged
merged 18 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/feat_math-time-duration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# 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 support for using addition and subtraction with time and duration'

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [22009]

# (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:
10 changes: 9 additions & 1 deletion pkg/ottl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,17 @@ When defining an OTTL function, if the function needs to take an Enum then the f

Math Expressions represent arithmetic calculations. They support `+`, `-`, `*`, and `/`, along with `()` for grouping.

Math Expressions currently only support `int64` and `float64`.
Math Expressions currently support `int64` and`float64`.
Math Expressions also support `time.Time` and `time.Duration`, but only with `+` and `-` and only using the following rules:
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
- A `time.Time` `-` a `time.Time` yields a `time.Duration`.
- A `time.Duration` `+` a `time.Time` yields a `time.Time`.
- A `time.Time` `-` a `time.Duration` yields a `time.Time`.
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
- A `time.Duration` `+` a `time.Duration` yields a `time.Duration`.
- A `time.Duration` `-` a `time.Duration` yields a `time.Duration`.

Math Expressions support `Paths` and `Editors` that return supported types.
Note that `*` and `/` take precedence over `+` and `-`.
Also note that `time.Time` and `time.Duration` can only be used with `+` and `-`.
Operations that share the same level of precedence will be executed in the order that they appear in the Math Expression.
Math Expressions can be grouped with parentheses to override evaluation precedence.
Math Expressions that mix `int64` and `float64` will result in an error.
Expand Down
Binary file added pkg/ottl/__debug_bin878371663
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
57 changes: 56 additions & 1 deletion pkg/ottl/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package ottl // import "github.com/open-telemetry/opentelemetry-collector-contri
import (
"context"
"fmt"
"time"
)

func (p *Parser[K]) evaluateMathExpression(expr *mathExpression) (Getter[K], error) {
Expand Down Expand Up @@ -98,14 +99,68 @@ func attemptMathOperation[K any](lhs Getter[K], op mathOp, rhs Getter[K]) Getter
default:
return nil, fmt.Errorf("%v must be int64 or float64", y)
}
case time.Time:
return performOpTime(newX, y, op)
case time.Duration:
return performOpDuration(newX, y, op)
default:
return nil, fmt.Errorf("%v must be int64 or float64", x)
return nil, fmt.Errorf("%v must be int64, float64, time.Time or time.Duration", x)
}
},
},
}
}

func performOpTime(x time.Time, y any, op mathOp) (any, error) {
switch op {
case ADD:
switch newY := y.(type) {
case time.Duration:
result := x.Add(newY)
return result, nil
default:
return nil, fmt.Errorf("time.Time must be added to time.Duration; found %v instead", y)
}
case SUB:
switch newY := y.(type) {
case time.Time:
result := x.Sub(newY)
return result, nil
case time.Duration:
result := x.Add(-1 * newY)
return result, nil
default:
return nil, fmt.Errorf("time.Time must be subtracted from time.Time or time.Duration; found %v instead", y)
fchikwekwe marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
}

func performOpDuration(x time.Duration, y any, op mathOp) (any, error) {
switch op {
case ADD:
switch newY := y.(type) {
case time.Duration:
result := x + newY
return result, nil
case time.Time:
result := newY.Add(x)
return result, nil
default:
return nil, fmt.Errorf("time.Duration must be added to time.Duration or time.Time; found %v instead", y)
}
case SUB:
switch newY := y.(type) {
case time.Duration:
result := x - newY
return result, nil
default:
return nil, fmt.Errorf("time.Duration must be subtracted from time.Duration; found %v instead", y)
}
}
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
}

func performOp[N int64 | float64](x N, y N, op mathOp) (N, error) {
switch op {
case ADD:
Expand Down
Loading