-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
76 lines (66 loc) · 2.13 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
69
70
71
72
73
74
75
76
package main
import (
"context"
"github.com/namsral/flag"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"log"
"mongodb-query-exporter/aggregations"
exporter2 "mongodb-query-exporter/exporter"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var addr = flag.String("listen-address", ":9736", "The address to listen for HTTP requests")
var aggregationPath = flag.String("config-path", "./data/*.json", "The glob pattern to the metric config files")
var mongoDbUri = flag.String("mongodb-uri", "mongodb://localhost:27017", "The connection string to connect to the mongodb")
func main() {
flag.Parse()
http.Handle("/metrics", promhttp.Handler())
client := connectToMongoDb()
exporter, err := exporter2.NewExporter(*aggregationPath, client)
if err != nil {
log.Println(err)
}
prometheus.MustRegister(exporter)
prometheus.MustRegister(aggregations.ExecutionHistogram)
signalListener(exporter)
log.Println("MongoDB query exporter starting on " + *addr + ", exporting metrics on /metrics")
log.Fatal(http.ListenAndServe(*addr, nil))
}
func connectToMongoDb() *mongo.Client {
log.Printf("Connecting to MongoDB")
client, err := mongo.NewClient(options.Client().ApplyURI(*mongoDbUri))
if err != nil {
log.Fatalf("could not create mongodb client. reason=%v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
err = client.Connect(ctx)
if err != nil {
log.Fatalf("could not connect to mongodb. reason=%v", err)
}
ctx, _ = context.WithTimeout(context.Background(), 5*time.Second)
err = client.Ping(ctx, readpref.Primary())
if err != nil {
log.Fatalf("Tried to establish connection to mongodb, but failed: reason=%v", err)
}
log.Printf("Connected to MongoDB")
return client
}
func signalListener(exporter *exporter2.Exporter) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1)
go func() {
for {
<-signals
log.Printf("Reloading config")
exporter.Reload()
}
}()
}