/
filter_processor.go
78 lines (64 loc) · 2.15 KB
/
filter_processor.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package processors
import (
"fmt"
"regexp"
"go.opentelemetry.io/collector/component"
"github.com/hashicorp/consul-telemetry-collector/internal/hcp"
"github.com/hashicorp/go-hclog"
)
const filterProcessorName = "filter"
// FilterProcessorID is the component id of the filter processor.
var FilterProcessorID component.ID = component.NewID(filterProcessorName)
const (
regexpMatchType = "regexp"
)
// FilterProcessorConfig creates the filter processor configuration.
type FilterProcessorConfig struct {
Metrics *MetricFilters `mapstructure:"metrics"`
}
// MetricFilters is the filter configuration for metrics.
type MetricFilters struct {
Include *MatchProperties `mapstructure:"include"`
}
// MatchProperties specifies how to match against a set of metric names for filtering signals.
type MatchProperties struct {
MatchType string `mapstructure:"match_type"`
MetricNames []string `mapstructure:"metric_names"`
}
// FilterProcessorCfg generates the config for a filter processor.
func FilterProcessorCfg(client hcp.TelemetryClient) *FilterProcessorConfig {
usableFilters := []string{}
filters, err := client.MetricFilters()
logger := hclog.Default().Named("config/helpers")
if err != nil {
// log failure here, but it's not fatal because the gateway should also filter metrics
logger.Warn("failed to retrieve metric filters from HCP", "error", err)
return &FilterProcessorConfig{}
}
for _, filter := range filters {
if err := validateFilter(filter); err != nil {
// log failure here, but it's not fatal because the gateway should also filter metrics
logger.Warn("failed to validate filter", "filter", filter, "error", err)
continue
}
usableFilters = append(usableFilters, filter)
}
logger.Info(fmt.Sprintf("created %d usable filters for the HCP pipeline", len(usableFilters)))
cfg := FilterProcessorConfig{
Metrics: &MetricFilters{
Include: &MatchProperties{
MatchType: regexpMatchType,
MetricNames: usableFilters,
},
},
}
return &cfg
}
func validateFilter(s string) error {
if _, err := regexp.Compile(s); err != nil {
return err
}
return nil
}