Skip to content

Commit

Permalink
[pkg/stanza] Fix support of include/exclude patterns on Windows (open…
Browse files Browse the repository at this point in the history
…-telemetry#17194)

Patterns with Windows-style separator `\` in `include`/`exclude` configuration section of stanza-based logs receivers were broken after upgrading the doublestar library.
  • Loading branch information
dmitryax authored Dec 22, 2022
1 parent 020c59c commit cedc744
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
11 changes: 11 additions & 0 deletions .chloggen/fix-stanza-finder-windows.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# 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: Fix support of include/exclude patterns with "\" separators on Windows.

# One or more tracking issues related to the change
issues: [14754]
12 changes: 3 additions & 9 deletions pkg/stanza/fileconsumer/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
package fileconsumer // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer"

import (
"os"
"path/filepath"

"github.com/bmatcuk/doublestar/v4"
)

Expand All @@ -30,14 +27,11 @@ type Finder struct {
func (f Finder) FindFiles() []string {
all := make([]string, 0, len(f.Include))
for _, include := range f.Include {
basepath, pattern := doublestar.SplitPattern(include)
fsys := os.DirFS(basepath)
matches, _ := doublestar.Glob(fsys, pattern) // compile error checked in build
matches, _ := doublestar.FilepathGlob(include) // compile error checked in build
INCLUDE:
for _, match := range matches {
for _, exclude := range f.Exclude {
_, pattern = doublestar.SplitPattern(exclude)
if itMatches, _ := doublestar.PathMatch(pattern, match); itMatches {
if itMatches, _ := doublestar.PathMatch(exclude, match); itMatches {
continue INCLUDE
}
}
Expand All @@ -48,7 +42,7 @@ func (f Finder) FindFiles() []string {
}
}

all = append(all, filepath.Join(basepath, match))
all = append(all, match)
}
}

Expand Down
24 changes: 12 additions & 12 deletions pkg/stanza/fileconsumer/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,31 +96,31 @@ func TestFinder(t *testing.T) {
},
{
name: "IncludeMultipleDirectories",
files: []string{"a/1.log", "a/2.log", "b/1.log", "b/2.log"},
include: []string{"a/*.log", "b/*.log"},
files: []string{filepath.Join("a", "1.log"), filepath.Join("a", "2.log"), filepath.Join("b", "1.log"), filepath.Join("b", "2.log")},
include: []string{filepath.Join("a", "*.log"), filepath.Join("b", "*.log")},
exclude: []string{},
expected: []string{"a/1.log", "a/2.log", "b/1.log", "b/2.log"},
expected: []string{filepath.Join("a", "1.log"), filepath.Join("a", "2.log"), filepath.Join("b", "1.log"), filepath.Join("b", "2.log")},
},
{
name: "IncludeMultipleDirectoriesVaryingDepth",
files: []string{"1.log", "a/1.log", "a/b/1.log", "c/1.log"},
include: []string{"*.log", "a/*.log", "a/b/*.log", "c/*.log"},
files: []string{"1.log", filepath.Join("a", "1.log"), filepath.Join("a", "b", "1.log"), filepath.Join("c", "1.log")},
include: []string{"*.log", filepath.Join("a", "*.log"), filepath.Join("a", "b", "*.log"), filepath.Join("c", "*.log")},
exclude: []string{},
expected: []string{"1.log", "a/1.log", "a/b/1.log", "c/1.log"},
expected: []string{"1.log", filepath.Join("a", "1.log"), filepath.Join("a", "b", "1.log"), filepath.Join("c", "1.log")},
},
{
name: "DoubleStarSameDepth",
files: []string{"a/1.log", "b/1.log", "c/1.log"},
include: []string{"**/*.log"},
files: []string{filepath.Join("a", "1.log"), filepath.Join("b", "1.log"), filepath.Join("c", "1.log")},
include: []string{filepath.Join("**", "*.log")},
exclude: []string{},
expected: []string{"a/1.log", "b/1.log", "c/1.log"},
expected: []string{filepath.Join("a", "1.log"), filepath.Join("b", "1.log"), filepath.Join("c", "1.log")},
},
{
name: "DoubleStarVaryingDepth",
files: []string{"1.log", "a/1.log", "a/b/1.log", "c/1.log"},
include: []string{"**/*.log"},
files: []string{"1.log", filepath.Join("a", "1.log"), filepath.Join("a", "b", "1.log"), filepath.Join("c", "1.log")},
include: []string{filepath.Join("**", "*.log")},
exclude: []string{},
expected: []string{"1.log", "a/1.log", "a/b/1.log", "c/1.log"},
expected: []string{"1.log", filepath.Join("a", "1.log"), filepath.Join("a", "b", "1.log"), filepath.Join("c", "1.log")},
},
}

Expand Down

0 comments on commit cedc744

Please sign in to comment.