Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/discovery/aws/lightsail.go
4096 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.lightsail",
20
Args: LightsailArguments{},
21
Exports: discovery.Exports{},
22
Build: func(opts component.Options, args component.Arguments) (component.Component, error) {
23
return NewLightsail(opts, args.(LightsailArguments))
24
},
25
})
26
}
27
28
// LightsailArguments is the configuration for AWS Lightsail based service discovery.
29
type LightsailArguments struct {
30
Endpoint string `river:"endpoint,attr,optional"`
31
Region string `river:"region,attr,optional"`
32
AccessKey string `river:"access_key,attr,optional"`
33
SecretKey rivertypes.Secret `river:"secret_key,attr,optional"`
34
Profile string `river:"profile,attr,optional"`
35
RoleARN string `river:"role_arn,attr,optional"`
36
RefreshInterval time.Duration `river:"refresh_interval,attr,optional"`
37
Port int `river:"port,attr,optional"`
38
}
39
40
func (args LightsailArguments) Convert() *promaws.LightsailSDConfig {
41
cfg := &promaws.LightsailSDConfig{
42
Endpoint: args.Endpoint,
43
Region: args.Region,
44
AccessKey: args.AccessKey,
45
SecretKey: promcfg.Secret(args.SecretKey),
46
Profile: args.Profile,
47
RoleARN: args.RoleARN,
48
RefreshInterval: model.Duration(args.RefreshInterval),
49
Port: args.Port,
50
}
51
return cfg
52
}
53
54
// DefaultLightsailSDConfig is the default Lightsail SD configuration.
55
var DefaultLightsailSDConfig = LightsailArguments{
56
Port: 80,
57
RefreshInterval: 60 * time.Second,
58
}
59
60
func (args *LightsailArguments) UnmarshalRiver(f func(interface{}) error) error {
61
*args = DefaultLightsailSDConfig
62
63
type arguments LightsailArguments
64
if err := f((*arguments)(args)); err != nil {
65
return err
66
}
67
if args.Region == "" {
68
sess, err := session.NewSession()
69
if err != nil {
70
return err
71
}
72
metadata := ec2metadata.New(sess)
73
region, err := metadata.Region()
74
if err != nil {
75
return errors.New("Lightsail SD configuration requires a region")
76
}
77
args.Region = region
78
}
79
return nil
80
}
81
82
// New creates a new discovery.lightsail component.
83
func NewLightsail(opts component.Options, args LightsailArguments) (component.Component, error) {
84
return discovery.New(opts, args, func(args component.Arguments) (discovery.Discoverer, error) {
85
conf := args.(LightsailArguments).Convert()
86
return promaws.NewLightsailDiscovery(conf, opts.Logger), nil
87
})
88
}
89
90