-
Notifications
You must be signed in to change notification settings - Fork 537
feat(loki.source.syslog): support raw format #5140
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
5549775
chore: use current go-syslog version for replacements as well
x1unix d10570f
feat: add raw syslog format option
x1unix 8a59b17
feat: implement raw parser
x1unix 6c765a4
feat: add raw read test
x1unix e7c8c39
fix: fix stream parser
x1unix ad24b24
fix: octet counting
x1unix 719b5a3
feat: add octetcount single line case
x1unix 86bc6e5
fix: remove unused ReadLineRaw
x1unix d66f315
feat: add raw support for TCP/UDP streams
x1unix 8aa0196
fix: TCP conn close logging
x1unix c0bd46b
fix: handle CEF logs
x1unix 7feaeae
fix: use helper to prepopulate facility and severity from priority
x1unix b385c48
feat: handle raw messages
x1unix 9158834
fix: handle empty vals
x1unix 36a9e26
fix: use more meaningful name for raw parse opts
x1unix f65d1c6
feat: add raw message parse option
x1unix a9c7022
fix: yaml attribute name
x1unix 21b7e45
feat: map alloy config to raw options
x1unix cbbe412
feat: update promtail yaml mapper
x1unix e526342
fix: component name
x1unix 064f17e
feat: update promtailconvert tests
x1unix f7290fb
feat: update component docs
x1unix 91837dc
fix: use strconv.Atoi instead of ParseInt
x1unix b275b9d
fix: Apply suggestions from docs review
x1unix 0589768
fix: linter
x1unix 5bddfb7
fix: deadlock
x1unix 0025d2b
fix: CEF test
x1unix 1ab7ab5
fix: review
x1unix 894e324
fix: add missing raw_format_options block in blocks list
x1unix 3ed603c
Apply suggestions from code review
x1unix 2ba0e7e
fix: typo
x1unix 786d411
feat: make raw format experimental
x1unix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
internal/component/loki/source/syslog/internal/syslogtarget/syslogparser/rawparser.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| package syslogparser | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "iter" | ||
| "strconv" | ||
| "unicode" | ||
|
|
||
| "github.com/leodido/go-syslog/v4" | ||
| ) | ||
|
|
||
| // IterStreamRaw returns an iterator to read syslog lines from a stream without contents parsing. | ||
| // | ||
| // Delimiter argument is used to determine line end for non-transparent framing. | ||
| func IterStreamRaw(r io.Reader, delimiter byte) iter.Seq2[*syslog.Base, error] { | ||
| return func(yield func(*syslog.Base, error) bool) { | ||
| buf := bufio.NewReaderSize(r, 1<<10) | ||
| for { | ||
| r, err := parseLineRaw(buf, delimiter) | ||
| if err != nil { | ||
| if !errors.Is(err, io.EOF) { | ||
| yield(nil, err) | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| // skip empty lines | ||
| if r == nil { | ||
| continue | ||
| } | ||
|
|
||
| if !yield(r, nil) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func parseLineRaw(buf *bufio.Reader, delimiter byte) (*syslog.Base, error) { | ||
| b, err := buf.ReadByte() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // TODO: use bytebufferpool? | ||
| _ = buf.UnreadByte() | ||
| ftype := framingTypeFromFirstByte(b) | ||
| if ftype == framingTypeOctetCounting { | ||
| contentLength, err := readFrameLength(buf) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read octet length header: %w", err) | ||
| } | ||
|
|
||
| buff := make([]byte, contentLength) | ||
| n, err := buf.Read(buff) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("cannot read message: %w (length: %d)", err, contentLength) | ||
| } | ||
|
|
||
| if n == 0 { | ||
| return nil, fmt.Errorf("empty buffer returned (expected: %d)", contentLength) | ||
| } | ||
|
|
||
| buff = buff[:n] | ||
| return readLogLine(buff), nil | ||
| } | ||
|
|
||
| // NOTE: CEF logs don't have log priority prefix and will be detected as [framingTypeUnknown], but logic still the same. | ||
| buff, err := buf.ReadBytes(delimiter) | ||
| if err != nil { | ||
| // Ignore io.EOF if some data was returned | ||
| if !errors.Is(err, io.EOF) || len(buff) == 0 { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| if len(buff) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // trim potential newline leftovers if called sequentially inside TCP conn. | ||
| buff = bytes.TrimFunc(buff, unicode.IsSpace) | ||
| if len(buff) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| return readLogLine(buff), nil | ||
| } | ||
|
|
||
| func readLogLine(line []byte) *syslog.Base { | ||
| out := &syslog.Base{} | ||
| line = readSeverity(line, out) | ||
|
|
||
| msg := string(bytes.TrimSpace(line)) | ||
| out.Message = &msg | ||
| return out | ||
| } | ||
|
|
||
| func readSeverity(line []byte, dst *syslog.Base) (next []byte) { | ||
| // priority has to be in format '<0-9+>' | ||
| if len(line) < 3 || line[0] != '<' { | ||
| return line | ||
| } | ||
|
|
||
| buff := line[1:] | ||
| priority := uint(0) | ||
| for i, v := range buff { | ||
| if v == '>' { | ||
| if i == 0 || priority > 255 { | ||
| return line | ||
| } | ||
|
|
||
| dst.ComputeFromPriority(uint8(priority)) | ||
| buff = buff[i+1:] | ||
| return buff | ||
| } | ||
|
|
||
| if !isDigit(v) { | ||
| return line | ||
| } | ||
|
|
||
| priority *= 10 | ||
| priority += uint(v - '0') | ||
| } | ||
|
|
||
| return line | ||
| } | ||
|
|
||
| func readFrameLength(r *bufio.Reader) (flen int, err error) { | ||
| // log lines with octet counted framing start with length. | ||
| // Example: `114 <34>1 2025-01-03T14:07:15.003Z message...` | ||
| part, err := r.ReadString(' ') | ||
| if err != nil { | ||
| return 0, fmt.Errorf("%w (read: %q)", err, part) | ||
| } | ||
|
|
||
| if len(part) == 0 { | ||
| return 0, errors.New("missing octet length") | ||
| } | ||
|
|
||
| // ReadString returns value with its delimiter | ||
| part = part[:len(part)-1] | ||
| c, err := strconv.Atoi(part) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("failed to parse octet length from %q: %w", part, err) | ||
| } | ||
|
|
||
| return c, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.