Skip to content

Commit f369f60

Browse files
authored
[pkg/ottl] Rename Statements to StatementSequence (#29598)
**Description:** This PR brings the `Statements` API up-to-date with the `ConditionSequence` API. **Link to tracking Issue:** <Issue number if applicable> Closes #13545 **Testing:** Unit tests **Documentation:** <Describe the documentation added.> Filled in godoc comments.
1 parent 4756047 commit f369f60

File tree

14 files changed

+104
-209
lines changed

14 files changed

+104
-209
lines changed

.chloggen/ottl-statementsequence.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Rename `Statements` to `StatementSequence`. Remove `Eval` function from `StatementSequence`, use `ConditionSequence` instead.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [29598]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [api]

pkg/ottl/contexts/ottldatapoint/datapoint.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
8282
return p, nil
8383
}
8484

85-
type StatementsOption func(*ottl.Statements[TransformContext])
85+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
8686

87-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
88-
return func(s *ottl.Statements[TransformContext]) {
89-
ottl.WithErrorMode[TransformContext](errorMode)(s)
87+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
88+
return func(s *ottl.StatementSequence[TransformContext]) {
89+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
9090
}
9191
}
9292

93-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
94-
s := ottl.NewStatements(statements, telemetrySettings)
93+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
94+
s := ottl.NewStatementSequence(statements, telemetrySettings)
9595
for _, op := range options {
9696
op(&s)
9797
}

pkg/ottl/contexts/ottllog/log.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
7272
return p, nil
7373
}
7474

75-
type StatementsOption func(*ottl.Statements[TransformContext])
75+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
7676

77-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
78-
return func(s *ottl.Statements[TransformContext]) {
79-
ottl.WithErrorMode[TransformContext](errorMode)(s)
77+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
78+
return func(s *ottl.StatementSequence[TransformContext]) {
79+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
8080
}
8181
}
8282

83-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
84-
s := ottl.NewStatements(statements, telemetrySettings)
83+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
84+
s := ottl.NewStatementSequence(statements, telemetrySettings)
8585
for _, op := range options {
8686
op(&s)
8787
}

pkg/ottl/contexts/ottlmetric/metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
7676
return p, err
7777
}
7878

79-
type StatementsOption func(*ottl.Statements[TransformContext])
79+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
8080

81-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
82-
return func(s *ottl.Statements[TransformContext]) {
83-
ottl.WithErrorMode[TransformContext](errorMode)(s)
81+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
82+
return func(s *ottl.StatementSequence[TransformContext]) {
83+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
8484
}
8585
}
8686

87-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
88-
s := ottl.NewStatements(statements, telemetrySettings)
87+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
88+
s := ottl.NewStatementSequence(statements, telemetrySettings)
8989
for _, op := range options {
9090
op(&s)
9191
}

pkg/ottl/contexts/ottlresource/resource.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
5555
return p, nil
5656
}
5757

58-
type StatementsOption func(*ottl.Statements[TransformContext])
58+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
5959

60-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
61-
return func(s *ottl.Statements[TransformContext]) {
62-
ottl.WithErrorMode[TransformContext](errorMode)(s)
60+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
61+
return func(s *ottl.StatementSequence[TransformContext]) {
62+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
6363
}
6464
}
6565

66-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
67-
s := ottl.NewStatements(statements, telemetrySettings)
66+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
67+
s := ottl.NewStatementSequence(statements, telemetrySettings)
6868
for _, op := range options {
6969
op(&s)
7070
}

pkg/ottl/contexts/ottlscope/scope.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
6262
return p, nil
6363
}
6464

65-
type StatementsOption func(*ottl.Statements[TransformContext])
65+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
6666

67-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
68-
return func(s *ottl.Statements[TransformContext]) {
69-
ottl.WithErrorMode[TransformContext](errorMode)(s)
67+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
68+
return func(s *ottl.StatementSequence[TransformContext]) {
69+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
7070
}
7171
}
7272

73-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
74-
s := ottl.NewStatements(statements, telemetrySettings)
73+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
74+
s := ottl.NewStatementSequence(statements, telemetrySettings)
7575
for _, op := range options {
7676
op(&s)
7777
}

pkg/ottl/contexts/ottlspan/span.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
6969
return p, nil
7070
}
7171

72-
type StatementsOption func(*ottl.Statements[TransformContext])
72+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
7373

74-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
75-
return func(s *ottl.Statements[TransformContext]) {
76-
ottl.WithErrorMode[TransformContext](errorMode)(s)
74+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
75+
return func(s *ottl.StatementSequence[TransformContext]) {
76+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
7777
}
7878
}
7979

80-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
81-
s := ottl.NewStatements(statements, telemetrySettings)
80+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
81+
s := ottl.NewStatementSequence(statements, telemetrySettings)
8282
for _, op := range options {
8383
op(&s)
8484
}

pkg/ottl/contexts/ottlspanevent/span_events.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,16 @@ func NewParser(functions map[string]ottl.Factory[TransformContext], telemetrySet
7777
return p, nil
7878
}
7979

80-
type StatementsOption func(*ottl.Statements[TransformContext])
80+
type StatementSequenceOption func(*ottl.StatementSequence[TransformContext])
8181

