|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ctxprofile // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxprofile" |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/pdata/pprofile" |
| 11 | + |
| 12 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror" |
| 14 | +) |
| 15 | + |
| 16 | +type valueTypeSource[K Context] = func(ctx K) pprofile.ValueType |
| 17 | + |
| 18 | +func valueTypeGetterSetter[K Context]( |
| 19 | + path ottl.Path[K], |
| 20 | + source valueTypeSource[K], |
| 21 | +) (ottl.GetSetter[K], error) { |
| 22 | + if path == nil || path.Next() == nil { |
| 23 | + return accessValueType(path, source), nil |
| 24 | + } |
| 25 | + |
| 26 | + nextPath := path.Next() |
| 27 | + switch nextPath.Name() { |
| 28 | + case "type": |
| 29 | + return accessValueTypeType[K](nextPath, source), nil |
| 30 | + case "unit": |
| 31 | + return accessValueTypeUnit[K](nextPath, source), nil |
| 32 | + default: |
| 33 | + return nil, ctxerror.New(path.Name(), nextPath.String(), Name, DocRef) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func accessValueType[K Context](path ottl.Path[K], getValueType valueTypeSource[K]) ottl.GetSetter[K] { |
| 38 | + return ottl.StandardGetSetter[K]{ |
| 39 | + Getter: func(_ context.Context, tCtx K) (any, error) { |
| 40 | + return getValueType(tCtx), nil |
| 41 | + }, |
| 42 | + Setter: func(_ context.Context, tCtx K, val any) error { |
| 43 | + newValue, ok := val.(pprofile.ValueType) |
| 44 | + if !ok { |
| 45 | + return fmt.Errorf("expected a pprofile.ValueType value for path %q, got %T", path.String(), val) |
| 46 | + } |
| 47 | + newValue.CopyTo(getValueType(tCtx)) |
| 48 | + return nil |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func accessValueTypeType[K Context](path ottl.Path[K], getValueType valueTypeSource[K]) ottl.GetSetter[K] { |
| 54 | + return ottl.StandardGetSetter[K]{ |
| 55 | + Getter: func(_ context.Context, tCtx K) (any, error) { |
| 56 | + valueType := getValueType(tCtx) |
| 57 | + return getValueTypeString(path, tCtx.GetProfilesDictionary(), valueType.TypeStrindex()) |
| 58 | + }, |
| 59 | + Setter: func(_ context.Context, tCtx K, val any) error { |
| 60 | + valueType := getValueType(tCtx) |
| 61 | + newIndex, err := setValueTypeString(path, tCtx.GetProfilesDictionary(), valueType.TypeStrindex(), val) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + valueType.SetTypeStrindex(newIndex) |
| 66 | + return nil |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func accessValueTypeUnit[K Context](path ottl.Path[K], getValueType valueTypeSource[K]) ottl.GetSetter[K] { |
| 72 | + return ottl.StandardGetSetter[K]{ |
| 73 | + Getter: func(_ context.Context, tCtx K) (any, error) { |
| 74 | + valueType := getValueType(tCtx) |
| 75 | + return getValueTypeString(path, tCtx.GetProfilesDictionary(), valueType.UnitStrindex()) |
| 76 | + }, |
| 77 | + Setter: func(_ context.Context, tCtx K, val any) error { |
| 78 | + valueType := getValueType(tCtx) |
| 79 | + newIndex, err := setValueTypeString(path, tCtx.GetProfilesDictionary(), valueType.UnitStrindex(), val) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + valueType.SetUnitStrindex(newIndex) |
| 84 | + return nil |
| 85 | + }, |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func getValueTypeString[K Context]( |
| 90 | + path ottl.Path[K], |
| 91 | + dict pprofile.ProfilesDictionary, |
| 92 | + currIndex int32, |
| 93 | +) (string, error) { |
| 94 | + if currIndex < 0 || int(currIndex) >= dict.StringTable().Len() { |
| 95 | + return "", fmt.Errorf("path %q with strindex %d is out of range", path.String(), currIndex) |
| 96 | + } |
| 97 | + return dict.StringTable().At(int(currIndex)), nil |
| 98 | +} |
| 99 | + |
| 100 | +func setValueTypeString[K Context]( |
| 101 | + path ottl.Path[K], |
| 102 | + dict pprofile.ProfilesDictionary, |
| 103 | + currIndex int32, |
| 104 | + val any, |
| 105 | +) (int32, error) { |
| 106 | + newValue, ok := val.(string) |
| 107 | + if !ok { |
| 108 | + return 0, fmt.Errorf("expected a string value for path %q, got %T", path.String(), val) |
| 109 | + } |
| 110 | + if currIndex != 0 && int(currIndex) < dict.StringTable().Len() { |
| 111 | + currValue := dict.StringTable().At(int(currIndex)) |
| 112 | + if currValue == newValue { |
| 113 | + return currIndex, nil |
| 114 | + } |
| 115 | + } |
| 116 | + newIndex, err := pprofile.SetString(dict.StringTable(), newValue) |
| 117 | + if err != nil { |
| 118 | + return 0, err |
| 119 | + } |
| 120 | + return newIndex, nil |
| 121 | +} |
0 commit comments