-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathmatcher.go
209 lines (177 loc) · 5.79 KB
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package matcher // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/matcher"
import (
"errors"
"fmt"
"regexp"
"time"
"go.opentelemetry.io/collector/featuregate"
"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"
)
const (
sortTypeNumeric = "numeric"
sortTypeTimestamp = "timestamp"
sortTypeAlphabetical = "alphabetical"
sortTypeMtime = "mtime"
)
const (
defaultOrderingCriteriaTopN = 1
)
var mtimeSortTypeFeatureGate = featuregate.GlobalRegistry().MustRegister(
"filelog.mtimeSortType",
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"),
)
type Criteria struct {
Include []string `mapstructure:"include,omitempty"`
Exclude []string `mapstructure:"exclude,omitempty"`
// ExcludeOlderThan allows excluding files whose modification time is older
// than the specified age.
ExcludeOlderThan time.Duration `mapstructure:"exclude_older_than"`
OrderingCriteria OrderingCriteria `mapstructure:"ordering_criteria,omitempty"`
}
type OrderingCriteria struct {
Regex string `mapstructure:"regex,omitempty"`
TopN int `mapstructure:"top_n,omitempty"`
SortBy []Sort `mapstructure:"sort_by,omitempty"`
GroupBy string `mapstructure:"group_by,omitempty"`
}
type Sort struct {
SortType string `mapstructure:"sort_type,omitempty"`
RegexKey string `mapstructure:"regex_key,omitempty"`
Ascending bool `mapstructure:"ascending,omitempty"`
// Timestamp only
Layout string `mapstructure:"layout,omitempty"`
Location string `mapstructure:"location,omitempty"`
}
func New(c Criteria) (*Matcher, error) {
if len(c.Include) == 0 {
return nil, fmt.Errorf("'include' must be specified")
}
if err := finder.Validate(c.Include); err != nil {
return nil, fmt.Errorf("include: %w", err)
}
if err := finder.Validate(c.Exclude); err != nil {
return nil, fmt.Errorf("exclude: %w", err)
}
m := &Matcher{
include: c.Include,
exclude: c.Exclude,
}
if c.ExcludeOlderThan != 0 {
m.filterOpts = append(m.filterOpts, filter.ExcludeOlderThan(c.ExcludeOlderThan))
}
if c.OrderingCriteria.GroupBy != "" {
r, err := regexp.Compile(c.OrderingCriteria.GroupBy)
if err != nil {
return nil, fmt.Errorf("compile group_by regex: %w", err)
}
m.groupBy = r
}
if len(c.OrderingCriteria.SortBy) == 0 {
return m, nil
}
if c.OrderingCriteria.TopN < 0 {
return nil, fmt.Errorf("'top_n' must be a positive integer")
}
if c.OrderingCriteria.TopN == 0 {
c.OrderingCriteria.TopN = defaultOrderingCriteriaTopN
}
if orderingCriteriaNeedsRegex(c.OrderingCriteria.SortBy) {
if c.OrderingCriteria.Regex == "" {
return nil, fmt.Errorf("'regex' must be specified when 'sort_by' is specified")
}
var err error
regex, err := regexp.Compile(c.OrderingCriteria.Regex)
if err != nil {
return nil, fmt.Errorf("compile regex: %w", err)
}
m.regex = regex
}
for _, sc := range c.OrderingCriteria.SortBy {
switch sc.SortType {
case sortTypeNumeric:
f, err := filter.SortNumeric(sc.RegexKey, sc.Ascending)
if err != nil {
return nil, fmt.Errorf("numeric sort: %w", err)
}
m.filterOpts = append(m.filterOpts, f)
case sortTypeAlphabetical:
f, err := filter.SortAlphabetical(sc.RegexKey, sc.Ascending)
if err != nil {
return nil, fmt.Errorf("alphabetical sort: %w", err)
}
m.filterOpts = append(m.filterOpts, f)
case sortTypeTimestamp:
f, err := filter.SortTemporal(sc.RegexKey, sc.Ascending, sc.Layout, sc.Location)
if err != nil {
return nil, fmt.Errorf("timestamp sort: %w", err)
}
m.filterOpts = append(m.filterOpts, f)
case sortTypeMtime:
if !mtimeSortTypeFeatureGate.IsEnabled() {
return nil, fmt.Errorf("the %q feature gate must be enabled to use %q sort type", mtimeSortTypeFeatureGate.ID(), sortTypeMtime)
}
m.filterOpts = append(m.filterOpts, filter.SortMtime(sc.Ascending))
default:
return nil, fmt.Errorf("'sort_type' must be specified")
}
}
m.filterOpts = append(m.filterOpts, filter.TopNOption(c.OrderingCriteria.TopN))
return m, nil
}
// orderingCriteriaNeedsRegex returns true if any of the sort options require a regex to be set.
func orderingCriteriaNeedsRegex(sorts []Sort) bool {
for _, s := range sorts {
switch s.SortType {
case sortTypeNumeric, sortTypeAlphabetical, sortTypeTimestamp:
return true
}
}
return false
}
type Matcher struct {
include []string
exclude []string
regex *regexp.Regexp
filterOpts []filter.Option
groupBy *regexp.Regexp
}
// MatchFiles gets a list of paths given an array of glob patterns to include and exclude
func (m Matcher) MatchFiles() ([]string, error) {
var errs error
files, err := finder.FindFiles(m.include, m.exclude)
if err != nil {
errs = errors.Join(errs, err)
}
if len(files) == 0 {
return files, errors.Join(fmt.Errorf("no files match the configured criteria"), errs)
}
if len(m.filterOpts) == 0 {
return files, errs
}
groups := make(map[string][]string)
if m.groupBy != nil {
for _, f := range files {
matches := m.groupBy.FindStringSubmatch(f)
if len(matches) > 1 {
group := matches[1]
groups[group] = append(groups[group], f)
}
}
} else {
groups["1"] = files
}
var result []string
for _, groupedFiles := range groups {
groupResult, err := filter.Filter(groupedFiles, m.regex, m.filterOpts...)
if len(groupResult) == 0 {
return groupResult, errors.Join(err, errs)
}
result = append(result, groupResult...)
}
return result, errs
}