-
Notifications
You must be signed in to change notification settings - Fork 487
/
config.go
90 lines (74 loc) · 3.51 KB
/
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
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
package cluster
import (
"flag"
"reflect"
"strings"
"time"
"github.com/grafana/agent/pkg/metrics/cluster/client"
flagutil "github.com/grafana/agent/pkg/util"
util_log "github.com/grafana/agent/pkg/util/log"
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/ring"
)
// DefaultConfig provides default values for the config
var DefaultConfig = *flagutil.DefaultConfigFromFlags(&Config{}).(*Config)
// KVConfig wraps the kv.Config type to allow defining IsZero, which is required to make omitempty work when marshalling YAML.
type KVConfig struct {
kv.Config `yaml:",inline"`
}
func (k KVConfig) IsZero() bool {
return reflect.DeepEqual(k, KVConfig{}) || reflect.DeepEqual(k, DefaultConfig.KVStore)
}
// LifecyclerConfig wraps the ring.LifecyclerConfig type to allow defining IsZero, which is required to make omitempty work when marshalling YAML.
type LifecyclerConfig struct {
ring.LifecyclerConfig `yaml:",inline"`
}
func (l LifecyclerConfig) IsZero() bool {
return reflect.DeepEqual(l, LifecyclerConfig{}) || reflect.DeepEqual(l, DefaultConfig.Lifecycler)
}
// Config describes how to instantiate a scraping service Server instance.
type Config struct {
Enabled bool `yaml:"enabled,omitempty"`
ReshardInterval time.Duration `yaml:"reshard_interval,omitempty"`
ReshardTimeout time.Duration `yaml:"reshard_timeout,omitempty"`
ClusterReshardEventTimeout time.Duration `yaml:"cluster_reshard_event_timeout,omitempty"`
KVStore KVConfig `yaml:"kvstore,omitempty"`
Lifecycler LifecyclerConfig `yaml:"lifecycler,omitempty"`
DangerousAllowReadingFiles bool `yaml:"dangerous_allow_reading_files,omitempty"`
// TODO(rfratto): deprecate scraping_service_client in Agent and replace with this.
Client client.Config `yaml:"-"`
APIEnableGetConfiguration bool `yaml:"-"`
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultConfig
type plain Config
err := unmarshal((*plain)(c))
if err != nil {
return err
}
c.Lifecycler.RingConfig.ReplicationFactor = 1
return nil
}
func (c Config) IsZero() bool {
return reflect.DeepEqual(c, Config{}) || reflect.DeepEqual(c, DefaultConfig)
}
// RegisterFlags adds the flags required to config the Server to the given
// FlagSet.
func (c *Config) RegisterFlags(f *flag.FlagSet) {
c.RegisterFlagsWithPrefix("", f)
}
// RegisterFlagsWithPrefix adds the flags required to config this to the given
// FlagSet with a specified prefix.
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
f.BoolVar(&c.Enabled, prefix+"enabled", false, "enables the scraping service mode")
f.DurationVar(&c.ReshardInterval, prefix+"reshard-interval", time.Minute*1, "how often to manually refresh configuration")
f.DurationVar(&c.ReshardTimeout, prefix+"reshard-timeout", time.Second*30, "timeout for refreshing the configuration. Timeout of 0s disables timeout.")
f.DurationVar(&c.ClusterReshardEventTimeout, prefix+"cluster-reshard-event-timeout", time.Second*30, "timeout for the cluster reshard. Timeout of 0s disables timeout.")
c.KVStore.RegisterFlagsWithPrefix(prefix+"config-store.", "configurations/", f)
c.Lifecycler.RegisterFlagsWithPrefix(prefix, f, util_log.Logger)
// GRPCClientConfig.RegisterFlags expects that prefix does not end in a ".",
// unlike all other flags.
noDotPrefix := strings.TrimSuffix(prefix, ".")
c.Client.GRPCClientConfig.RegisterFlagsWithPrefix(noDotPrefix, f)
}