Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][pkg/stanza] Cleanup regex parser operator files #32113

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
}
Loading