Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/config/agentmanagement_remote_config.go
4094 views
1
package config
2
3
import (
4
"github.com/grafana/agent/pkg/integrations"
5
"github.com/grafana/agent/pkg/logs"
6
"github.com/grafana/agent/pkg/metrics/instance"
7
"github.com/grafana/loki/clients/pkg/promtail/scrapeconfig"
8
pc "github.com/prometheus/prometheus/config"
9
"gopkg.in/yaml.v2"
10
)
11
12
type (
13
RemoteConfig struct {
14
BaseConfig BaseConfigContent `json:"base_config" yaml:"base_config"`
15
Snippets []Snippet `json:"snippets" yaml:"snippets"`
16
}
17
18
// BaseConfigContent is the content of a base config
19
BaseConfigContent string
20
21
// Snippet is a snippet of configuration returned by the config API.
22
Snippet struct {
23
// Config is the snippet of config to be included.
24
Config string `json:"config" yaml:"config"`
25
}
26
27
// SnippetContent defines the internal structure of a snippet configuration.
28
SnippetContent struct {
29
// MetricsScrapeConfigs is a YAML containing list of metrics scrape configs.
30
MetricsScrapeConfigs []*pc.ScrapeConfig `yaml:"metrics_scrape_configs,omitempty"`
31
32
// LogsScrapeConfigs is a YAML containing list of logs scrape configs.
33
LogsScrapeConfigs []scrapeconfig.Config `yaml:"logs_scrape_configs,omitempty"`
34
35
// IntegrationConfigs is a YAML containing list of integrations.
36
IntegrationConfigs integrations.ManagerConfig `yaml:"integration_configs,omitempty"`
37
}
38
)
39
40
func NewRemoteConfig(buf []byte) (*RemoteConfig, error) {
41
rc := &RemoteConfig{}
42
err := yaml.Unmarshal(buf, rc)
43
if err != nil {
44
return nil, err
45
}
46
return rc, nil
47
}
48
49
// BuildAgentConfig builds an agent configuration from a base config and a list of snippets
50
func (rc *RemoteConfig) BuildAgentConfig() (*Config, error) {
51
c := DefaultConfig()
52
err := yaml.Unmarshal([]byte(rc.BaseConfig), &c)
53
if err != nil {
54
return nil, err
55
}
56
57
// For now Agent Management only supports integrations v1
58
if err := c.Integrations.setVersion(integrationsVersion1); err != nil {
59
return nil, err
60
}
61
62
err = appendSnippets(&c, rc.Snippets)
63
if err != nil {
64
return nil, err
65
}
66
return &c, nil
67
}
68
69
func appendSnippets(c *Config, snippets []Snippet) error {
70
metricsConfigs := instance.DefaultConfig
71
metricsConfigs.Name = "snippets"
72
logsConfigs := logs.InstanceConfig{
73
Name: "snippets",
74
ScrapeConfig: []scrapeconfig.Config{},
75
}
76
logsConfigs.Initialize()
77
integrationConfigs := integrations.DefaultManagerConfig()
78
79
for _, snippet := range snippets {
80
var snippetContent SnippetContent
81
err := yaml.Unmarshal([]byte(snippet.Config), &snippetContent)
82
if err != nil {
83
return err
84
}
85
metricsConfigs.ScrapeConfigs = append(metricsConfigs.ScrapeConfigs, snippetContent.MetricsScrapeConfigs...)
86
logsConfigs.ScrapeConfig = append(logsConfigs.ScrapeConfig, snippetContent.LogsScrapeConfigs...)
87
integrationConfigs.Integrations = append(integrationConfigs.Integrations, snippetContent.IntegrationConfigs.Integrations...)
88
}
89
if len(metricsConfigs.ScrapeConfigs) > 0 {
90
c.Metrics.Configs = append(c.Metrics.Configs, metricsConfigs)
91
}
92
93
if len(logsConfigs.ScrapeConfig) > 0 {
94
// rc.Config.Logs is initialized as nil, so we need to check if it's nil before appending
95
if c.Logs == nil {
96
c.Logs = &logs.Config{
97
Configs: []*logs.InstanceConfig{},
98
}
99
}
100
c.Logs.Configs = append(c.Logs.Configs, &logsConfigs)
101
}
102
103
c.Integrations.configV1.Integrations = append(c.Integrations.configV1.Integrations, integrationConfigs.Integrations...)
104
return nil
105
}
106
107