Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/docker/docker.go
4096 views
1
// Package docker implements the discovery.docker component.
2
package docker
3
4
import (
5
"fmt"
6
"net/url"
7
"time"
8
9
"github.com/grafana/agent/component"
10
"github.com/grafana/agent/component/common/config"
11
"github.com/grafana/agent/component/discovery"
12
"github.com/grafana/agent/pkg/river"
13
"github.com/prometheus/common/model"
14
"github.com/prometheus/prometheus/discovery/moby"
15
)
16
17
func init() {
18
component.Register(component.Registration{
19
Name: "discovery.docker",
20
Args: Arguments{},
21
Exports: discovery.Exports{},
22
23
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
24
return New(opts, args.(Arguments))
25
},
26
})
27
}
28
29
// Arguments configures the discovery.docker component.
30
type Arguments struct {
31
Host string `river:"host,attr"`
32
Port int `river:"port,attr,optional"`
33
HostNetworkingHost string `river:"host_networking_host,attr,optional"`
34
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
35
Filters []Filter `river:"filter,block,optional"`
36
HTTPClientConfig config.HTTPClientConfig `river:",squash"`
37
}
38
39
// Filter is used to limit the discovery process to a subset of available
40
// resources.
41
type Filter struct {
42
Name string `river:"name,attr"`
43
Values []string `river:"values,attr"`
44
}
45
46
// Convert converts a Filter to the upstream Prometheus SD type.
47
func (f Filter) Convert() moby.Filter {
48
return moby.Filter{
49
Name: f.Name,
50
Values: f.Values,
51
}
52
}
53
54
// DefaultArguments holds default values for Arguments.
55
var DefaultArguments = Arguments{
56
Port: 80,
57
HostNetworkingHost: "localhost",
58
RefreshInterval: time.Minute,
59
HTTPClientConfig: config.DefaultHTTPClientConfig,
60
}
61
62
var _ river.Unmarshaler = (*Arguments)(nil)
63
64
// UnmarshalRiver implements river.Unmarshaler, applying defaults and
65
// validating the provided config.
66
func (args *Arguments) UnmarshalRiver(f func(interface{}) error) error {
67
*args = DefaultArguments
68
69
type arguments Arguments
70
if err := f((*arguments)(args)); err != nil {
71
return err
72
}
73
74
if args.Host == "" {
75
return fmt.Errorf("host attribute must not be empty")
76
} else if _, err := url.Parse(args.Host); err != nil {
77
return fmt.Errorf("parsing host attribute: %w", err)
78
}
79
80
if args.RefreshInterval <= 0 {
81
return fmt.Errorf("refresh_interval must be greater than 0")
82
}
83
84
return args.HTTPClientConfig.Validate()
85
}
86
87
// Convert converts Arguments to the upstream Prometheus SD type.
88
func (args Arguments) Convert() moby.DockerSDConfig {
89
filters := make([]moby.Filter, len(args.Filters))
90
for i, filter := range args.Filters {
91
filters[i] = filter.Convert()
92
}
93
94
return moby.DockerSDConfig{
95
HTTPClientConfig: *args.HTTPClientConfig.Convert(),
96
97
Host: args.Host,
98
Port: args.Port,
99
Filters: filters,
100
HostNetworkingHost: args.HostNetworkingHost,
101
102
RefreshInterval: model.Duration(args.RefreshInterval),
103
}
104
}
105
106
// New returns a new instance of a discovery.docker component.
107
func New(opts component.Options, args Arguments) (component.Component, error) {
108
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
109
conf := args.(Arguments).Convert()
110
return moby.NewDockerDiscovery(&conf, opts.Logger)
111
})
112
}
113
114