Skip to content

Commit 905674a

Browse files
fchikwekweTylerHelmuthevan-bradley
authored
feat: allow math expressions for time and duration (#24453)
Description: Allows addition and subtraction between times and duration according to rules in #22009 Link to tracking Issue: Closes #22009 Testing: Unit testing for math operations Documentation: --------- Co-authored-by: Tyler Helmuth <[email protected]> Co-authored-by: Evan Bradley <[email protected]>
1 parent 087b851 commit 905674a

File tree

4 files changed

+885
-11
lines changed

4 files changed

+885
-11
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: 'enhancement'
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: 'pkg/ottl'
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: 'Add support for using addition and subtraction with time and duration'
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [22009]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

pkg/ottl/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,18 @@ When defining an OTTL function, if the function needs to take an Enum then the f
172172

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

175-
Math Expressions currently only support `int64` and `float64`.
175+
Math Expressions currently support `int64`, `float64`, `time.Time` and `time.Duration`.
176+
For `time.Time` and `time.Duration`, only `+` and `-` are supported with the following rules:
177+
- A `time.Time` `-` a `time.Time` yields a `time.Duration`.
178+
- A `time.Duration` `+` a `time.Time` yields a `time.Time`.
179+
- A `time.Time` `+` a `time.Duration` yields a `time.Time`.
180+
- A `time.Time` `-` a `time.Duration` yields a `time.Time`.
181+
- A `time.Duration` `+` a `time.Duration` yields a `time.Duration`.
182+
- A `time.Duration` `-` a `time.Duration` yields a `time.Duration`.
183+
176184
Math Expressions support `Paths` and `Editors` that return supported types.
177185
Note that `*` and `/` take precedence over `+` and `-`.
186+
Also note that `time.Time` and `time.Duration` can only be used with `+` and `-`.
178187
Operations that share the same level of precedence will be executed in the order that they appear in the Math Expression.
179188
Math Expressions can be grouped with parentheses to override evaluation precedence.
180189
Math Expressions that mix `int64` and `float64` will result in an error.

pkg/ottl/math.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ottl // import "github.com/open-telemetry/opentelemetry-collector-contri
66
import (
77
"context"
88
"fmt"
9+
"time"
910
)
1011

1112
func (p *Parser[K]) evaluateMathExpression(expr *mathExpression) (Getter[K], error) {
@@ -98,14 +99,68 @@ func attemptMathOperation[K any](lhs Getter[K], op mathOp, rhs Getter[K]) Getter
9899
default:
99100
return nil, fmt.Errorf("%v must be int64 or float64", y)
100101
}
102+
case time.Time:
103+
return performOpTime(newX, y, op)
104+
case time.Duration:
105+
return performOpDuration(newX, y, op)
101106
default:
102-
return nil, fmt.Errorf("%v must be int64 or float64", x)
107+
return nil, fmt.Errorf("%v must be int64, float64, time.Time or time.Duration", x)
103108
}
104109
},
105110
},
106111
}
107112
}
108113

114+
func performOpTime(x time.Time, y any, op mathOp) (any, error) {
115+
switch op {
116+
case ADD:
117+
switch newY := y.(type) {
118+
case time.Duration:
119+
result := x.Add(newY)
120+
return result, nil
121+
default:
122+
return nil, fmt.Errorf("time.Time must be added to time.Duration; found %v instead", y)
123+
}
124+
case SUB:
125+
switch newY := y.(type) {
126+
case time.Time:
127+
result := x.Sub(newY)
128+
return result, nil
129+
case time.Duration:
130+
result := x.Add(-1 * newY)
131+
return result, nil
132+
default:
133+
return nil, fmt.Errorf("time.Time or time.Duration must be subtracted from time.Time; found %v instead", y)
134+
}
135+
}
136+
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
137+
}
138+
139+
func performOpDuration(x time.Duration, y any, op mathOp) (any, error) {
140+
switch op {
141+
case ADD:
142+
switch newY := y.(type) {
143+
case time.Duration:
144+
result := x + newY
145+
return result, nil
146+
case time.Time:
147+
result := newY.Add(x)
148+
return result, nil
149+
default:
150+
return nil, fmt.Errorf("time.Duration must be added to time.Duration or time.Time; found %v instead", y)
151+
}
152+
case SUB:
153+
switch newY := y.(type) {
154+
case time.Duration:
155+
result := x - newY
156+
return result, nil
157+
default:
158+
return nil, fmt.Errorf("time.Duration must be subtracted from time.Duration; found %v instead", y)
159+
}
160+
}
161+
return nil, fmt.Errorf("only addition and subtraction supported for time.Time and time.Duration")
162+
}
163+
109164
func performOp[N int64 | float64](x N, y N, op mathOp) (N, error) {
110165
switch op {
111166
case ADD:

0 commit comments

Comments
 (0)