github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/clients/pkg/promtail/config/config.go (about) 1 package config 2 3 import ( 4 "flag" 5 "fmt" 6 7 "github.com/go-kit/log" 8 "github.com/go-kit/log/level" 9 dskit_flagext "github.com/grafana/dskit/flagext" 10 11 yaml "gopkg.in/yaml.v2" 12 13 "github.com/grafana/loki/clients/pkg/promtail/client" 14 "github.com/grafana/loki/clients/pkg/promtail/limit" 15 "github.com/grafana/loki/clients/pkg/promtail/positions" 16 "github.com/grafana/loki/clients/pkg/promtail/scrapeconfig" 17 "github.com/grafana/loki/clients/pkg/promtail/server" 18 "github.com/grafana/loki/clients/pkg/promtail/targets/file" 19 20 "github.com/grafana/loki/pkg/util/flagext" 21 ) 22 23 // Options contains cross-cutting promtail configurations 24 type Options struct { 25 StreamLagLabels dskit_flagext.StringSliceCSV `yaml:"stream_lag_labels,omitempty"` 26 } 27 28 // Config for promtail, describing what files to watch. 29 type Config struct { 30 ServerConfig server.Config `yaml:"server,omitempty"` 31 // deprecated use ClientConfigs instead 32 ClientConfig client.Config `yaml:"client,omitempty"` 33 ClientConfigs []client.Config `yaml:"clients,omitempty"` 34 PositionsConfig positions.Config `yaml:"positions,omitempty"` 35 ScrapeConfig []scrapeconfig.Config `yaml:"scrape_configs,omitempty"` 36 TargetConfig file.Config `yaml:"target_config,omitempty"` 37 LimitsConfig limit.Config `yaml:"limits_config,omitempty"` 38 Options Options `yaml:"options,omitempty"` 39 } 40 41 // RegisterFlags with prefix registers flags where every name is prefixed by 42 // prefix. If prefix is a non-empty string, prefix should end with a period. 43 func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) { 44 c.ServerConfig.RegisterFlagsWithPrefix(prefix, f) 45 c.ClientConfig.RegisterFlagsWithPrefix(prefix, f) 46 c.PositionsConfig.RegisterFlagsWithPrefix(prefix, f) 47 c.TargetConfig.RegisterFlagsWithPrefix(prefix, f) 48 c.LimitsConfig.RegisterFlagsWithPrefix(prefix, f) 49 } 50 51 // RegisterFlags registers flags. 52 func (c *Config) RegisterFlags(f *flag.FlagSet) { 53 c.RegisterFlagsWithPrefix("", f) 54 } 55 56 func (c Config) String() string { 57 b, err := yaml.Marshal(c) 58 if err != nil { 59 return fmt.Sprintf("<error creating config string: %s>", err) 60 } 61 return string(b) 62 } 63 64 func (c *Config) Setup(l log.Logger) { 65 if c.ClientConfig.URL.URL != nil { 66 level.Warn(l).Log("msg", "use of CLI client.* and config file Client block are both deprecated in favour of the config file Clients block and will be removed in a future release") 67 // if a single client config is used we add it to the multiple client config for backward compatibility 68 c.ClientConfigs = append(c.ClientConfigs, c.ClientConfig) 69 } 70 71 // This is a bit crude but if the Loki Push API target is specified, 72 // force the log level to match the promtail log level 73 for i := range c.ScrapeConfig { 74 if c.ScrapeConfig[i].PushConfig != nil { 75 c.ScrapeConfig[i].PushConfig.Server.LogLevel = c.ServerConfig.LogLevel 76 c.ScrapeConfig[i].PushConfig.Server.LogFormat = c.ServerConfig.LogFormat 77 } 78 } 79 80 // Merge the provided external labels from the single client config/command line with each client config from 81 // `clients`. This is done to allow --client.external-labels=key=value passed at command line to apply to all clients 82 // The order here is specified to allow the yaml to override the command line flag if there are any labels 83 // which exist in both the command line arguments as well as the yaml, and while this is 84 // not typically the order of precedence, the assumption here is someone providing a specific config in 85 // yaml is doing so explicitly to make a key specific to a client. 86 if len(c.ClientConfig.ExternalLabels.LabelSet) > 0 { 87 for i := range c.ClientConfigs { 88 c.ClientConfigs[i].ExternalLabels = flagext.LabelSet{LabelSet: c.ClientConfig.ExternalLabels.LabelSet.Merge(c.ClientConfigs[i].ExternalLabels.LabelSet)} 89 } 90 } 91 }