diff --git a/pkg/stanza/operator/parser/regex/regex.go b/pkg/stanza/operator/parser/regex/config.go similarity index 62% rename from pkg/stanza/operator/parser/regex/regex.go rename to pkg/stanza/operator/parser/regex/config.go index afb20913067e..3cbfe8f8d1a4 100644 --- a/pkg/stanza/operator/parser/regex/regex.go +++ b/pkg/stanza/operator/parser/regex/config.go @@ -4,13 +4,11 @@ package regex // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex" import ( - "context" "fmt" "regexp" "go.uber.org/zap" - "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/errors" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" @@ -86,64 +84,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) { return op, nil } - -// Parser is an operator that parses regex in an entry. -type Parser struct { - helper.ParserOperator - regexp *regexp.Regexp - cache cache -} - -func (r *Parser) Stop() error { - if r.cache != nil { - r.cache.stop() - } - return nil -} - -// Process will parse an entry for regex. -func (r *Parser) Process(ctx context.Context, entry *entry.Entry) error { - return r.ParserOperator.ProcessWith(ctx, entry, r.parse) -} - -// parse will parse a value using the supplied regex. -func (r *Parser) parse(value any) (any, error) { - var raw string - switch m := value.(type) { - case string: - raw = m - default: - return nil, fmt.Errorf("type '%T' cannot be parsed as regex", value) - } - return r.match(raw) -} - -func (r *Parser) match(value string) (any, error) { - if r.cache != nil { - if x := r.cache.get(value); x != nil { - return x, nil - } - } - - matches := r.regexp.FindStringSubmatch(value) - if matches == nil { - return nil, fmt.Errorf("regex pattern does not match") - } - - parsedValues := map[string]any{} - for i, subexp := range r.regexp.SubexpNames() { - if i == 0 { - // Skip whole match - continue - } - if subexp != "" { - parsedValues[subexp] = matches[i] - } - } - - if r.cache != nil { - r.cache.add(value, parsedValues) - } - - return parsedValues, nil -} diff --git a/pkg/stanza/operator/parser/regex/parser.go b/pkg/stanza/operator/parser/regex/parser.go new file mode 100644 index 000000000000..2821ea551293 --- /dev/null +++ b/pkg/stanza/operator/parser/regex/parser.go @@ -0,0 +1,74 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package regex // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex" + +import ( + "context" + "fmt" + "regexp" + + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper" +) + +// Parser is an operator that parses regex in an entry. +type Parser struct { + helper.ParserOperator + regexp *regexp.Regexp + cache cache +} + +func (p *Parser) Stop() error { + if p.cache != nil { + p.cache.stop() + } + return nil +} + +// Process will parse an entry for regex. +func (p *Parser) Process(ctx context.Context, entry *entry.Entry) error { + return p.ParserOperator.ProcessWith(ctx, entry, p.parse) +} + +// parse will parse a value using the supplied regex. +func (p *Parser) parse(value any) (any, error) { + var raw string + switch m := value.(type) { + case string: + raw = m + default: + return nil, fmt.Errorf("type '%T' cannot be parsed as regex", value) + } + return p.match(raw) +} + +func (p *Parser) match(value string) (any, error) { + if p.cache != nil { + if x := p.cache.get(value); x != nil { + return x, nil + } + } + + matches := p.regexp.FindStringSubmatch(value) + if matches == nil { + return nil, fmt.Errorf("regex pattern does not match") + } + + parsedValues := map[string]any{} + for i, subexp := range p.regexp.SubexpNames() { + if i == 0 { + // Skip whole match + continue + } + if subexp != "" { + parsedValues[subexp] = matches[i] + } + } + + if p.cache != nil { + p.cache.add(value, parsedValues) + } + + return parsedValues, nil +} diff --git a/pkg/stanza/operator/parser/regex/regex_test.go b/pkg/stanza/operator/parser/regex/parser_test.go similarity index 100% rename from pkg/stanza/operator/parser/regex/regex_test.go rename to pkg/stanza/operator/parser/regex/parser_test.go