forked from PierreZ/fdb-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
130 lines (103 loc) · 3.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/joyent/fdb-prometheus-exporter/models"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
db fdb.Database
jsonKey = append([]byte{255, 255}, []byte("/status/json")...)
)
func main() {
apiVersion, err := strconv.Atoi(getEnv("FDB_API_VERSION", "620"))
if err != nil {
log.Fatal("cannot parse FDB_API_VERSION from env")
}
// Different API versions may expose different runtime behaviors.
fdb.MustAPIVersion(apiVersion)
clusterFile := getEnv("FDB_CLUSTER_FILE", "/etc/foundationdb/fdb.cluster")
// Open the default database from the system cluster
log.Println("opening cluster file at", clusterFile)
db = fdb.MustOpenDatabase(clusterFile)
exportWorkload, err := strconv.ParseBool(getEnv("FDB_EXPORT_WORKLOAD", "true"))
if err != nil {
log.Fatal("cannot parse FDB_EXPORT_WORLOAD from env")
}
exportDatabaseStatus, err := strconv.ParseBool(getEnv("FDB_EXPORT_DATABASE_STATUS", "true"))
if err != nil {
log.Fatal("cannot parse FDB_EXPORT_DATABASE_STATUS from env")
}
exportConfiguration, err := strconv.ParseBool(getEnv("FDB_EXPORT_CONFIGURATION", "true"))
if err != nil {
log.Fatal("cannot parse FDB_EXPORT_CONFIGURATION from env")
}
exportProcesses, err := strconv.ParseBool(getEnv("FDB_EXPORT_PROCESSES", "true"))
if err != nil {
log.Fatal("cannot parse FDB_EXPORT_PROCESSES from env")
}
listenTo := getEnv("FDB_METRICS_LISTEN", ":8080")
refreshEvery, err := strconv.Atoi(getEnv("FDB_METRICS_EVERY", "10"))
if err != nil {
log.Fatal("cannot parse FDB_METRICS_EVERY from env")
}
ticker := time.NewTicker(time.Duration(refreshEvery) * time.Second)
go func() {
for range ticker.C {
//Call the periodic function here.
models, err := retrieveMetrics()
if err != nil {
log.Printf("cannot retrieve metrics from FDB: (%v)\n", err)
continue
}
log.Println("retrieved fdb data")
if exportWorkload {
models.ExportWorkload()
}
if exportDatabaseStatus {
models.ExportDatabaseStatus()
}
if exportConfiguration {
models.ExportConfiguration()
}
if exportProcesses {
models.ExportProcesses()
}
}
}()
r := prometheus.NewRegistry()
models.Register(r)
// [...] update metrics within a goroutine
handler := promhttp.HandlerFor(r, promhttp.HandlerOpts{})
http.Handle("/metrics", handler)
log.Fatal(http.ListenAndServe(listenTo, nil))
}
func retrieveMetrics() (*models.FDBStatus, error) {
jsonRaw, err := db.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) {
return tr.Get(fdb.Key(jsonKey)).Get()
})
if err != nil {
return nil, errors.Wrap(err, "cannot get status")
}
var status models.FDBStatus
err = json.Unmarshal([]byte(jsonRaw.([]byte)), &status)
if err != nil {
return nil, errors.Wrap(err, "cannot decode json")
}
return &status, nil
}
// getEnv is wrapping os.getenv with a fallback
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
return fallback
}
return value
}