@@ -33,8 +33,11 @@ type contextInferrer interface {
33
33
inferFromStatements (statements []string ) (string , error )
34
34
// inferFromConditions returns the OTTL context inferred from the given conditions.
35
35
inferFromConditions (conditions []string ) (string , error )
36
- // infer returns the OTTL context inferred from the given statements and conditions.
37
- infer (statements []string , conditions []string ) (string , error )
36
+ // inferFromValueExpressions returns the OTTL context inferred from the given value expressions.
37
+ inferFromValueExpressions (expressions []string ) (string , error )
38
+ // infer returns the OTTL context inferred from the given statements, conditions,
39
+ // and value expressions.
40
+ infer (statements , conditions , valueExpressions []string ) (string , error )
38
41
}
39
42
40
43
type priorityContextInferrer struct {
@@ -88,15 +91,19 @@ func withContextInferrerPriorities(priorities []string) priorityContextInferrerO
88
91
}
89
92
90
93
func (s * priorityContextInferrer ) inferFromConditions (conditions []string ) (inferredContext string , err error ) {
91
- return s .infer (nil , conditions )
94
+ return s .infer (nil , conditions , nil )
92
95
}
93
96
94
97
func (s * priorityContextInferrer ) inferFromStatements (statements []string ) (inferredContext string , err error ) {
95
- return s .infer (statements , nil )
98
+ return s .infer (statements , nil , nil )
96
99
}
97
100
98
- func (s * priorityContextInferrer ) infer (statements []string , conditions []string ) (inferredContext string , err error ) {
99
- var statementsHints , conditionsHints []priorityContextInferrerHints
101
+ func (s * priorityContextInferrer ) inferFromValueExpressions (expressions []string ) (inferredContext string , err error ) {
102
+ return s .infer (nil , nil , expressions )
103
+ }
104
+
105
+ func (s * priorityContextInferrer ) infer (statements , conditions , valueExprs []string ) (inferredContext string , err error ) {
106
+ var statementsHints , conditionsHints , valueExprsHints []priorityContextInferrerHints
100
107
if len (statements ) > 0 {
101
108
statementsHints , err = s .getStatementsHints (statements )
102
109
if err != nil {
@@ -109,23 +116,30 @@ func (s *priorityContextInferrer) infer(statements []string, conditions []string
109
116
return "" , err
110
117
}
111
118
}
119
+ if len (valueExprs ) > 0 {
120
+ valueExprsHints , err = s .getValueExpressionsHints (valueExprs )
121
+ if err != nil {
122
+ return "" , err
123
+ }
124
+ }
112
125
if s .telemetrySettings .Logger .Core ().Enabled (zap .DebugLevel ) {
113
- s .telemetrySettings .Logger .Debug ("Inferring context from statements and conditions " ,
126
+ s .telemetrySettings .Logger .Debug ("Inferring OTTL context " ,
114
127
zap .Strings ("candidates" , maps .Keys (s .contextCandidate )),
115
128
zap .Any ("priority" , s .contextPriority ),
116
129
zap .Strings ("statements" , statements ),
117
130
zap .Strings ("conditions" , conditions ),
131
+ zap .Strings ("value_expressions" , valueExprs ),
118
132
)
119
133
}
120
- return s .inferFromHints (append (statementsHints , conditionsHints ... ))
134
+ return s .inferFromHints (slices . Concat (statementsHints , conditionsHints , valueExprsHints ))
121
135
}
122
136
123
137
func (s * priorityContextInferrer ) inferFromHints (hints []priorityContextInferrerHints ) (inferredContext string , err error ) {
124
138
defer func () {
125
139
if inferredContext != "" {
126
- s .telemetrySettings .Logger .Debug (fmt .Sprintf (`Inferred context: "%s"` , inferredContext ))
140
+ s .telemetrySettings .Logger .Debug (fmt .Sprintf (`Inferred OTTL context: "%s"` , inferredContext ))
127
141
} else {
128
- s .telemetrySettings .Logger .Debug ("Unable to infer context from statements " , zap .Error (err ))
142
+ s .telemetrySettings .Logger .Debug ("Unable to infer OTTL context " , zap .Error (err ))
129
143
}
130
144
}()
131
145
@@ -284,6 +298,24 @@ func (s *priorityContextInferrer) getStatementsHints(statements []string) ([]pri
284
298
return hints , nil
285
299
}
286
300
301
+ // getValueExpressionsHints extracts all path, function (converter) names, and enumSymbol
302
+ // from the given value expressions. These values are used by the context inferrer as hints to
303
+ // select a context in which the function/enum are supported.
304
+ func (s * priorityContextInferrer ) getValueExpressionsHints (exprs []string ) ([]priorityContextInferrerHints , error ) {
305
+ hints := make ([]priorityContextInferrerHints , 0 , len (exprs ))
306
+ for _ , expr := range exprs {
307
+ parsed , err := parseValueExpression (expr )
308
+ if err != nil {
309
+ return nil , err
310
+ }
311
+
312
+ visitor := newGrammarContextInferrerVisitor ()
313
+ parsed .accept (& visitor )
314
+ hints = append (hints , visitor )
315
+ }
316
+ return hints , nil
317
+ }
318
+
287
319
// priorityContextInferrerHints is a grammarVisitor implementation that collects
288
320
// all path, function names (converter.Function and editor.Function), and enumSymbol.
289
321
type priorityContextInferrerHints struct {
0 commit comments