82-
func WithErrorMode(errorMode ottl.ErrorMode) StatementsOption {
83-
return func(s *ottl.Statements[TransformContext]) {
84-
ottl.WithErrorMode[TransformContext](errorMode)(s)
82+
func WithStatementSequenceErrorMode(errorMode ottl.ErrorMode) StatementSequenceOption {
83+
return func(s *ottl.StatementSequence[TransformContext]) {
84+
ottl.WithStatementSequenceErrorMode[TransformContext](errorMode)(s)
8585
}
8686
}
8787

88-
func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementsOption) ottl.Statements[TransformContext] {
89-
s := ottl.NewStatements(statements, telemetrySettings)
88+
func NewStatementSequence(statements []*ottl.Statement[TransformContext], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption) ottl.StatementSequence[TransformContext] {
89+
s := ottl.NewStatementSequence(statements, telemetrySettings)
9090
for _, op := range options {
9191
op(&s)
9292
}

pkg/ottl/parser.go

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (c *Condition[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
8989
return c.condition.Eval(ctx, tCtx)
9090
}
9191

92-
// Parser provides the means to parse OTTL Statements and Conditions given a specific set of functions,
92+
// Parser provides the means to parse OTTL StatementSequence and Conditions given a specific set of functions,
9393
// a PathExpressionParser, and an EnumParser.
9494
type Parser[K any] struct {
9595
functions map[string]Factory[K]
@@ -263,24 +263,30 @@ func newParser[G any]() *participle.Parser[G] {
263263
return parser
264264
}
265265

266-
// Statements represents a list of statements that will be executed sequentially for a TransformContext.
267-
type Statements[K any] struct {
266+
// StatementSequence represents a list of statements that will be executed sequentially for a TransformContext
267+
// and will handle errors based on an ErrorMode.
268+
type StatementSequence[K any] struct {
268269
statements []*Statement[K]
269270
errorMode ErrorMode
270271
telemetrySettings component.TelemetrySettings
271272
}
272273

273-
type StatementsOption[K any] func(*Statements[K])
274+
type StatementSequenceOption[K any] func(*StatementSequence[K])
274275

275-
func WithErrorMode[K any](errorMode ErrorMode) StatementsOption[K] {
276-
return func(s *Statements[K]) {
276+
// WithStatementSequenceErrorMode sets the ErrorMode of a StatementSequence
277+
func WithStatementSequenceErrorMode[K any](errorMode ErrorMode) StatementSequenceOption[K] {
278+
return func(s *StatementSequence[K]) {
277279
s.errorMode = errorMode
278280
}
279281
}
280282

281-
func NewStatements[K any](statements []*Statement[K], telemetrySettings component.TelemetrySettings, options ...StatementsOption[K]) Statements[K] {
282-
s := Statements[K]{
283+
// NewStatementSequence creates a new StatementSequence with the provided Statement slice and component.TelemetrySettings.
284+
// The default ErrorMode is `Propagate`.
285+
// You may also augment the StatementSequence with a slice of StatementSequenceOption.
286+
func NewStatementSequence[K any](statements []*Statement[K], telemetrySettings component.TelemetrySettings, options ...StatementSequenceOption[K]) StatementSequence[K] {
287+
s := StatementSequence[K]{
283288
statements: statements,
289+
errorMode: PropagateError,
284290
telemetrySettings: telemetrySettings,
285291
}
286292
for _, op := range options {
@@ -289,8 +295,10 @@ func NewStatements[K any](statements []*Statement[K], telemetrySettings componen
289295
return s
290296
}
291297

292-
// Execute is a function that will execute all the statements in the Statements list.
293-
func (s *Statements[K]) Execute(ctx context.Context, tCtx K) error {
298+
// Execute is a function that will execute all the statements in the StatementSequence list.
299+
// When the ErrorMode of the StatementSequence is `propagate`, errors cause the execution to halt and the error is returned.
300+
// When the ErrorMode of the StatementSequence is `ignore`, errors are logged and execution continues to the next statement.
301+
func (s *StatementSequence[K]) Execute(ctx context.Context, tCtx K) error {
294302
for _, statement := range s.statements {
295303
_, _, err := statement.Execute(ctx, tCtx)
296304
if err != nil {
@@ -304,28 +312,6 @@ func (s *Statements[K]) Execute(ctx context.Context, tCtx K) error {
304312
return nil
305313
}
306314

307-
// Eval returns true if any statement's condition is true and returns false otherwise.
308-
// Does not execute the statement's function.
309-
// When errorMode is `propagate`, errors cause the evaluation to be false and an error is returned.
310-
// When errorMode is `ignore`, errors cause evaluation to continue to the next statement.
311-
func (s *Statements[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
312-
for _, statement := range s.statements {
313-
match, err := statement.condition.Eval(ctx, tCtx)
314-
if err != nil {
315-
if s.errorMode == PropagateError {
316-
err = fmt.Errorf("failed to eval statement: %v, %w", statement.origText, err)
317-
return false, err
318-
}
319-
s.telemetrySettings.Logger.Warn("failed to eval statement", zap.Error(err), zap.String("statement", statement.origText))
320-
continue
321-
}
322-
if match {
323-
return true, nil
324-
}
325-
}
326-
return false, nil
327-
}
328-
329315
// ConditionSequence represents a list of Conditions that will be evaluated sequentially for a TransformContext
330316
// and will handle errors returned by conditions based on an ErrorMode.
331317
// By default, the conditions are ORed together, but they can be ANDed together using the WithLogicOperation option.
@@ -354,7 +340,8 @@ func WithLogicOperation[K any](logicOp LogicOperation) ConditionSequenceOption[K
354340
}
355341
}
356342

357-
// NewConditionSequence creates a new ConditionSequence with the provided Condition slice, ErrorMode, and component.TelemetrySettings.
343+
// NewConditionSequence creates a new ConditionSequence with the provided Condition slice and component.TelemetrySettings.
344+
// The default ErrorMode is `Propagate` and the default LogicOperation is `OR`.
358345
// You may also augment the ConditionSequence with a slice of ConditionSequenceOption.
359346
func NewConditionSequence[K any](conditions []*Condition[K], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption[K]) ConditionSequence[K] {
360347
c := ConditionSequence[K]{

0 commit comments

Comments
 (0)