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 syslog parser operator files #32118

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
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