forked from commercetools/mongodbatlas_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (54 loc) · 2.35 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
68
package main
import (
"fmt"
"mongodbatlas_exporter/mongodbatlas"
"mongodbatlas_exporter/registerer"
"net/http"
"os"
"time"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"gopkg.in/alecthomas/kingpin.v2"
)
const (
name = "mongodbatlas_exporter"
)
var (
listenAddress = kingpin.Flag("listen-address", "The address to listen on for HTTP requests.").Default(":9905").Envar("LISTEN_ADDRESS").String()
atlasPublicKey = kingpin.Flag("atlas.public-key", "Atlas API public key").Envar("ATLAS_PUBLIC_KEY").String()
atlasPrivateKey = kingpin.Flag("atlas.private-key", "Atlas API private key").Envar("ATLAS_PRIVATE_KEY").String()
atlasProjectID = kingpin.Flag("atlas.project-id", "Atlas project id (group id) to scrape metrics from").Envar("ATLAS_PROJECT_ID").String()
atlasClusters = kingpin.Flag("atlas.cluster", "Atlas cluster name to scrape metrics from. Can be defined multiple times. If not defined all clusters in the project will be scraped").Strings()
logLevel = kingpin.Flag("log-level", "Printed logs level.").Default("info").Enum("error", "warn", "info", "debug")
up = promauto.NewGauge(prometheus.GaugeOpts{
Name: "mongodbatlas_up",
Help: "Was the last communication with MongoDB Atlas API successful and Project is not empty.",
})
)
func main() {
kingpin.Version(version.Print(name))
kingpin.Parse()
logger, err := createLogger(*logLevel)
if err != nil {
fmt.Printf("failed to create logger with error: %v", err)
os.Exit(1)
}
prometheus.MustRegister(version.NewCollector(name))
client, err := mongodbatlas.NewClient(logger, *atlasPublicKey, *atlasPrivateKey, *atlasProjectID, *atlasClusters)
if err != nil {
level.Error(logger).Log("msg", "failed to create MongoDB Atlas client", "err", err)
os.Exit(1)
}
processRegister := registerer.NewProcessRegisterer(logger, client, time.Minute)
go processRegister.Observe()
up.Set(1)
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(*listenAddress, nil); err != nil {
level.Error(logger).Log("msg", "failed to start the http server", "err", err)
os.Exit(1)
}
level.Info(logger).Log("msg", "successfully started http server", "address", listenAddress)
}