/
config_topic_config.go
53 lines (43 loc) · 1.54 KB
/
config_topic_config.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
package minion
import "fmt"
const (
TopicGranularityTopic string = "topic"
TopicGranularityPartition string = "partition"
)
type TopicConfig struct {
// Granularity can be per topic or per partition. If you want to reduce the number of exported metric series and
// you aren't interested in per partition metrics you could choose "topic".
Granularity string `koanf:"granularity"`
// AllowedTopics are regex strings of topic names whose topic metrics that shall be exported.
AllowedTopics []string `koanf:"allowedTopics"`
// IgnoredTopics are regex strings of topic names that shall be ignored/skipped when exporting metrics. Ignored topics
// take precedence over allowed topics.
IgnoredTopics []string `koanf:"ignoredTopics"`
}
// Validate if provided TopicConfig is valid.
func (c *TopicConfig) Validate() error {
switch c.Granularity {
case TopicGranularityPartition, TopicGranularityTopic:
default:
return fmt.Errorf("given granularity '%v' is invalid", c.Granularity)
}
// Check whether each provided string is valid regex
for _, topic := range c.AllowedTopics {
_, err := compileRegex(topic)
if err != nil {
return fmt.Errorf("allowed topic string '%v' is not valid regex", topic)
}
}
for _, topic := range c.IgnoredTopics {
_, err := compileRegex(topic)
if err != nil {
return fmt.Errorf("ignored topic string '%v' is not valid regex", topic)
}
}
return nil
}
// SetDefaults for topic config
func (c *TopicConfig) SetDefaults() {
c.Granularity = TopicGranularityPartition
c.AllowedTopics = []string{"/.*/"}
}