-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
// FilterList holds a map of filters, i.e. regular expressions. | ||
// These filters are used to identify which Consul metrics can be transmitted to HCP. | ||
type FilterList struct { | ||
filters map[string]*regexp.Regexp | ||
} | ||
|
||
// NewFilterList returns a FilterList which holds valid regex | ||
// used to filter metrics. It will not fail if invalid REGEX is given, but returns a list of errors. | ||
func NewFilterList(filters []string) (*FilterList, error) { | ||
var err error | ||
f := &FilterList{} | ||
compiledList := make(map[string]*regexp.Regexp, len(filters)) | ||
for idx, filter := range filters { | ||
re, err := regexp.Compile(filter) | ||
if err != nil { | ||
multierror.Append(err, fmt.Errorf("compilation of filter at index %d failed: %w", idx, err)) | ||
} | ||
compiledList[filter] = re | ||
} | ||
f.filters = compiledList | ||
return f, err | ||
} | ||
|
||
// Match returns true if the metric name matches a REGEX in the allowed metric filters. | ||
func (fl *FilterList) Match(name string) bool { | ||
for _, re := range fl.filters { | ||
if re.Match([]byte(name)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,39 @@ | ||
package telemetry | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFilter(t *testing.T) { | ||
for name, tc := range map[string]struct { | ||
filters []string | ||
wantMatch bool | ||
wantErr string | ||
}{ | ||
"badFilterRegex": { | ||
filters: []string{"(*LF)"}, | ||
wantErr: "compilation of filter at index 0 failed", | ||
}, | ||
"matchFound": { | ||
filters: []string{"raft.*"}, | ||
wantMatch: true, | ||
}, | ||
"matchNotFound": { | ||
filters: []string{"mem.heap_size"}, | ||
wantMatch: false, | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
f, err := NewFilterList(tc.filters) | ||
if tc.wantErr != "" { | ||
require.Contains(t, err.Error(), tc.wantErr) | ||
|
||
} else { | ||
m := f.Match("consul.raft.peers") | ||
require.Equal(t, tc.wantMatch, m) | ||
} | ||
}) | ||
} | ||
} |
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