Path: blob/main/component/common/loki/client/config.go
4096 views
package client12// This code is copied from Promtail. The client package is used to configure3// and run the clients that can send log entries to a Loki instance.45import (6"flag"7"time"89"github.com/grafana/dskit/backoff"10"github.com/grafana/dskit/flagext"11"github.com/prometheus/common/config"1213lokiflag "github.com/grafana/loki/pkg/util/flagext"14)1516// NOTE the helm chart for promtail and fluent-bit also have defaults for these values, please update to match if you make changes here.17const (18BatchWait = 1 * time.Second19BatchSize int = 1024 * 102420MinBackoff = 500 * time.Millisecond21MaxBackoff = 5 * time.Minute22MaxRetries int = 1023Timeout = 10 * time.Second24)2526// Config describes configuration for an HTTP pusher client.27type Config struct {28Name string `yaml:"name,omitempty"`29URL flagext.URLValue30BatchWait time.Duration31BatchSize int3233Client config.HTTPClientConfig `yaml:",inline"`3435BackoffConfig backoff.Config `yaml:"backoff_config"`36// The labels to add to any time series or alerts when communicating with loki37ExternalLabels lokiflag.LabelSet `yaml:"external_labels,omitempty"`38Timeout time.Duration `yaml:"timeout"`3940// The tenant ID to use when pushing logs to Loki (empty string means41// single tenant mode)42TenantID string `yaml:"tenant_id"`4344// deprecated use StreamLagLabels from config.Config instead45StreamLagLabels flagext.StringSliceCSV `yaml:"stream_lag_labels"`46}4748// RegisterFlags with prefix registers flags where every name is prefixed by49// prefix. If prefix is a non-empty string, prefix should end with a period.50func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {51f.Var(&c.URL, prefix+"client.url", "URL of log server (deprecated).")52f.DurationVar(&c.BatchWait, prefix+"client.batch-wait", BatchWait, "Maximum wait period before sending batch (deprecated).")53f.IntVar(&c.BatchSize, prefix+"client.batch-size-bytes", BatchSize, "Maximum batch size to accrue before sending (deprecated).")54// Default backoff schedule: 0.5s, 1s, 2s, 4s, 8s, 16s, 32s, 64s, 128s, 256s(4.267m) For a total time of 511.5s(8.5m) before logs are lost55f.IntVar(&c.BackoffConfig.MaxRetries, prefix+"client.max-retries", MaxRetries, "Maximum number of retires when sending batches (deprecated).")56f.DurationVar(&c.BackoffConfig.MinBackoff, prefix+"client.min-backoff", MinBackoff, "Initial backoff time between retries (deprecated).")57f.DurationVar(&c.BackoffConfig.MaxBackoff, prefix+"client.max-backoff", MaxBackoff, "Maximum backoff time between retries (deprecated).")58f.DurationVar(&c.Timeout, prefix+"client.timeout", Timeout, "Maximum time to wait for server to respond to a request (deprecated).")59f.Var(&c.ExternalLabels, prefix+"client.external-labels", "list of external labels to add to each log (e.g: --client.external-labels=lb1=v1,lb2=v2) (deprecated).")6061f.StringVar(&c.TenantID, prefix+"client.tenant-id", "", "Tenant ID to use when pushing logs to Loki (deprecated).")62}6364// RegisterFlags registers flags.65func (c *Config) RegisterFlags(flags *flag.FlagSet) {66c.RegisterFlagsWithPrefix("", flags)67}6869// UnmarshalYAML implement Yaml Unmarshaler70func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {71type raw Config72var cfg raw73if c.URL.URL != nil {74// we used flags to set that value, which already has sane default.75cfg = raw(*c)76} else {77// force sane defaults.78cfg = raw{79BackoffConfig: backoff.Config{80MaxBackoff: MaxBackoff,81MaxRetries: MaxRetries,82MinBackoff: MinBackoff,83},84BatchSize: BatchSize,85BatchWait: BatchWait,86Timeout: Timeout,87}88}8990if err := unmarshal(&cfg); err != nil {91return err92}9394*c = Config(cfg)95return nil96}979899