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 input operator files #32076

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,14 +4,10 @@
package syslog // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/syslog"

import (
"bufio"
"errors"
"fmt"
"regexp"
"strconv"

"go.uber.org/zap"
"golang.org/x/text/encoding"

"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 @@ -116,69 +112,3 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {

return nil, fmt.Errorf("need tcp config or udp config")
}

// Input is an operator that listens for log entries over tcp.
type Input struct {
helper.InputOperator
tcp *tcp.Input
udp *udp.Input
parser *syslog.Parser
}

// Start will start listening for log entries over tcp or udp.
func (t *Input) Start(p operator.Persister) error {
if t.tcp != nil {
return t.tcp.Start(p)
}
return t.udp.Start(p)
}

// Stop will stop listening for messages.
func (t *Input) Stop() error {
if t.tcp != nil {
return t.tcp.Stop()
}
return t.udp.Stop()
}

// SetOutputs will set the outputs of the internal syslog parser.
func (t *Input) SetOutputs(operators []operator.Operator) error {
t.parser.SetOutputIDs(t.GetOutputIDs())
return t.parser.SetOutputs(operators)
}

func OctetSplitFuncBuilder(_ encoding.Encoding) (bufio.SplitFunc, error) {
return newOctetFrameSplitFunc(true), nil
}

func newOctetFrameSplitFunc(flushAtEOF bool) bufio.SplitFunc {
frameRegex := regexp.MustCompile(`^[1-9]\d*\s`)
return func(data []byte, atEOF bool) (int, []byte, error) {
frameLoc := frameRegex.FindIndex(data)
if frameLoc == nil {
// Flush if no more data is expected
if len(data) != 0 && atEOF && flushAtEOF {
return len(data), data, nil
}
return 0, nil, nil
}

frameMaxIndex := frameLoc[1]
// Remove the delimiter (space) between length and log, and parse the length
frameLenValue, err := strconv.Atoi(string(data[:frameMaxIndex-1]))
if err != nil {
// This should not be possible because the regex matched.
// However, return an error just in case.
return 0, nil, err
}

advance := frameMaxIndex + frameLenValue
if advance > len(data) {
if atEOF && flushAtEOF {
return len(data), data, nil
}
return 0, nil, nil
}
return advance, data[:advance], nil
}
}
84 changes: 84 additions & 0 deletions pkg/stanza/operator/input/syslog/input.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"bufio"
"regexp"
"strconv"

"golang.org/x/text/encoding"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/tcp"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/input/udp"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/syslog"
)

// Input is an operator that listens for log entries over tcp.
type Input struct {
helper.InputOperator
tcp *tcp.Input
udp *udp.Input
parser *syslog.Parser
}

// Start will start listening for log entries over tcp or udp.
func (i *Input) Start(p operator.Persister) error {
if i.tcp != nil {
return i.tcp.Start(p)
}
return i.udp.Start(p)
}

// Stop will stop listening for messages.
func (i *Input) Stop() error {
if i.tcp != nil {
return i.tcp.Stop()
}
return i.udp.Stop()
}

// SetOutputs will set the outputs of the internal syslog parser.
func (i *Input) SetOutputs(operators []operator.Operator) error {
i.parser.SetOutputIDs(i.GetOutputIDs())
return i.parser.SetOutputs(operators)
}

func OctetSplitFuncBuilder(_ encoding.Encoding) (bufio.SplitFunc, error) {
return newOctetFrameSplitFunc(true), nil
}

func newOctetFrameSplitFunc(flushAtEOF bool) bufio.SplitFunc {
frameRegex := regexp.MustCompile(`^[1-9]\d*\s`)
return func(data []byte, atEOF bool) (int, []byte, error) {
frameLoc := frameRegex.FindIndex(data)
if frameLoc == nil {
// Flush if no more data is expected
if len(data) != 0 && atEOF && flushAtEOF {
return len(data), data, nil
}
return 0, nil, nil
}

frameMaxIndex := frameLoc[1]
// Remove the delimiter (space) between length and log, and parse the length
frameLenValue, err := strconv.Atoi(string(data[:frameMaxIndex-1]))
if err != nil {
// This should not be possible because the regex matched.
// However, return an error just in case.
return 0, nil, err
}

advance := frameMaxIndex + frameLenValue
if advance > len(data) {
if atEOF && flushAtEOF {
return len(data), data, nil
}
return 0, nil, nil
}
return advance, data[:advance], nil
}
}