Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/common/loki/client/config.go
4096 views
1
package client
2
3
// This code is copied from Promtail. The client package is used to configure
4
// and run the clients that can send log entries to a Loki instance.
5
6
import (
7
"flag"
8
"time"
9
10
"github.com/grafana/dskit/backoff"
11
"github.com/grafana/dskit/flagext"
12
"github.com/prometheus/common/config"
13
14
lokiflag "github.com/grafana/loki/pkg/util/flagext"
15
)
16
17
// NOTE the helm chart for promtail and fluent-bit also have defaults for these values, please update to match if you make changes here.
18
const (
19
BatchWait = 1 * time.Second
20
BatchSize int = 1024 * 1024
21
MinBackoff = 500 * time.Millisecond
22
MaxBackoff = 5 * time.Minute
23
MaxRetries int = 10
24
Timeout = 10 * time.Second
25
)
26
27
// Config describes configuration for an HTTP pusher client.
28
type Config struct {
29
Name string `yaml:"name,omitempty"`
30
URL flagext.URLValue
31
BatchWait time.Duration
32
BatchSize int
33
34
Client config.HTTPClientConfig `yaml:",inline"`
35
36
BackoffConfig backoff.Config `yaml:"backoff_config"`
37
// The labels to add to any time series or alerts when communicating with loki
38
ExternalLabels lokiflag.LabelSet `yaml:"external_labels,omitempty"`
39
Timeout time.Duration `yaml:"timeout"`
40
41
// The tenant ID to use when pushing logs to Loki (empty string means
42
// single tenant mode)
43
TenantID string `yaml:"tenant_id"`
44
45
// deprecated use StreamLagLabels from config.Config instead
46
StreamLagLabels flagext.StringSliceCSV `yaml:"stream_lag_labels"`
47
}
48
49
// RegisterFlags with prefix registers flags where every name is prefixed by
50
// prefix. If prefix is a non-empty string, prefix should end with a period.
51
func (c *Config) RegisterFlagsWithPrefix(prefix string, f *flag.FlagSet) {
52
f.Var(&c.URL, prefix+"client.url", "URL of log server (deprecated).")
53
f.DurationVar(&c.BatchWait, prefix+"client.batch-wait", BatchWait, "Maximum wait period before sending batch (deprecated).")
54
f.IntVar(&c.BatchSize, prefix+"client.batch-size-bytes", BatchSize, "Maximum batch size to accrue before sending (deprecated).")
55
// 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 lost
56
f.IntVar(&c.BackoffConfig.MaxRetries, prefix+"client.max-retries", MaxRetries, "Maximum number of retires when sending batches (deprecated).")
57
f.DurationVar(&c.BackoffConfig.MinBackoff, prefix+"client.min-backoff", MinBackoff, "Initial backoff time between retries (deprecated).")
58
f.DurationVar(&c.BackoffConfig.MaxBackoff, prefix+"client.max-backoff", MaxBackoff, "Maximum backoff time between retries (deprecated).")
59
f.DurationVar(&c.Timeout, prefix+"client.timeout", Timeout, "Maximum time to wait for server to respond to a request (deprecated).")
60
f.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).")
61
62
f.StringVar(&c.TenantID, prefix+"client.tenant-id", "", "Tenant ID to use when pushing logs to Loki (deprecated).")
63
}
64
65
// RegisterFlags registers flags.
66
func (c *Config) RegisterFlags(flags *flag.FlagSet) {
67
c.RegisterFlagsWithPrefix("", flags)
68
}
69
70
// UnmarshalYAML implement Yaml Unmarshaler
71
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
72
type raw Config
73
var cfg raw
74
if c.URL.URL != nil {
75
// we used flags to set that value, which already has sane default.
76
cfg = raw(*c)
77
} else {
78
// force sane defaults.
79
cfg = raw{
80
BackoffConfig: backoff.Config{
81
MaxBackoff: MaxBackoff,
82
MaxRetries: MaxRetries,
83
MinBackoff: MinBackoff,
84
},
85
BatchSize: BatchSize,
86
BatchWait: BatchWait,
87
Timeout: Timeout,
88
}
89
}
90
91
if err := unmarshal(&cfg); err != nil {
92
return err
93
}
94
95
*c = Config(cfg)
96
return nil
97
}
98
99