Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/consul/consul.go
4096 views
1
package consul
2
3
import (
4
"fmt"
5
"time"
6
7
"github.com/grafana/agent/component"
8
"github.com/grafana/agent/component/common/config"
9
"github.com/grafana/agent/component/discovery"
10
"github.com/grafana/agent/pkg/river/rivertypes"
11
config_util "github.com/prometheus/common/config"
12
"github.com/prometheus/common/model"
13
prom_discovery "github.com/prometheus/prometheus/discovery/consul"
14
)
15
16
func init() {
17
component.Register(component.Registration{
18
Name: "discovery.consul",
19
Args: Arguments{},
20
Exports: discovery.Exports{},
21
22
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
23
return New(opts, args.(Arguments))
24
},
25
})
26
}
27
28
type Arguments struct {
29
Server string `river:"server,attr,optional"`
30
Token rivertypes.Secret `river:"token,attr,optional"`
31
Datacenter string `river:"datacenter,attr,optional"`
32
Namespace string `river:"namespace,attr,optional"`
33
Partition string `river:"partition,attr,optional"`
34
TagSeparator string `river:"tag_separator,attr,optional"`
35
Scheme string `river:"scheme,attr,optional"`
36
Username string `river:"username,attr,optional"`
37
Password rivertypes.Secret `river:"password,attr,optional"`
38
AllowStale bool `river:"allow_stale,attr,optional"`
39
Services []string `river:"services,attr,optional"`
40
ServiceTags []string `river:"tags,attr,optional"`
41
NodeMeta map[string]string `river:"node_meta,attr,optional"`
42
43
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
44
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
45
}
46
47
var DefaultArguments = Arguments{
48
Server: "localhost:8500",
49
TagSeparator: ",",
50
Scheme: "http",
51
AllowStale: true,
52
RefreshInterval: 30 * time.Second,
53
}
54
55
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
56
*args = DefaultArguments
57
type arguments Arguments
58
err := f((*arguments)(args))
59
if err != nil {
60
return err
61
}
62
63
if args.RefreshInterval <= 0 {
64
return fmt.Errorf("refresh_interval must be greater than 0")
65
}
66
67
return args.HTTPClientConfig.Validate()
68
}
69
70
func (args *Arguments) Convert() *prom_discovery.SDConfig {
71
httpClient := &args.HTTPClientConfig
72
73
return &prom_discovery.SDConfig{
74
RefreshInterval: model.Duration(args.RefreshInterval),
75
HTTPClientConfig: *httpClient.Convert(),
76
Server: args.Server,
77
Token: config_util.Secret(args.Token),
78
Datacenter: args.Datacenter,
79
Namespace: args.Namespace,
80
Partition: args.Partition,
81
TagSeparator: args.TagSeparator,
82
Scheme: args.Scheme,
83
Username: args.Username,
84
Password: config_util.Secret(args.Password),
85
AllowStale: args.AllowStale,
86
Services: args.Services,
87
ServiceTags: args.ServiceTags,
88
NodeMeta: args.NodeMeta,
89
}
90
}
91
92
func New(opts component.Options, args Arguments) (component.Component, error) {
93
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
94
newArgs := args.(Arguments)
95
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
96
})
97
}
98
99