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][fileconsumer] Extract splitter, flusher, multiline, encoding into dedicated package #25914

Merged
merged 1 commit into from
Aug 22, 2023
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
2 changes: 1 addition & 1 deletion .chloggen/pkg-stanza-encoding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: deprecation
component: pkg/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate encoding related elements of helper pacakge, in favor of new decode package
note: Deprecate encoding related elements of helper pacakge, in favor of new decoder package

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26019]
Expand Down
40 changes: 40 additions & 0 deletions .chloggen/pkg-stanza-tokenize.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate tokenization related elements of helper pacakge, in favor of new tokenize package

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25914]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: Includes the following deprecations |
- Flusher
- FlusherConfig
- NewFlusherConfig
- Multiline
- MultilineConfig
- NewMultilineConfig
- NewLineStartSplitFunc
- NewLineEndSplitFunc
- NewNewlineSplitFunc
- Splitter
- SplitterConfig
- NewSplitterConfig
- SplitNone

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
29 changes: 15 additions & 14 deletions pkg/stanza/fileconsumer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher"
"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/tokenize"
)

const (
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewConfig() *Config {
IncludeFileNameResolved: false,
IncludeFilePathResolved: false,
PollInterval: 200 * time.Millisecond,
Splitter: helper.NewSplitterConfig(),
Splitter: tokenize.NewSplitterConfig(),
StartAt: "end",
FingerprintSize: fingerprint.DefaultSize,
MaxLogSize: defaultMaxLogSize,
Expand All @@ -61,19 +62,19 @@ func NewConfig() *Config {
// Config is the configuration of a file input operator
type Config struct {
matcher.Criteria `mapstructure:",squash"`
IncludeFileName bool `mapstructure:"include_file_name,omitempty"`
IncludeFilePath bool `mapstructure:"include_file_path,omitempty"`
IncludeFileNameResolved bool `mapstructure:"include_file_name_resolved,omitempty"`
IncludeFilePathResolved bool `mapstructure:"include_file_path_resolved,omitempty"`
PollInterval time.Duration `mapstructure:"poll_interval,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
FingerprintSize helper.ByteSize `mapstructure:"fingerprint_size,omitempty"`
MaxLogSize helper.ByteSize `mapstructure:"max_log_size,omitempty"`
MaxConcurrentFiles int `mapstructure:"max_concurrent_files,omitempty"`
MaxBatches int `mapstructure:"max_batches,omitempty"`
DeleteAfterRead bool `mapstructure:"delete_after_read,omitempty"`
Splitter helper.SplitterConfig `mapstructure:",squash,omitempty"`
Header *HeaderConfig `mapstructure:"header,omitempty"`
IncludeFileName bool `mapstructure:"include_file_name,omitempty"`
IncludeFilePath bool `mapstructure:"include_file_path,omitempty"`
IncludeFileNameResolved bool `mapstructure:"include_file_name_resolved,omitempty"`
IncludeFilePathResolved bool `mapstructure:"include_file_path_resolved,omitempty"`
PollInterval time.Duration `mapstructure:"poll_interval,omitempty"`
StartAt string `mapstructure:"start_at,omitempty"`
FingerprintSize helper.ByteSize `mapstructure:"fingerprint_size,omitempty"`
MaxLogSize helper.ByteSize `mapstructure:"max_log_size,omitempty"`
MaxConcurrentFiles int `mapstructure:"max_concurrent_files,omitempty"`
MaxBatches int `mapstructure:"max_batches,omitempty"`
DeleteAfterRead bool `mapstructure:"delete_after_read,omitempty"`
Splitter tokenize.SplitterConfig `mapstructure:",squash,omitempty"`
Header *HeaderConfig `mapstructure:"header,omitempty"`
}

type HeaderConfig struct {
Expand Down
37 changes: 19 additions & 18 deletions pkg/stanza/fileconsumer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/operatortest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/regex"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

func TestUnmarshal(t *testing.T) {
Expand Down Expand Up @@ -279,7 +280,7 @@ func TestUnmarshal(t *testing.T) {
Name: "multiline_line_start_string",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
newSplit := helper.NewSplitterConfig()
newSplit := tokenize.NewSplitterConfig()
newSplit.Multiline.LineStartPattern = "Start"
cfg.Splitter = newSplit
return newMockOperatorConfig(cfg)
Expand All @@ -289,7 +290,7 @@ func TestUnmarshal(t *testing.T) {
Name: "multiline_line_start_special",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
newSplit := helper.NewSplitterConfig()
newSplit := tokenize.NewSplitterConfig()
newSplit.Multiline.LineStartPattern = "%"
cfg.Splitter = newSplit
return newMockOperatorConfig(cfg)
Expand All @@ -299,7 +300,7 @@ func TestUnmarshal(t *testing.T) {
Name: "multiline_line_end_string",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
newSplit := helper.NewSplitterConfig()
newSplit := tokenize.NewSplitterConfig()
newSplit.Multiline.LineEndPattern = "Start"
cfg.Splitter = newSplit
return newMockOperatorConfig(cfg)
Expand All @@ -309,7 +310,7 @@ func TestUnmarshal(t *testing.T) {
Name: "multiline_line_end_special",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
newSplit := helper.NewSplitterConfig()
newSplit := tokenize.NewSplitterConfig()
newSplit.Multiline.LineEndPattern = "%"
cfg.Splitter = newSplit
return newMockOperatorConfig(cfg)
Expand Down Expand Up @@ -451,8 +452,8 @@ func TestBuild(t *testing.T) {
{
"MultilineConfiguredStartAndEndPatterns",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineEndPattern: "Exists",
LineStartPattern: "Exists",
}
Expand All @@ -463,8 +464,8 @@ func TestBuild(t *testing.T) {
{
"MultilineConfiguredStartPattern",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineStartPattern: "START.*",
}
},
Expand All @@ -474,8 +475,8 @@ func TestBuild(t *testing.T) {
{
"MultilineConfiguredEndPattern",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineEndPattern: "END.*",
}
},
Expand All @@ -493,8 +494,8 @@ func TestBuild(t *testing.T) {
{
"LineStartAndEnd",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineStartPattern: ".*",
LineEndPattern: ".*",
}
Expand All @@ -505,17 +506,17 @@ func TestBuild(t *testing.T) {
{
"NoLineStartOrEnd",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{}
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{}
},
require.NoError,
func(t *testing.T, f *Manager) {},
},
{
"InvalidLineStartRegex",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineStartPattern: "(",
}
},
Expand All @@ -525,8 +526,8 @@ func TestBuild(t *testing.T) {
{
"InvalidLineEndRegex",
func(f *Config) {
f.Splitter = helper.NewSplitterConfig()
f.Splitter.Multiline = helper.MultilineConfig{
f.Splitter = tokenize.NewSplitterConfig()
f.Splitter.Multiline = tokenize.MultilineConfig{
LineEndPattern: "(",
}
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/stanza/fileconsumer/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/testutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

func TestCleanStop(t *testing.T) {
Expand Down Expand Up @@ -545,7 +546,7 @@ func TestNoNewline(t *testing.T) {
tempDir := t.TempDir()
cfg := NewConfig().includeDir(tempDir)
cfg.StartAt = "beginning"
cfg.Splitter = helper.NewSplitterConfig()
cfg.Splitter = tokenize.NewSplitterConfig()
cfg.Splitter.Flusher.Period = time.Nanosecond
operator, emitCalls := buildTestManager(t, cfg)

Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/fileconsumer/internal/header/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"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/pipeline"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

type Config struct {
Expand Down Expand Up @@ -69,7 +69,7 @@ func NewConfig(matchRegex string, metadataOperators []operator.Config, enc encod
return nil, fmt.Errorf("failed to compile `pattern`: %w", err)
}

splitFunc, err := helper.NewNewlineSplitFunc(enc, false, func(b []byte) []byte {
splitFunc, err := tokenize.NewNewlineSplitFunc(enc, false, func(b []byte) []byte {
return bytes.Trim(b, "\r\n")
})
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/stanza/fileconsumer/internal/splitter/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ package splitter // import "github.com/open-telemetry/opentelemetry-collector-co
import (
"bufio"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

type customFactory struct {
Flusher helper.FlusherConfig
Flusher tokenize.FlusherConfig
Splitter bufio.SplitFunc
}

var _ Factory = (*customFactory)(nil)

func NewCustomFactory(
flusher helper.FlusherConfig,
flusher tokenize.FlusherConfig,
splitter bufio.SplitFunc) Factory {
return &customFactory{
Flusher: flusher,
Expand Down
6 changes: 3 additions & 3 deletions pkg/stanza/fileconsumer/internal/splitter/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

func TestCustomFactory(t *testing.T) {
type fields struct {
Flusher helper.FlusherConfig
Flusher tokenize.FlusherConfig
Splitter bufio.SplitFunc
}
type args struct {
Expand All @@ -29,7 +29,7 @@ func TestCustomFactory(t *testing.T) {
{
name: "default configuration",
fields: fields{
Flusher: helper.NewFlusherConfig(),
Flusher: tokenize.NewFlusherConfig(),
Splitter: func(data []byte, atEOF bool) (advance int, token []byte, err error) {
return len(data), data, nil
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/stanza/fileconsumer/internal/splitter/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import (
"bufio"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/decoder"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

type multilineFactory struct {
helper.SplitterConfig
tokenize.SplitterConfig
}

var _ Factory = (*multilineFactory)(nil)

func NewMultilineFactory(splitter helper.SplitterConfig) Factory {
func NewMultilineFactory(splitter tokenize.SplitterConfig) Factory {
return &multilineFactory{
SplitterConfig: splitter,
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/stanza/fileconsumer/internal/splitter/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/tokenize"
)

func TestMultilineBuild(t *testing.T) {
Expand All @@ -17,24 +17,24 @@ func TestMultilineBuild(t *testing.T) {
}
tests := []struct {
name string
splitterConfig helper.SplitterConfig
splitterConfig tokenize.SplitterConfig
args args
wantErr bool
}{
{
name: "default configuration",
splitterConfig: helper.NewSplitterConfig(),
splitterConfig: tokenize.NewSplitterConfig(),
args: args{
maxLogSize: 1024,
},
wantErr: false,
},
{
name: "eoncoding error",
splitterConfig: helper.SplitterConfig{
splitterConfig: tokenize.SplitterConfig{
Encoding: "error",
Flusher: helper.NewFlusherConfig(),
Multiline: helper.NewMultilineConfig(),
Flusher: tokenize.NewFlusherConfig(),
Multiline: tokenize.NewMultilineConfig(),
},
args: args{
maxLogSize: 1024,
Expand All @@ -43,10 +43,10 @@ func TestMultilineBuild(t *testing.T) {
},
{
name: "Multiline error",
splitterConfig: helper.SplitterConfig{
splitterConfig: tokenize.SplitterConfig{
Encoding: "utf-8",
Flusher: helper.NewFlusherConfig(),
Multiline: helper.MultilineConfig{
Flusher: tokenize.NewFlusherConfig(),
Multiline: tokenize.MultilineConfig{
LineStartPattern: "START",
LineEndPattern: "END",
},
Expand Down
Loading