Skip to content

Commit

Permalink
mdatagen: add wildcard name matching for configs
Browse files Browse the repository at this point in the history
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
braydonk committed May 1, 2024
1 parent fa02afe commit 026ad18
Show file tree
Hide file tree
Showing 5 changed files with 553 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .chloggen/braydonk_mdatagen-wildcard-matching.yaml
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: []
9 changes: 9 additions & 0 deletions cmd/mdatagen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func run(ymlPath string) error {
return err
}

if err = generateFile(filepath.Join(tmplDir, "match.go.tmpl"),
filepath.Join(codeDir, "generated_match.go"), md, "metadata"); err != nil {
return err
}
if err = generateFile(filepath.Join(tmplDir, "match_test.go.tmpl"),
filepath.Join(codeDir, "generated_match_test.go"), md, "metadata"); err != nil {
return err
}

if err = generateFile(filepath.Join(tmplDir, "config.go.tmpl"),
filepath.Join(codeDir, "generated_config.go"), md, "metadata"); err != nil {
return err
Expand Down
38 changes: 38 additions & 0 deletions cmd/mdatagen/templates/config.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
)
{{ if .Metrics -}}

var MetricNames = []string{
{{- range $name, $metric := .Metrics }}
"{{ $name }}",
{{- end }}
}

// MetricConfig provides common config for a particular metric.
type MetricConfig struct {
Enabled bool `mapstructure:"enabled"`
Expand Down Expand Up @@ -45,9 +51,28 @@ func DefaultMetricsConfig() MetricsConfig {
{{- end }}
}
}

func (msc *MetricsConfig) Unmarshal(parser *confmap.Conf) error {
if parser == nil {
return nil
}
if ContainsPattern(parser.AllKeys()) {
confStrMap := parser.ToStringMap()
expandedConfig := ExpandPatternMap(confStrMap, MetricNames)
newParser := confmap.NewFromStringMap(expandedConfig)
return newParser.Unmarshal(msc)
}
return parser.Unmarshal(msc)
}
{{- end }}

{{ if .ResourceAttributes -}}
var ResourceAttributeNames = []string{
{{- range $name, $attr := .ResourceAttributes }}
"{{ $name }}",
{{- end }}
}

// ResourceAttributeConfig provides common config for a particular resource attribute.
type ResourceAttributeConfig struct {
Enabled bool `mapstructure:"enabled"`
Expand Down Expand Up @@ -92,6 +117,19 @@ func DefaultResourceAttributesConfig() ResourceAttributesConfig {
{{- end }}
}
}

func (rasc *ResourceAttributesConfig) Unmarshal(parser *confmap.Conf) error {
if parser == nil {
return nil
}
if ContainsPattern(parser.AllKeys()) {
confStrMap := parser.ToStringMap()
expandedConfig := ExpandPatternMap(confStrMap, ResourceAttributeNames)
newParser := confmap.NewFromStringMap(expandedConfig)
return newParser.Unmarshal(rasc)
}
return parser.Unmarshal(rasc)
}
{{- end }}

{{ if .Metrics -}}
Expand Down
234 changes: 234 additions & 0 deletions cmd/mdatagen/templates/match.go.tmpl
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
}
Loading

0 comments on commit 026ad18

Please sign in to comment.