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

[filelogreceiver]: Add ability to sort by mtime #28691

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
add featuregate for mtime configuration
  • Loading branch information
BinaryFissionGames committed Oct 30, 2023
commit 5ba50444cd263199ccf09793f22c50277a8f154e
12 changes: 12 additions & 0 deletions pkg/stanza/fileconsumer/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher/internal/filter"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher/internal/finder"
"go.opentelemetry.io/collector/featuregate"
)

var allowOrderByMtime = featuregate.GlobalRegistry().MustRegister(
"filelog.allowOrderByMtime",
featuregate.StageAlpha,
featuregate.WithRegisterDescription("When enabled, allows usage of `ordering_criteria.mode` = `mtime`."),
featuregate.WithRegisterReferenceURL("https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/27812"),
)

const (
Expand Down Expand Up @@ -65,6 +73,10 @@ func New(c Criteria) (*Matcher, error) {
var f filter.Filter
switch c.OrderingCriteria.Method {
case methodMtime:
if !allowOrderByMtime.IsEnabled() {
return nil, fmt.Errorf("feature gate %q must be enabled to use mtime method", allowOrderByMtime.ID())
}

f = filter.NewMTimeFilter()
case methodRegex, "": // If no method is specified, defaults to regex
// regex type with no SortBy indicates no-op
Expand Down
33 changes: 30 additions & 3 deletions pkg/stanza/fileconsumer/matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/featuregate"
)

func TestNew(t *testing.T) {
cases := []struct {
name string
criteria Criteria
expectedErr string
name string
criteria Criteria
expectedErr string
disableMTimeFeaturegate bool
}{
{
name: "IncludeEmpty",
Expand Down Expand Up @@ -188,6 +190,17 @@ func TestNew(t *testing.T) {
},
},
},
{
name: "SortByMtimeDisabled",
criteria: Criteria{
Include: []string{"*.log"},
OrderingCriteria: OrderingCriteria{
Method: methodMtime,
},
},
disableMTimeFeaturegate: true,
expectedErr: `feature gate "filelog.allowOrderByMtime" must be enabled to use mtime method`,
},
{
name: "InvalidMethod",
criteria: Criteria{
Expand All @@ -201,6 +214,10 @@ func TestNew(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if !tc.disableMTimeFeaturegate {
enableSortByMTimeFeature(t)
}

matcher, err := New(tc.criteria)
if tc.expectedErr != "" {
assert.EqualError(t, err, tc.expectedErr)
Expand Down Expand Up @@ -701,6 +718,7 @@ func TestMatcher(t *testing.T) {

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
enableSortByMTimeFeature(t)
cwd, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(t.TempDir()))
Expand Down Expand Up @@ -733,3 +751,12 @@ func TestMatcher(t *testing.T) {
})
}
}

func enableSortByMTimeFeature(t *testing.T) {
if !allowOrderByMtime.IsEnabled() {
require.NoError(t, featuregate.GlobalRegistry().Set(allowOrderByMtime.ID(), true))
t.Cleanup(func() {
require.NoError(t, featuregate.GlobalRegistry().Set(allowOrderByMtime.ID(), false))
})
}
}
Loading