-
Notifications
You must be signed in to change notification settings - Fork 482
/
hetzner.go
72 lines (61 loc) · 2.11 KB
/
hetzner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package hetzner
import (
"fmt"
"time"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/common/config"
"github.com/grafana/agent/component/discovery"
"github.com/prometheus/common/model"
prom_discovery "github.com/prometheus/prometheus/discovery/hetzner"
)
func init() {
component.Register(component.Registration{
Name: "discovery.hetzner",
Args: Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
},
})
}
type Arguments struct {
Role string `river:"role,attr"`
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
Port int `river:"port,attr,optional"`
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
}
var DefaultArguments = Arguments{
Port: 80,
RefreshInterval: 60 * time.Second,
HTTPClientConfig: config.DefaultHTTPClientConfig,
}
// SetToDefault implements river.Defaulter.
func (args *Arguments) SetToDefault() {
*args = DefaultArguments
}
// Validate implements river.Validator.
func (args *Arguments) Validate() error {
switch args.Role {
case string(prom_discovery.HetznerRoleRobot), string(prom_discovery.HetznerRoleHcloud):
default:
return fmt.Errorf("unknown role %s, must be one of robot or hcloud", args.Role)
}
return args.HTTPClientConfig.Validate()
}
func (args *Arguments) Convert() *prom_discovery.SDConfig {
httpClient := &args.HTTPClientConfig
cfg := &prom_discovery.SDConfig{
RefreshInterval: model.Duration(args.RefreshInterval),
Port: args.Port,
HTTPClientConfig: *httpClient.Convert(),
Role: prom_discovery.Role(args.Role),
}
return cfg
}
// New returns a new instance of a discovery.hetzner component.
func New(opts component.Options, args Arguments) (*discovery.Component, error) {
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
newArgs := args.(Arguments)
return prom_discovery.NewDiscovery(newArgs.Convert(), opts.Logger)
})
}