warning: ParseIdentifier is used to parse filter policies in pkg/spanfilter/config/config.go if changed, it can break existing config. Behavioral note vs. the previous implementation: this now rejects identifiers containing unquoted spaces (e.g. ".foo bar") that were previously accepted via HasPref
(s string)
| 86 | // unquoted spaces (e.g. ".foo bar") that were previously accepted via HasPrefix matching. |
| 87 | // Quoted identifiers with spaces (e.g. span."foo bar") are still accepted. |
| 88 | func ParseIdentifier(s string) (Attribute, error) { |
| 89 | // Wrap the identifier in curly braces to create a valid spanset filter expression |
| 90 | attr := "{" + s + "}" |
| 91 | expr, err := ParseNoOptimizations(attr) |
| 92 | if err != nil { |
| 93 | return Attribute{}, fmt.Errorf("failed to parse identifier %s: %w", s, err) |
| 94 | } |
| 95 | |
| 96 | // Validate the parsed expression structure |
| 97 | if expr == nil { |
| 98 | return Attribute{}, fmt.Errorf("failed to parse identifier %s: parsed expression is nil", s) |
| 99 | } |
| 100 | |
| 101 | if len(expr.Pipeline.Elements) == 0 { |
| 102 | return Attribute{}, fmt.Errorf("failed to parse identifier %s: no pipeline elements found", s) |
| 103 | } |
| 104 | |
| 105 | // Extract and validate the spanset filter |
| 106 | filter, ok := expr.Pipeline.Elements[0].(*SpansetFilter) |
| 107 | if !ok { |
| 108 | return Attribute{}, fmt.Errorf("failed to parse identifier %s: expected SpansetFilter but got %T", s, expr.Pipeline.Elements[0]) |
| 109 | } |
| 110 | |
| 111 | // Extract and validate the attribute |
| 112 | attribute, ok := filter.Expression.(Attribute) |
| 113 | if !ok { |
| 114 | return Attribute{}, fmt.Errorf("failed to parse identifier %s: expected Attribute but got %T", s, filter.Expression) |
| 115 | } |
| 116 | |
| 117 | return attribute, nil |
| 118 | } |
| 119 | |
| 120 | func MustParseIdentifier(s string) Attribute { |
| 121 | a, err := ParseIdentifier(s) |