Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/dns/dns.go
4095 views
1
package dns
2
3
import (
4
"errors"
5
"fmt"
6
"strings"
7
"time"
8
9
"github.com/grafana/agent/component"
10
"github.com/grafana/agent/component/discovery"
11
"github.com/prometheus/common/model"
12
"github.com/prometheus/prometheus/discovery/dns"
13
)
14
15
func init() {
16
component.Register(component.Registration{
17
Name: "discovery.dns",
18
Args: Arguments{},
19
Exports: discovery.Exports{},
20
21
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
22
return New(opts, args.(Arguments))
23
},
24
})
25
}
26
27
// Arguments configures the discovery.dns component.
28
type Arguments struct {
29
Names []string `river:"names,attr"`
30
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
31
Type string `river:"type,attr,optional"`
32
Port int `river:"port,attr,optional"`
33
}
34
35
var DefaultArguments = Arguments{
36
RefreshInterval: 30 * time.Second,
37
Type: "SRV",
38
}
39
40
// UnmarshalRiver implements river.Unmarshaler, applying defaults and
41
// validating the provided config.
42
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
43
*args = DefaultArguments
44
45
type arguments Arguments
46
if err := f((*arguments)(args)); err != nil {
47
return err
48
}
49
switch strings.ToUpper(args.Type) {
50
case "SRV":
51
case "A", "AAAA", "MX":
52
if args.Port == 0 {
53
return errors.New("a port is required in DNS-SD configs for all record types except SRV")
54
}
55
default:
56
return fmt.Errorf("invalid DNS-SD records type %s", args.Type)
57
}
58
return nil
59
}
60
61
// Convert converts Arguments to the upstream Prometheus SD type.
62
func (args Arguments) Convert() dns.SDConfig {
63
return dns.SDConfig{
64
Names: args.Names,
65
RefreshInterval: model.Duration(args.RefreshInterval),
66
Type: args.Type,
67
Port: args.Port,
68
}
69
}
70
71
// New returns a new instance of a discovery.dns component.
72
func New(opts component.Options, args Arguments) (component.Component, error) {
73
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
74
conf := args.(Arguments).Convert()
75
return dns.NewDiscovery(conf, opts.Logger), nil
76
})
77
}
78
79