Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/aws/ec2.go
4095 views
1
package aws
2
3
import (
4
"errors"
5
"time"
6
7
"github.com/aws/aws-sdk-go/aws/ec2metadata"
8
"github.com/aws/aws-sdk-go/aws/session"
9
"github.com/grafana/agent/component"
10
"github.com/grafana/agent/component/discovery"
11
"github.com/grafana/agent/pkg/river/rivertypes"
12
promcfg "github.com/prometheus/common/config"
13
"github.com/prometheus/common/model"
14
promaws "github.com/prometheus/prometheus/discovery/aws"
15
)
16
17
func init() {
18
component.Register(component.Registration{
19
Name: "discovery.ec2",
20
Args: EC2Arguments{},
21
Exports: discovery.Exports{},
22
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
23
return NewEC2(opts, args.(EC2Arguments))
24
},
25
})
26
}
27
28
// EC2Filter is the configuration for filtering EC2 instances.
29
type EC2Filter struct {
30
Name string `river:"name,attr"`
31
Values []string `river:"values,attr"`
32
}
33
34
// EC2Arguments is the configuration for EC2 based service discovery.
35
type EC2Arguments struct {
36
Endpoint string `river:"endpoint,attr,optional"`
37
Region string `river:"region,attr,optional"`
38
AccessKey string `river:"access_key,attr,optional"`
39
SecretKey rivertypes.Secret `river:"secret_key,attr,optional"`
40
Profile string `river:"profile,attr,optional"`
41
RoleARN string `river:"role_arn,attr,optional"`
42
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
43
Port int `river:"port,attr,optional"`
44
Filters []*EC2Filter `river:"filter,block,optional"`
45
}
46
47
func (args EC2Arguments) Convert() *promaws.EC2SDConfig {
48
cfg := &promaws.EC2SDConfig{
49
Endpoint: args.Endpoint,
50
Region: args.Region,
51
AccessKey: args.AccessKey,
52
SecretKey: promcfg.Secret(args.SecretKey),
53
Profile: args.Profile,
54
RoleARN: args.RoleARN,
55
RefreshInterval: model.Duration(args.RefreshInterval),
56
Port: args.Port,
57
}
58
for _, f := range args.Filters {
59
cfg.Filters = append(cfg.Filters, &promaws.EC2Filter{
60
Name: f.Name,
61
Values: f.Values,
62
})
63
}
64
return cfg
65
}
66
67
var DefaultEC2SDConfig = EC2Arguments{
68
Port: 80,
69
RefreshInterval: 60 * time.Second,
70
}
71
72
// UnmarshalRiver implements river.Unmarshaler, applying defaults and
73
// validating the provided config.
74
func (args *EC2Arguments) UnmarshalRiver(f func(interface{}) error) error {
75
*args = DefaultEC2SDConfig
76
77
type arguments EC2Arguments
78
if err := f((*arguments)(args)); err != nil {
79
return err
80
}
81
if args.Region == "" {
82
sess, err := session.NewSession()
83
if err != nil {
84
return err
85
}
86
metadata := ec2metadata.New(sess)
87
region, err := metadata.Region()
88
if err != nil {
89
return errors.New("EC2 SD configuration requires a region")
90
}
91
args.Region = region
92
}
93
for _, f := range args.Filters {
94
if len(f.Values) == 0 {
95
return errors.New("EC2 SD configuration filter values cannot be empty")
96
}
97
}
98
return nil
99
}
100
101
// New creates a new discovery.ec2 component.
102
func NewEC2(opts component.Options, args EC2Arguments) (component.Component, error) {
103
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
104
conf := args.(EC2Arguments).Convert()
105
return promaws.NewEC2Discovery(conf, opts.Logger), nil
106
})
107
}
108
109