-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore][pkg/stanza] Extract a trim package for managing trim functions (
- Loading branch information
1 parent
fe7ee75
commit a09b4d7
Showing
9 changed files
with
160 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,48 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package trim // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim" | ||
|
||
import ( | ||
"bytes" | ||
) | ||
|
||
type Func func([]byte) []byte | ||
|
||
func Whitespace(preserveLeading, preserveTrailing bool) Func { | ||
if preserveLeading && preserveTrailing { | ||
return noTrim | ||
} | ||
if preserveLeading { | ||
return trimTrailingWhitespacesFunc | ||
} | ||
if preserveTrailing { | ||
return trimLeadingWhitespacesFunc | ||
} | ||
return trimWhitespacesFunc | ||
} | ||
|
||
func noTrim(token []byte) []byte { | ||
return token | ||
} | ||
|
||
func trimLeadingWhitespacesFunc(data []byte) []byte { | ||
// TrimLeft to strip EOF whitespaces in case of using $ in regex | ||
// For some reason newline and carriage return are being moved to beginning of next log | ||
token := bytes.TrimLeft(data, "\r\n\t ") | ||
|
||
// TrimLeft will return nil if data is an empty slice | ||
if token == nil { | ||
return []byte{} | ||
} | ||
return token | ||
} | ||
|
||
func trimTrailingWhitespacesFunc(data []byte) []byte { | ||
// TrimRight to strip all whitespaces from the end of log | ||
return bytes.TrimRight(data, "\r\n\t ") | ||
} | ||
|
||
func trimWhitespacesFunc(data []byte) []byte { | ||
return trimLeadingWhitespacesFunc(trimTrailingWhitespacesFunc(data)) | ||
} |
Oops, something went wrong.