-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mdatagen: add wildcard name matching for configs
This PR adds support for wildcard name matching. Generated MetricsConfig and ResourceAttributesConfig will now support providing names not just as full names, but also using `*` wildcards and multimatching with `{x,y,etc}`. This allows you to apply configs to groups of metrics and resource attributes, simplifying configs. Signed-off-by: braydonk <[email protected]>
- Loading branch information
Showing
5 changed files
with
553 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
component: mdatagen | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Adds wildcard matching for names in metrics and resource attributes configs. | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/31285] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
// Code generated by mdatagen. DO NOT EDIT. | ||
|
||
package metadata | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
var ErrNotMultimatch = errors.New("this index doesn't represent a valid multimatch pattern") | ||
|
||
type Pattern struct { | ||
pattern string | ||
level int | ||
hasWildcard bool | ||
valueAssign any | ||
} | ||
|
||
func NewPattern(pattern string, valueAssign any) Pattern { | ||
level := 0 | ||
for _, c := range pattern { | ||
if c == '.' { | ||
level++ | ||
} | ||
} | ||
wildcard := strings.Contains(pattern, "*") | ||
return Pattern{ | ||
pattern: pattern, | ||
level: level, | ||
hasWildcard: wildcard, | ||
valueAssign: valueAssign, | ||
} | ||
} | ||
|
||
func (p Pattern) Match(s string) bool { | ||
if len(s) == 0 { | ||
return true | ||
} | ||
lastWildcard := -1 | ||
patternScan := &scanner{str: p.pattern} | ||
strScan := &scanner{str: s} | ||
for !strScan.isFinished() { | ||
strChar, err := strScan.current() | ||
if err != nil { | ||
return false | ||
} | ||
patternChar, err := patternScan.current() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
switch patternChar { | ||
case '*': | ||
if patternScan.isLast() { | ||
return true | ||
} | ||
patternNext, err := patternScan.peek() | ||
if err == nil && strChar == patternNext { | ||
lastWildcard = patternScan.idx | ||
patternScan.idx += 2 | ||
} | ||
|
||
case '{': | ||
allowedMatches, err := patternScan.parseMultimatch() | ||
if err == nil { | ||
if !strScan.tryMultimatch(allowedMatches) { | ||
return false | ||
} | ||
// Breaks the switch statement | ||
break | ||
} | ||
// Fallthrough if the multimatch parsing failed | ||
fallthrough | ||
|
||
default: | ||
if strChar != patternChar { | ||
if lastWildcard == -1 { | ||
return false | ||
} | ||
patternScan.idx = lastWildcard | ||
lastWildcard = -1 | ||
} else { | ||
patternScan.idx++ | ||
} | ||
} | ||
|
||
strScan.idx++ | ||
} | ||
return patternScan.isFinished() | ||
} | ||
|
||
type Patterns []Pattern | ||
|
||
func AddPattern(ps *Patterns, p Pattern) { | ||
newPatterns := append(*ps, p) | ||
sort.SliceStable(newPatterns, func(i, j int) bool { | ||
if newPatterns[i].hasWildcard && newPatterns[j].hasWildcard { | ||
return newPatterns[i].level < newPatterns[j].level | ||
} | ||
return newPatterns[i].hasWildcard | ||
}) | ||
*ps = newPatterns | ||
} | ||
|
||
type scanner struct { | ||
str string | ||
idx int | ||
} | ||
|
||
func (s *scanner) current() (rune, error) { | ||
if s.isFinished() { | ||
return 0, io.EOF | ||
} | ||
|
||
return rune(s.str[s.idx]), nil | ||
} | ||
|
||
func (s *scanner) peek() (rune, error) { | ||
if s.idx >= len(s.str)-1 { | ||
return 0, io.EOF | ||
} | ||
|
||
return rune(s.str[s.idx+1]), nil | ||
} | ||
|
||
func (s *scanner) isLast() bool { | ||
return s.idx == len(s.str)-1 | ||
} | ||
|
||
func (s *scanner) isFinished() bool { | ||
return s.idx >= len(s.str) | ||
} | ||
|
||
func (s *scanner) parseMultimatch() ([]string, error) { | ||
current, err := s.current() | ||
if err != nil || current != '{' { | ||
return nil, ErrNotMultimatch | ||
} | ||
startIdx := s.idx | ||
s.idx++ | ||
matchStrings := []string{} | ||
currentStr := "" | ||
for !s.isFinished() { | ||
c, err := s.current() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch c { | ||
case '}': | ||
matchStrings = append(matchStrings, currentStr) | ||
s.idx++ | ||
return matchStrings, nil | ||
|
||
case ',': | ||
matchStrings = append(matchStrings, currentStr) | ||
currentStr = "" | ||
|
||
default: | ||
currentStr += string(c) | ||
} | ||
|
||
s.idx++ | ||
} | ||
|
||
s.idx = startIdx | ||
return nil, ErrNotMultimatch | ||
} | ||
|
||
func (s *scanner) tryMultimatch(multimatch []string) bool { | ||
if len(multimatch) == 0 { | ||
return true | ||
} | ||
startIdx := s.idx | ||
for _, m := range multimatch { | ||
matchScan := &scanner{str: m} | ||
for !s.isFinished() { | ||
matchChar, err := matchScan.current() | ||
if err != nil { | ||
break | ||
} | ||
strChar, err := s.current() | ||
if err != nil { | ||
break | ||
} | ||
|
||
if matchChar != strChar { | ||
s.idx = startIdx | ||
break | ||
} | ||
|
||
matchScan.idx++ | ||
if matchScan.isFinished() { | ||
return true | ||
} | ||
s.idx++ | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ContainsPattern(keys []string) bool { | ||
for _, key := range keys { | ||
if strings.Contains(key, "*") || strings.Contains(key, "{") { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ExpandPatternMap(patternMap map[string]any, matchNames []string) map[string]any { | ||
expandedMap := map[string]any{} | ||
patterns := Patterns{} | ||
|
||
for pattern, assign := range patternMap { | ||
AddPattern(&patterns, NewPattern(pattern, assign)) | ||
} | ||
|
||
for _, pattern := range patterns { | ||
matched := []string{} | ||
for _, name := range matchNames { | ||
if pattern.Match(name) { | ||
matched = append(matched, name) | ||
} | ||
} | ||
for _, name := range matched { | ||
expandedMap[name] = pattern.valueAssign | ||
} | ||
} | ||
|
||
return expandedMap | ||
} |
Oops, something went wrong.