This repository has been archived by the owner on Jun 4, 2024. It is now read-only.
forked from awslabs/aws-cloudmap-prometheus-sd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (58 loc) · 2.03 KB
/
main.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
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/awslabs/aws-cloudmap-prometheus-sd/pkg/adapter"
"github.com/awslabs/aws-cloudmap-prometheus-sd/pkg/discovery"
"github.com/go-kit/kit/log"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
a = kingpin.New("aws-cloudmap-prometheus-sd usage", "Tool to generate file_sd target files for AWS Cloud Map services.")
outputFileFlag = a.Flag("output.file", "Output file for file_sd compatible file.").Default("cloudmap_sd.json").String()
awsRegionFlag = a.Flag("aws.region", "AWS Region to use. If none provided, region will be auto-discovered by AWS SDK using environment.").String()
cloudmapNamespaceFlag = a.Flag("cloudmap.namespace", "CloudMap namespace to discovery services. If none provided all namespaces will be discovered").String()
refreshIntervalFlag = a.Flag("target.refresh", "The refresh interval (in seconds).").Default("60").Int()
logger log.Logger
)
func main() {
a.HelpFlag.Short('h')
_, err := a.Parse(os.Args[1:])
if err != nil {
fmt.Println("err: ", err)
return
}
logger = log.NewSyncLogger(log.NewLogfmtLogger(os.Stdout))
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
ctx := context.Background()
cfg := discovery.SDConfig{
RefreshInterval: *refreshIntervalFlag,
}
sess, err := session.NewSession()
if err != nil {
fmt.Println("err: ", err)
return
}
if aws.StringValue(awsRegionFlag) != "" {
cfg.Region = aws.StringValue(awsRegionFlag)
} else {
metadata := ec2metadata.New(sess)
region, err := metadata.Region()
if err != nil {
fmt.Println("err: SD configuration requires a region")
return
}
cfg.Region = region
}
disc, err := discovery.NewDiscovery(logger, cfg)
if err != nil {
fmt.Println("err: ", err)
}
sdAdapter := adapter.NewAdapter(ctx, *outputFileFlag, "aws-cloudmap-prometheus-sd", disc, logger)
sdAdapter.Run()
<-ctx.Done()
}