Skip to content

Commit

Permalink
[pkg/stanza] Standardize function and struct names on key_value_parser (
Browse files Browse the repository at this point in the history
open-telemetry#13273)

All other operators have already been updated to match this standard.
  • Loading branch information
djaglowski authored Aug 15, 2022
1 parent e26ca3b commit 5392aed
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 66 deletions.
20 changes: 10 additions & 10 deletions pkg/stanza/operator/parser/keyvalue/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,39 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper/operatortest"
)

func TestKVParserConfig(t *testing.T) {
func TestConfig(t *testing.T) {
cases := []operatortest.ConfigUnmarshalTest{
{
Name: "default",
Expect: defaultCfg(),
},
{
Name: "parse_from_simple",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
cfg.ParseFrom = entry.NewBodyField("from")
return cfg
}(),
},
{
Name: "parse_to_simple",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
cfg.ParseTo = entry.NewBodyField("log")
return cfg
}(),
},
{
Name: "on_error_drop",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
cfg.OnError = "drop"
return cfg
}(),
},
{
Name: "timestamp",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
parseField := entry.NewBodyField("timestamp_field")
newTime := helper.TimeParser{
Expand All @@ -67,7 +67,7 @@ func TestKVParserConfig(t *testing.T) {
},
{
Name: "severity",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
parseField := entry.NewBodyField("severity_field")
severityField := helper.NewSeverityConfig()
Expand All @@ -85,15 +85,15 @@ func TestKVParserConfig(t *testing.T) {
},
{
Name: "delimiter",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
cfg.Delimiter = ";"
return cfg
}(),
},
{
Name: "pair_delimiter",
Expect: func() *KVParserConfig {
Expect: func() *Config {
cfg := defaultCfg()
cfg.PairDelimiter = ";"
return cfg
Expand All @@ -108,6 +108,6 @@ func TestKVParserConfig(t *testing.T) {
}
}

func defaultCfg() *KVParserConfig {
return NewKVParserConfig("key_value_parser")
func defaultCfg() *Config {
return NewConfig()
}
35 changes: 21 additions & 14 deletions pkg/stanza/operator/parser/keyvalue/keyvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,35 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const operatorType = "key_value_parser"

func init() {
operator.Register("key_value_parser", func() operator.Builder { return NewKVParserConfig("") })
operator.Register(operatorType, func() operator.Builder { return NewConfig() })
}

// NewConfig creates a new key value parser config with default values
func NewConfig() *Config {
return NewConfigWithID(operatorType)
}

// NewKVParserConfig creates a new key value parser config with default values
func NewKVParserConfig(operatorID string) *KVParserConfig {
return &KVParserConfig{
ParserConfig: helper.NewParserConfig(operatorID, "key_value_parser"),
// NewConfigWithID creates a new key value parser config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
ParserConfig: helper.NewParserConfig(operatorID, operatorType),
Delimiter: "=",
}
}

// KVParserConfig is the configuration of a key value parser operator.
type KVParserConfig struct {
// Config is the configuration of a key value parser operator.
type Config struct {
helper.ParserConfig `mapstructure:",squash" yaml:",inline"`

Delimiter string `mapstructure:"delimiter" yaml:"delimiter"`
PairDelimiter string `mapstructure:"pair_delimiter" yaml:"pair_delimiter"`
}

// Build will build a key value parser operator.
func (c KVParserConfig) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
parserOperator, err := c.ParserConfig.Build(logger)
if err != nil {
return nil, err
Expand All @@ -72,27 +79,27 @@ func (c KVParserConfig) Build(logger *zap.SugaredLogger) (operator.Operator, err
}
}

return &KVParser{
return &Parser{
ParserOperator: parserOperator,
delimiter: c.Delimiter,
pairSplitFunc: pairSplitFunc,
}, nil
}

// KVParser is an operator that parses key value pairs.
type KVParser struct {
// Parser is an operator that parses key value pairs.
type Parser struct {
helper.ParserOperator
delimiter string
pairSplitFunc func(input string) []string
}

// Process will parse an entry for key value pairs.
func (kv *KVParser) Process(ctx context.Context, entry *entry.Entry) error {
func (kv *Parser) Process(ctx context.Context, entry *entry.Entry) error {
return kv.ParserOperator.ProcessWith(ctx, entry, kv.parse)
}

// parse will parse a value as key values.
func (kv *KVParser) parse(value interface{}) (interface{}, error) {
func (kv *Parser) parse(value interface{}) (interface{}, error) {
switch m := value.(type) {
case string:
return kv.parser(m, kv.delimiter)
Expand All @@ -101,7 +108,7 @@ func (kv *KVParser) parse(value interface{}) (interface{}, error) {
}
}

func (kv *KVParser) parser(input string, delimiter string) (map[string]interface{}, error) {
func (kv *Parser) parser(input string, delimiter string) (map[string]interface{}, error) {
if input == "" {
return nil, fmt.Errorf("parse from field %s is empty", kv.ParseFrom.String())
}
Expand Down
Loading

0 comments on commit 5392aed

Please sign in to comment.