Skip to content

Commit

Permalink
Switch error mode to option
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHelmuth committed Nov 30, 2023
1 parent aac66dd commit c06f7b9
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 25 deletions.
12 changes: 6 additions & 6 deletions internal/filter/filterottl/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewBoolExprForSpan(conditions []string, functions map[string]ottl.Factory[o
if err != nil {
return nil, err
}
c := ottlspan.NewConditionSequence(statements, errorMode, set)
c := ottlspan.NewConditionSequence(statements, set, ottlspan.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}

Expand All @@ -44,7 +44,7 @@ func NewBoolExprForSpanEvent(conditions []string, functions map[string]ottl.Fact
if err != nil {
return nil, err
}
c := ottlspanevent.NewConditionSequence(statements, errorMode, set)
c := ottlspanevent.NewConditionSequence(statements, set, ottlspanevent.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}

Expand All @@ -60,7 +60,7 @@ func NewBoolExprForMetric(conditions []string, functions map[string]ottl.Factory
if err != nil {
return nil, err
}
c := ottlmetric.NewConditionSequence(statements, errorMode, set)
c := ottlmetric.NewConditionSequence(statements, set, ottlmetric.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}

Expand All @@ -76,7 +76,7 @@ func NewBoolExprForDataPoint(conditions []string, functions map[string]ottl.Fact
if err != nil {
return nil, err
}
c := ottldatapoint.NewConditionSequence(statements, errorMode, set)
c := ottldatapoint.NewConditionSequence(statements, set, ottldatapoint.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}

Expand All @@ -92,7 +92,7 @@ func NewBoolExprForLog(conditions []string, functions map[string]ottl.Factory[ot
if err != nil {
return nil, err
}
c := ottllog.NewConditionSequence(statements, errorMode, set)
c := ottllog.NewConditionSequence(statements, set, ottllog.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}

Expand All @@ -108,6 +108,6 @@ func NewBoolExprForResource(conditions []string, functions map[string]ottl.Facto
if err != nil {
return nil, err
}
c := ottlresource.NewConditionSequence(statements, errorMode, set)
c := ottlresource.NewConditionSequence(statements, set, ottlresource.WithConditionSequenceErrorMode(errorMode))
return &c, nil
}
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottldatapoint/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottllog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

var symbolTable = map[ottl.EnumSymbol]ottl.Enum{
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottlmetric/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

var symbolTable = internal.MetricSymbolTable
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottlresource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottlscope/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

func parseEnum(_ *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottlspan/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
16 changes: 14 additions & 2 deletions pkg/ottl/contexts/ottlspanevent/span_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,20 @@ func NewStatements(statements []*ottl.Statement[TransformContext], telemetrySett
return s
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], errorMode ottl.ErrorMode, telemetrySettings component.TelemetrySettings, options ...ottl.ConditionSequenceOption[TransformContext]) ottl.ConditionSequence[TransformContext] {
return ottl.NewConditionSequence(conditions, errorMode, telemetrySettings, options...)
type ConditionSequenceOption func(*ottl.ConditionSequence[TransformContext])

func WithConditionSequenceErrorMode(errorMode ottl.ErrorMode) ConditionSequenceOption {
return func(c *ottl.ConditionSequence[TransformContext]) {
ottl.WithConditionSequenceErrorMode[TransformContext](errorMode)(c)
}
}

func NewConditionSequence(conditions []*ottl.Condition[TransformContext], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption) ottl.ConditionSequence[TransformContext] {
c := ottl.NewConditionSequence(conditions, telemetrySettings)
for _, op := range options {
op(&c)
}
return c
}

func parseEnum(val *ottl.EnumSymbol) (*ottl.Enum, error) {
Expand Down
24 changes: 19 additions & 5 deletions pkg/ottl/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func (e *ErrorMode) UnmarshalText(text []byte) error {
}
}

// TODO: move this and ErrorMode to a config.go file
type LogicOperation string

const (
Expand Down Expand Up @@ -337,6 +338,13 @@ type ConditionSequence[K any] struct {

type ConditionSequenceOption[K any] func(*ConditionSequence[K])

// WithConditionSequenceErrorMode sets the ErrorMode of a ConditionSequence
func WithConditionSequenceErrorMode[K any](errorMode ErrorMode) ConditionSequenceOption[K] {
return func(c *ConditionSequence[K]) {
c.errorMode = errorMode
}
}

// WithLogicOperation sets the LogicOperation of a ConditionSequence
// When setting AND the conditions will be ANDed together.
// When setting OR the conditions will be ORed together.
Expand All @@ -348,17 +356,17 @@ func WithLogicOperation[K any](logicOp LogicOperation) ConditionSequenceOption[K

// NewConditionSequence creates a new ConditionSequence with the provided Condition slice, ErrorMode, and component.TelemetrySettings.
// You may also augment the ConditionSequence with a slice of ConditionSequenceOption.
func NewConditionSequence[K any](conditions []*Condition[K], errorMode ErrorMode, telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption[K]) ConditionSequence[K] {
s := ConditionSequence[K]{
func NewConditionSequence[K any](conditions []*Condition[K], telemetrySettings component.TelemetrySettings, options ...ConditionSequenceOption[K]) ConditionSequence[K] {
c := ConditionSequence[K]{
conditions: conditions,
errorMode: errorMode,
errorMode: IgnoreError,
telemetrySettings: telemetrySettings,
logicOp: Or,
}
for _, op := range options {
op(&s)
op(&c)
}
return s
return c
}

// Eval evaluates the result of each Condition in the ConditionSequence.
Expand All @@ -367,6 +375,7 @@ func NewConditionSequence[K any](conditions []*Condition[K], errorMode ErrorMode
// If using the AND LogicOperation, if any Condition evaluates to false, then false is returned and if all Conditions evaluate to true, then true is returned.
// When the ErrorMode of the ConditionSequence is `propagate`, errors cause the evaluation to be false and an error is returned.
// When the ErrorMode of the ConditionSequence is `ignore`, errors cause the evaluation to continue to the next condition.
// When using the AND LogicOperation with the `ignore` ErrorMode the sequence will evaluate to false if all conditions error.
func (c *ConditionSequence[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
var atLeastOneMatch bool
for _, condition := range c.conditions {
Expand All @@ -389,5 +398,10 @@ func (c *ConditionSequence[K]) Eval(ctx context.Context, tCtx K) (bool, error) {
return false, nil
}
}
// When ANDing it is possible to arrive here not because everything was true, but because everything errored and was ignored.
// In that situation, we don't want to return True when no conditions actually passed. In a situation when everything failed
// we are essentially left with an empty set, which is normally evaluated in mathematics as False. We will use that
// idea to return False when ANDing and everything errored. We use atLeastOneMatch here to return true if anything did match.
// It is not possible to get here if any condition during an AND explicitly failed.
return c.logicOp == And && atLeastOneMatch, nil
}

0 comments on commit c06f7b9

Please sign in to comment.