-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexporter.go
114 lines (99 loc) · 3.73 KB
/
exporter.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
dto "github.com/prometheus/client_model/go"
"github.com/grafana/cloudcost-exporter/cmd/exporter/config"
"github.com/grafana/cloudcost-exporter/pkg/aws"
"github.com/grafana/cloudcost-exporter/pkg/collector"
"github.com/grafana/cloudcost-exporter/pkg/google"
)
func ProviderFlags(fs *flag.FlagSet, awsProfiles, gcpProjects, awsServices, gcpServices *config.StringSliceFlag) {
fs.Var(awsProfiles, "aws.profile", "AWS profile(s).")
// TODO: RENAME THIS TO JUST PROJECTS
fs.Var(gcpProjects, "gcp.bucket-projects", "GCP project(s).")
fs.Var(awsServices, "aws.services", "AWS service(s).")
fs.Var(gcpServices, "gcp.services", "GCP service(s).")
}
func main() {
var cfg config.Config
ProviderFlags(flag.CommandLine, &cfg.Providers.AWS.Profiles, &cfg.Providers.GCP.Projects, &cfg.Providers.AWS.Services, &cfg.Providers.GCP.Services)
provider := flag.String("provider", "aws", "AWS or GCP")
flag.DurationVar(&cfg.Collector.ScrapeInterval, "scrape-interval", 1*time.Hour, "Scrape interval")
flag.StringVar(&cfg.Providers.AWS.Region, "aws.region", "", "AWS region")
flag.StringVar(&cfg.ProjectID, "project-id", "ops-tools-1203", "Project ID to target.")
flag.StringVar(&cfg.Server.Address, "server.address", ":8080", "Default address for the server to listen on.")
flag.StringVar(&cfg.Server.Path, "server.path", "/metrics", "Default path for the server to listen on.")
flag.IntVar(&cfg.Providers.GCP.DefaultGCSDiscount, "gcp.default-discount", 19, "GCP default discount")
flag.Parse()
log.Print("Version ", version.Info())
log.Print("Build Context ", version.BuildContext())
var gatherer prometheus.GathererFunc
var csp collector.CSP
var err error
switch *provider {
case "aws":
csp, err = aws.NewAWS(&aws.Config{
Region: cfg.Providers.AWS.Region,
Profile: cfg.Providers.AWS.Profiles.String(),
ScrapeInterval: cfg.Collector.ScrapeInterval,
Services: strings.Split(cfg.Providers.AWS.Services.String(), ","),
})
case "gcp":
csp, err = google.NewGCP(&google.Config{
ProjectId: cfg.ProjectID,
Region: cfg.Providers.GCP.Region,
Projects: cfg.Providers.GCP.Projects.String(),
DefaultDiscount: cfg.Providers.GCP.DefaultGCSDiscount,
ScrapeInterval: cfg.Collector.ScrapeInterval,
Services: strings.Split(cfg.Providers.GCP.Services.String(), ","),
})
default:
err = fmt.Errorf("unknown provider")
}
if err != nil {
log.Printf("Error setting up provider %s: %s", *provider, err)
os.Exit(1)
}
gatherer, err = gathererFunc(csp)
if err != nil {
log.Printf("Error setting up gatherer: %s", err)
os.Exit(1)
}
// Collect http server for prometheus
http.Handle(cfg.Server.Path, promhttp.HandlerFor(gatherer, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
log.Printf("Listening on %s:%s", cfg.Server.Address, cfg.Server.Path)
if err = http.ListenAndServe(cfg.Server.Address, nil); err != nil {
log.Printf("Error listening and serving: %s", err)
os.Exit(1)
}
}
func gathererFunc(csp collector.CSP) (prometheus.GathererFunc, error) {
reg := prometheus.NewRegistry()
reg.MustRegister(
collectors.NewBuildInfoCollector(),
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
version.NewCollector("cloudcost_exporter"),
)
if err := csp.RegisterCollectors(reg); err != nil {
return nil, err
}
return func() ([]*dto.MetricFamily, error) {
if err := csp.CollectMetrics(); err != nil {
return nil, err
}
return reg.Gather()
}, nil
}