-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathinput.go
84 lines (72 loc) · 2.41 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
}
}