-
Notifications
You must be signed in to change notification settings - Fork 487
/
nerve.go
66 lines (55 loc) · 1.74 KB
/
nerve.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
package nerve
import (
"fmt"
"time"
"github.com/grafana/agent/component"
"github.com/grafana/agent/component/discovery"
"github.com/prometheus/common/model"
prom_discovery "github.com/prometheus/prometheus/discovery/zookeeper"
)
func init() {
component.Register(component.Registration{
Name: "discovery.nerve",
Args: Arguments{},
Exports: discovery.Exports{},
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
return New(opts, args.(Arguments))
},
})
}
// Arguments configure the discovery.nerve component.
type Arguments struct {
Servers []string `river:"servers,attr"`
Paths []string `river:"paths,attr"`
Timeout time.Duration `river:"timeout,attr,optional"`
}
// DefaultArguments is used to initialize default values for Arguments.
var DefaultArguments = Arguments{
Timeout: 10 * time.Second,
}
// SetToDefault implements river.Defaulter.
func (args *Arguments) SetToDefault() {
*args = DefaultArguments
}
// Validate implements river.Validator.
func (args *Arguments) Validate() error {
if args.Timeout <= 0 {
return fmt.Errorf("timeout must be greater than 0")
}
return nil
}
// Convert returns the upstream configuration struct.
func (args *Arguments) Convert() *prom_discovery.NerveSDConfig {
return &prom_discovery.NerveSDConfig{
Servers: args.Servers,
Paths: args.Paths,
Timeout: model.Duration(args.Timeout),
}
}
// New returns a new instance of a discovery.nerve 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.NewNerveDiscovery(newArgs.Convert(), opts.Logger)
})
}