Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup regex parser operator files (#32113)
Browse files Browse the repository at this point in the history
Contributes to #32058
  • Loading branch information
djaglowski committed Apr 4, 2024
1 parent 349156c commit b25b78a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 63 deletions.
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
74 changes: 74 additions & 0 deletions 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
}

0 comments on commit b25b78a

Please sign in to comment.