Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/logs/config.go
4094 views
1
package logs
2
3
import (
4
"flag"
5
"fmt"
6
"path/filepath"
7
8
"github.com/grafana/loki/clients/pkg/promtail/client"
9
"github.com/grafana/loki/clients/pkg/promtail/limit"
10
"github.com/grafana/loki/clients/pkg/promtail/positions"
11
"github.com/grafana/loki/clients/pkg/promtail/scrapeconfig"
12
"github.com/grafana/loki/clients/pkg/promtail/targets/file"
13
)
14
15
// Config controls the configuration of the Loki log scraper.
16
type Config struct {
17
PositionsDirectory string `yaml:"positions_directory,omitempty"`
18
Global GlobalConfig `yaml:"global,omitempty"`
19
Configs []*InstanceConfig `yaml:"configs,omitempty"`
20
}
21
22
// UnmarshalYAML implements yaml.Unmarshaler.
23
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
24
type config Config
25
err := unmarshal((*config)(c))
26
if err != nil {
27
return err
28
}
29
30
return nil
31
}
32
33
// ApplyDefaults applies defaults to the Config and ensures that it is valid.
34
//
35
// Validations:
36
//
37
// 1. No two InstanceConfigs may have the same name.
38
// 2. No two InstanceConfigs may have the same positions path.
39
// 3. No InstanceConfig may have an empty name.
40
// 4. If InstanceConfig positions path is empty, shared PositionsDirectory
41
// must not be empty.
42
//
43
// Defaults:
44
//
45
// 1. If a positions config is empty, it will be generated based on
46
// the InstanceConfig name and Config.PositionsDirectory.
47
// 2. If an InstanceConfigs's ClientConfigs is empty, it will be generated based on
48
// the Config.GlobalConfig.ClientConfigs.
49
func (c *Config) ApplyDefaults() error {
50
var (
51
names = map[string]struct{}{}
52
positions = map[string]string{} // positions file name -> config using it
53
)
54
55
for idx, ic := range c.Configs {
56
if ic.Name == "" {
57
return fmt.Errorf("Loki config index %d must have a name", idx)
58
}
59
if _, ok := names[ic.Name]; ok {
60
return fmt.Errorf("found two Loki configs with name %s", ic.Name)
61
}
62
names[ic.Name] = struct{}{}
63
64
if ic.PositionsConfig.PositionsFile == "" {
65
if c.PositionsDirectory == "" {
66
return fmt.Errorf("cannot generate Loki positions file path for %s because positions_directory is not configured", ic.Name)
67
}
68
ic.PositionsConfig.PositionsFile = filepath.Join(c.PositionsDirectory, ic.Name+".yml")
69
}
70
if orig, ok := positions[ic.PositionsConfig.PositionsFile]; ok {
71
return fmt.Errorf("Loki configs %s and %s must have different positions file paths", orig, ic.Name)
72
}
73
positions[ic.PositionsConfig.PositionsFile] = ic.Name
74
75
if len(ic.ClientConfigs) == 0 {
76
ic.ClientConfigs = c.Global.ClientConfigs
77
}
78
}
79
80
return nil
81
}
82
83
// InstanceConfig is an individual Promtail config.
84
type InstanceConfig struct {
85
Name string `yaml:"name,omitempty"`
86
87
ClientConfigs []client.Config `yaml:"clients,omitempty"`
88
PositionsConfig positions.Config `yaml:"positions,omitempty"`
89
ScrapeConfig []scrapeconfig.Config `yaml:"scrape_configs,omitempty"`
90
TargetConfig file.Config `yaml:"target_config,omitempty"`
91
LimitsConfig limit.Config `yaml:"limits_config,omitempty"`
92
}
93
94
func (c *InstanceConfig) Initialize() {
95
// Defaults for Promtail are hidden behind flags. Register flags to a fake flagset
96
// just to set the defaults in the configs.
97
fs := flag.NewFlagSet("temp", flag.PanicOnError)
98
c.PositionsConfig.RegisterFlags(fs)
99
c.TargetConfig.RegisterFlags(fs)
100
101
// Blank out the positions file since we set our own default for that.
102
c.PositionsConfig.PositionsFile = ""
103
}
104
105
// UnmarshalYAML implements yaml.Unmarshaler.
106
func (c *InstanceConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
107
c.Initialize()
108
type instanceConfig InstanceConfig
109
return unmarshal((*instanceConfig)(c))
110
}
111
112