-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
functions.go
683 lines (609 loc) · 18.5 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottl // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
import (
"context"
"errors"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/iancoleman/strcase"
)
type PathExpressionParser[K any] func(Path[K]) (GetSetter[K], error)
type EnumParser func(*EnumSymbol) (*Enum, error)
type Enum int64
type EnumSymbol string
func buildOriginalText(path *path) string {
var builder strings.Builder
if path.Context != "" {
builder.WriteString(path.Context)
if len(path.Fields) > 0 {
builder.WriteString(".")
}
}
for i, f := range path.Fields {
builder.WriteString(f.Name)
if len(f.Keys) > 0 {
builder.WriteString(buildOriginalKeysText(f.Keys))
}
if i != len(path.Fields)-1 {
builder.WriteString(".")
}
}
return builder.String()
}
func buildOriginalKeysText(keys []key) string {
var builder strings.Builder
if len(keys) > 0 {
for _, k := range keys {
builder.WriteString("[")
if k.Int != nil {
builder.WriteString(strconv.FormatInt(*k.Int, 10))
}
if k.String != nil {
builder.WriteString(*k.String)
}
builder.WriteString("]")
}
}
return builder.String()
}
func (p *Parser[K]) newPath(path *path) (*basePath[K], error) {
if len(path.Fields) == 0 {
return nil, fmt.Errorf("cannot make a path from zero fields")
}
pathContext, fields, err := p.parsePathContext(path)
if err != nil {
return nil, err
}
originalText := buildOriginalText(path)
var current *basePath[K]
for i := len(fields) - 1; i >= 0; i-- {
current = &basePath[K]{
context: pathContext,
name: fields[i].Name,
keys: newKeys[K](fields[i].Keys),
nextPath: current,
originalText: originalText,
}
}
current.fetched = true
current.originalText = originalText
return current, nil
}
func (p *Parser[K]) parsePathContext(path *path) (string, []field, error) {
hasPathContextNames := len(p.pathContextNames) > 0
if path.Context != "" {
// no pathContextNames means the Parser isn't handling the grammar path's context yet,
// so it falls back to the previous behavior with the path.Context value as the first
// path's segment.
if !hasPathContextNames {
return "", append([]field{{Name: path.Context}}, path.Fields...), nil
}
if _, ok := p.pathContextNames[path.Context]; !ok {
return "", path.Fields, fmt.Errorf(`context "%s" from path "%s" is not valid, it must be replaced by one of: %s`, path.Context, buildOriginalText(path), p.buildPathContextNamesText(""))
}
return path.Context, path.Fields, nil
}
if hasPathContextNames {
originalText := buildOriginalText(path)
return "", nil, fmt.Errorf(`missing context name for path "%s", possibly valid options are: %s`, originalText, p.buildPathContextNamesText(originalText))
}
return "", path.Fields, nil
}
func (p *Parser[K]) buildPathContextNamesText(path string) string {
var builder strings.Builder
var suffix string
if path != "" {
suffix = "." + path
}
i := 0
for ctx := range p.pathContextNames {
builder.WriteString(fmt.Sprintf(`"%s%s"`, ctx, suffix))
if i != len(p.pathContextNames)-1 {
builder.WriteString(", ")
}
i++
}
return builder.String()
}
// Path represents a chain of path parts in an OTTL statement, such as `body.string`.
// A Path has a name, and potentially a set of keys.
// If the path in the OTTL statement contains multiple parts (separated by a dot (`.`)), then the Path will have a pointer to the next Path.
type Path[K any] interface {
// Context is the OTTL context name of this Path.
Context() string
// Name is the name of this segment of the path.
Name() string
// Next provides the next path segment for this Path.
// Will return nil if there is no next path.
Next() Path[K]
// Keys provides the Keys for this Path.
// Will return nil if there are no Keys.
Keys() []Key[K]
// String returns a string representation of this Path and the next Paths
String() string
}
var _ Path[any] = &basePath[any]{}
type basePath[K any] struct {
context string
name string
keys []Key[K]
nextPath *basePath[K]
fetched bool
fetchedKeys bool
originalText string
}
func (p *basePath[K]) Context() string {
return p.context
}
func (p *basePath[K]) Name() string {
return p.name
}
func (p *basePath[K]) Next() Path[K] {
if p.nextPath == nil {
return nil
}
p.nextPath.fetched = true
return p.nextPath
}
func (p *basePath[K]) Keys() []Key[K] {
if p.keys == nil {
return nil
}
p.fetchedKeys = true
return p.keys
}
func (p *basePath[K]) String() string {
return p.originalText
}
func (p *basePath[K]) isComplete() error {
if !p.fetched {
return fmt.Errorf("the path section %q was not used by the context - this likely means you are using extra path sections", p.name)
}
if p.keys != nil && !p.fetchedKeys {
return fmt.Errorf("the keys indexing %q were not used by the context - this likely means you are trying to index a path that does not support indexing", p.name)
}
if p.nextPath == nil {
return nil
}
return p.nextPath.isComplete()
}
func newKeys[K any](keys []key) []Key[K] {
if len(keys) == 0 {
return nil
}
ks := make([]Key[K], len(keys))
for i := range keys {
ks[i] = &baseKey[K]{
s: keys[i].String,
i: keys[i].Int,
}
}
return ks
}
// Key represents a chain of keys in an OTTL statement, such as `attributes["foo"]["bar"]`.
// A Key has a String or Int, and potentially the next Key.
// If the path in the OTTL statement contains multiple keys, then the Key will have a pointer to the next Key.
type Key[K any] interface {
// String returns a pointer to the Key's string value.
// If the Key does not have a string value the returned value is nil.
// If Key experiences an error retrieving the value it is returned.
String(context.Context, K) (*string, error)
// Int returns a pointer to the Key's int value.
// If the Key does not have a int value the returned value is nil.
// If Key experiences an error retrieving the value it is returned.
Int(context.Context, K) (*int64, error)
}
var _ Key[any] = &baseKey[any]{}
type baseKey[K any] struct {
s *string
i *int64
}
func (k *baseKey[K]) String(_ context.Context, _ K) (*string, error) {
return k.s, nil
}
func (k *baseKey[K]) Int(_ context.Context, _ K) (*int64, error) {
return k.i, nil
}
func (p *Parser[K]) parsePath(ip *basePath[K]) (GetSetter[K], error) {
g, err := p.pathParser(ip)
if err != nil {
return nil, err
}
err = ip.isComplete()
if err != nil {
return nil, err
}
return g, nil
}
func (p *Parser[K]) newFunctionCall(ed editor) (Expr[K], error) {
f, ok := p.functions[ed.Function]
if !ok {
return Expr[K]{}, fmt.Errorf("undefined function %q", ed.Function)
}
defaultArgs := f.CreateDefaultArguments()
var args Arguments
// A nil value indicates the function takes no arguments.
if defaultArgs != nil {
// Pointer values are necessary to fulfill the Go reflection
// settability requirements. Non-pointer values are not
// modifiable through reflection.
if reflect.TypeOf(defaultArgs).Kind() != reflect.Pointer {
return Expr[K]{}, fmt.Errorf("factory for %q must return a pointer to an Arguments value in its CreateDefaultArguments method", ed.Function)
}
args = reflect.New(reflect.ValueOf(defaultArgs).Elem().Type()).Interface()
err := p.buildArgs(ed, reflect.ValueOf(args).Elem())
if err != nil {
return Expr[K]{}, fmt.Errorf("error while parsing arguments for call to %q: %w", ed.Function, err)
}
}
fn, err := f.CreateFunction(FunctionContext{Set: p.telemetrySettings}, args)
if err != nil {
return Expr[K]{}, fmt.Errorf("couldn't create function: %w", err)
}
return Expr[K]{exprFunc: fn}, err
}
func (p *Parser[K]) buildArgs(ed editor, argsVal reflect.Value) error {
requiredArgs := 0
seenNamed := false
for i := 0; i < len(ed.Arguments); i++ {
if !seenNamed && ed.Arguments[i].Name != "" {
seenNamed = true
} else if seenNamed && ed.Arguments[i].Name == "" {
return errors.New("unnamed argument used after named argument")
}
}
for i := 0; i < argsVal.NumField(); i++ {
if !strings.HasPrefix(argsVal.Field(i).Type().Name(), "Optional") {
requiredArgs++
}
}
if len(ed.Arguments) < requiredArgs || len(ed.Arguments) > argsVal.NumField() {
return fmt.Errorf("incorrect number of arguments. Expected: %d Received: %d", argsVal.NumField(), len(ed.Arguments))
}
for i, edArg := range ed.Arguments {
var field reflect.Value
var fieldType reflect.Type
var isOptional bool
var arg argument
if edArg.Name == "" {
field = argsVal.Field(i)
fieldType = field.Type()
isOptional = strings.HasPrefix(fieldType.Name(), "Optional")
arg = ed.Arguments[i]
} else {
field = argsVal.FieldByName(strcase.ToCamel(edArg.Name))
if !field.IsValid() {
return fmt.Errorf("no such parameter: %s", edArg.Name)
}
fieldType = field.Type()
isOptional = strings.HasPrefix(fieldType.Name(), "Optional")
arg = edArg
}
var val any
var manager optionalManager
var err error
var ok bool
if isOptional {
manager, ok = field.Interface().(optionalManager)
if !ok {
return errors.New("optional type is not manageable by the OTTL parser. This is an error in the OTTL")
}
fieldType = manager.get().Type()
}
switch {
case strings.HasPrefix(fieldType.Name(), "FunctionGetter"):
var name string
switch {
case arg.Value.Enum != nil:
name = string(*arg.Value.Enum)
case arg.FunctionName != nil:
name = *arg.FunctionName
default:
return fmt.Errorf("invalid function name given")
}
f, ok := p.functions[name]
if !ok {
return fmt.Errorf("undefined function %s", name)
}
val = StandardFunctionGetter[K]{FCtx: FunctionContext{Set: p.telemetrySettings}, Fact: f}
case fieldType.Kind() == reflect.Slice:
val, err = p.buildSliceArg(arg.Value, fieldType)
default:
val, err = p.buildArg(arg.Value, fieldType)
}
if err != nil {
return fmt.Errorf("invalid argument at position %v: %w", i, err)
}
if isOptional {
field.Set(manager.set(val))
} else {
field.Set(reflect.ValueOf(val))
}
}
return nil
}
func (p *Parser[K]) buildSliceArg(argVal value, argType reflect.Type) (any, error) {
name := argType.Elem().Name()
switch {
case name == reflect.Uint8.String():
if argVal.Bytes == nil {
return nil, fmt.Errorf("slice parameter must be a byte slice literal")
}
return ([]byte)(*argVal.Bytes), nil
case name == reflect.String.String():
arg, err := buildSlice[string](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case name == reflect.Float64.String():
arg, err := buildSlice[float64](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case name == reflect.Int64.String():
arg, err := buildSlice[int64](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "Getter"):
arg, err := buildSlice[Getter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "PMapGetter"):
arg, err := buildSlice[PMapGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "StringGetter"):
arg, err := buildSlice[StringGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "StringLikeGetter"):
arg, err := buildSlice[StringLikeGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "FloatGetter"):
arg, err := buildSlice[FloatGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "FloatLikeGetter"):
arg, err := buildSlice[FloatLikeGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "IntGetter"):
arg, err := buildSlice[IntGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "IntLikeGetter"):
arg, err := buildSlice[IntLikeGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "DurationGetter"):
arg, err := buildSlice[DurationGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "TimeGetter"):
arg, err := buildSlice[TimeGetter[K]](argVal, argType, p.buildArg, name)
if err != nil {
return nil, err
}
return arg, nil
default:
return nil, fmt.Errorf("unsupported slice type %q for function", argType.Elem().Name())
}
}
// Handle interfaces that can be passed as arguments to OTTL functions.
func (p *Parser[K]) buildArg(argVal value, argType reflect.Type) (any, error) {
name := argType.Name()
switch {
case strings.HasPrefix(name, "Setter"):
fallthrough
case strings.HasPrefix(name, "GetSetter"):
if argVal.Literal == nil || argVal.Literal.Path == nil {
return nil, fmt.Errorf("must be a path")
}
np, err := p.newPath(argVal.Literal.Path)
if err != nil {
return nil, err
}
arg, err := p.parsePath(np)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "Getter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return arg, nil
case strings.HasPrefix(name, "StringGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardStringGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "StringLikeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardStringLikeGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "FloatGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardFloatGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "FloatLikeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardFloatLikeGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "IntGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardIntGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "IntLikeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardIntLikeGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "PMapGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardPMapGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "DurationGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardDurationGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "TimeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardTimeGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "BoolGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardBoolGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "BoolLikeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardBoolLikeGetter[K]{Getter: arg.Get}, nil
case strings.HasPrefix(name, "ByteSliceLikeGetter"):
arg, err := p.newGetter(argVal)
if err != nil {
return nil, err
}
return StandardByteSliceLikeGetter[K]{Getter: arg.Get}, nil
case name == "Enum":
arg, err := p.enumParser((*EnumSymbol)(argVal.Enum))
if err != nil {
return nil, fmt.Errorf("must be an Enum")
}
return *arg, nil
case name == reflect.String.String():
if argVal.String == nil {
return nil, fmt.Errorf("must be a string")
}
return *argVal.String, nil
case name == reflect.Float64.String():
if argVal.Literal == nil || argVal.Literal.Float == nil {
return nil, fmt.Errorf("must be a float")
}
return *argVal.Literal.Float, nil
case name == reflect.Int64.String():
if argVal.Literal == nil || argVal.Literal.Int == nil {
return nil, fmt.Errorf("must be an int")
}
return *argVal.Literal.Int, nil
case name == reflect.Bool.String():
if argVal.Bool == nil {
return nil, fmt.Errorf("must be a bool")
}
return bool(*argVal.Bool), nil
default:
return nil, fmt.Errorf("unsupported argument type: %s", name)
}
}
type buildArgFunc func(value, reflect.Type) (any, error)
func buildSlice[T any](argVal value, argType reflect.Type, buildArg buildArgFunc, name string) (any, error) {
if argVal.List == nil {
return nil, fmt.Errorf("must be a list of type %v", name)
}
vals := []T{}
values := argVal.List.Values
for j := 0; j < len(values); j++ {
untypedVal, err := buildArg(values[j], argType.Elem())
if err != nil {
return nil, fmt.Errorf("error while parsing list argument at index %v: %w", j, err)
}
val, ok := untypedVal.(T)
if !ok {
return nil, fmt.Errorf("invalid element type at list index %v, must be of type %v", j, name)
}
vals = append(vals, val)
}
return vals, nil
}
// optionalManager provides a way for the parser to handle Optional[T] structs
// without needing to know the concrete type of T, which is inaccessible through
// the reflect package.
// Would likely be resolved by https://github.com/golang/go/issues/54393.
type optionalManager interface {
// set takes a non-reflection value and returns a reflect.Value of
// an Optional[T] struct with this value set.
set(val any) reflect.Value
// get returns a reflect.Value value of the value contained within
// an Optional[T]. This allows obtaining a reflect.Type for T.
get() reflect.Value
}
type Optional[T any] struct {
val T
hasValue bool
}
// This is called only by reflection.
// nolint:unused
func (o Optional[T]) set(val any) reflect.Value {
return reflect.ValueOf(Optional[T]{
val: val.(T),
hasValue: true,
})
}
func (o Optional[T]) IsEmpty() bool {
return !o.hasValue
}
func (o Optional[T]) Get() T {
return o.val
}
func (o Optional[T]) get() reflect.Value {
// `(reflect.Value).Call` will create a reflect.Value containing a zero-valued T.
// Trying to create a reflect.Value for T by calling reflect.TypeOf or
// reflect.ValueOf on an empty T value creates an invalid reflect.Value object,
// the `Call` method appears to do extra processing to capture the type.
return reflect.ValueOf(o).MethodByName("Get").Call(nil)[0]
}
// Allows creating an Optional with a value already populated for use in testing
// OTTL functions.
func NewTestingOptional[T any](val T) Optional[T] {
return Optional[T]{
val: val,
hasValue: true,
}
}