Skip to content

Commit

Permalink
[chore][pkg/stanza] Cleanup syslog parser operator files (#32118)
Browse files Browse the repository at this point in the history
Contributes to #32058
  • Loading branch information
djaglowski authored Apr 2, 2024
1 parent c1f6503 commit 4e9baa7
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 147 deletions.
109 changes: 109 additions & 0 deletions pkg/stanza/operator/parser/syslog/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package syslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/syslog"

import (
"errors"
"fmt"
"strings"
"time"

"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
)

const (
operatorType = "syslog_parser"

RFC3164 = "rfc3164"
RFC5424 = "rfc5424"

NULTrailer = "NUL"
LFTrailer = "LF"
)

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

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

// NewConfigWithID creates a new syslog parser config with default values
func NewConfigWithID(operatorID string) *Config {
return &Config{
ParserConfig: helper.NewParserConfig(operatorID, operatorType),
}
}

// Config is the configuration of a syslog parser operator.
type Config struct {
helper.ParserConfig `mapstructure:",squash"`
BaseConfig `mapstructure:",squash"`
}

// BaseConfig is the detailed configuration of a syslog parser.
type BaseConfig struct {
Protocol string `mapstructure:"protocol,omitempty"`
Location string `mapstructure:"location,omitempty"`
EnableOctetCounting bool `mapstructure:"enable_octet_counting,omitempty"`
AllowSkipPriHeader bool `mapstructure:"allow_skip_pri_header,omitempty"`
NonTransparentFramingTrailer *string `mapstructure:"non_transparent_framing_trailer,omitempty"`
}

// Build will build a JSON parser operator.
func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
if c.ParserConfig.TimeParser == nil {
parseFromField := entry.NewAttributeField("timestamp")
c.ParserConfig.TimeParser = &helper.TimeParser{
ParseFrom: &parseFromField,
LayoutType: helper.NativeKey,
}
}

parserOperator, err := c.ParserConfig.Build(logger)
if err != nil {
return nil, err
}

proto := strings.ToLower(c.Protocol)

switch {
case proto == "":
return nil, fmt.Errorf("missing field 'protocol'")
case proto != RFC5424 && (c.NonTransparentFramingTrailer != nil || c.EnableOctetCounting):
return nil, errors.New("octet_counting and non_transparent_framing are only compatible with protocol rfc5424")
case proto == RFC5424 && (c.NonTransparentFramingTrailer != nil && c.EnableOctetCounting):
return nil, errors.New("only one of octet_counting or non_transparent_framing can be enabled")
case proto == RFC5424 && c.NonTransparentFramingTrailer != nil:
if *c.NonTransparentFramingTrailer != NULTrailer && *c.NonTransparentFramingTrailer != LFTrailer {
return nil, fmt.Errorf("invalid non_transparent_framing_trailer '%s'. Must be either 'LF' or 'NUL'", *c.NonTransparentFramingTrailer)
}
case proto != RFC5424 && proto != RFC3164:
return nil, fmt.Errorf("unsupported protocol version: %s", proto)
}

if c.Location == "" {
c.Location = "UTC"
}

location, err := time.LoadLocation(c.Location)
if err != nil {
return nil, fmt.Errorf("failed to load location %s: %w", c.Location, err)
}

return &Parser{
ParserOperator: parserOperator,
protocol: proto,
location: location,
enableOctetCounting: c.EnableOctetCounting,
allowSkipPriHeader: c.AllowSkipPriHeader,
nonTransparentFramingTrailer: c.NonTransparentFramingTrailer,
}, nil
}
Loading

0 comments on commit 4e9baa7

Please sign in to comment.