-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
97 lines (79 loc) · 2.78 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
package main
import (
"log"
"net/http"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr string
dbDriver string
dbDsn string
dbDsnPasswordMasked string
)
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Print("Starting airflow-exporter")
loadEnv()
log.Print("Connecting to: ", dbDriver, "://", dbDsnPasswordMasked)
c := newCollector(dbDriver, dbDsn)
prometheus.Register(c)
log.Print("Listening on: ", addr)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(addr, nil))
}
func loadEnv() {
databaseDefaultPort := map[string]string{
"mysql": "3306",
"postgres": "5432",
}
databaseBackend := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_BACKEND", "mysql")
databaseHost := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_HOST", "localhost")
databasePort := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_PORT", databaseDefaultPort[databaseBackend])
if !(databaseBackend == "mysql" || databaseBackend == "postgres") {
log.Fatal("airflow-exporter: Unknown database backend specified: ", databaseBackend)
}
databaseUser := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_USER", "airflow")
databasePassword := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_PASSWORD", "airflow")
databaseName := getEnvOr("AIRFLOW_PROMETHEUS_DATABASE_NAME", "airflow")
addr = getEnvOr("AIRFLOW_PROMETHEUS_LISTEN_ADDR", ":9112")
dbDriver = databaseBackend
if databaseBackend == "mysql" {
dbDsn = databaseUser + ":" + databasePassword + "@(" + databaseHost + ":" + databasePort + ")/" + databaseName
dbDsnPasswordMasked = databaseUser + ":********@(" + databaseHost + ":" + databasePort + ")/" + databaseName
} else if databaseBackend == "postgres" {
properties := map[string]string{
"user": databaseUser,
"password": databasePassword,
"host": databaseHost,
"port": databasePort,
"dbname": databaseName,
"sslmode": getEnvOr("AIRFLOW_PROMETHEUS_POSTGRES_SSL_MODE", "disable"),
"sslcert": getEnvOr("AIRFLOW_PROMETHEUS_POSTGRES_SSL_CERT", ""),
"sslkey": getEnvOr("AIRFLOW_PROMETHEUS_POSTGRES_SSL_KEY", ""),
"sslrootcert": getEnvOr("AIRFLOW_PROMETHEUS_POSTGRES_SSL_ROOT_CERT", ""),
}
dbDsn = createPostgresDsn(properties)
if properties["password"] != "" {
properties["password"] = "********"
}
dbDsnPasswordMasked = createPostgresDsn(properties)
}
}
func getEnvOr(key string, defaultValue string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return defaultValue
}
func createPostgresDsn(properties map[string]string) string {
list := make([]string, 0)
for key, value := range properties {
if value != "" {
list = append(list, key+"="+value)
}
}
return strings.Join(list, " ")
}