-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
149 lines (121 loc) · 4.04 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
"os"
"github.com/doodlescheduling/saml-exporter/internal/collector"
"github.com/doodlescheduling/saml-exporter/internal/transport"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/common/version"
"github.com/sethvargo/go-envconfig"
flag "github.com/spf13/pflag"
"go.uber.org/zap"
)
type Config struct {
URL []string `env:"URL"`
Log struct {
Level string `env:"LOG_LEVEL, default=info"`
Encoding string `env:"LOG_ENCODING, default=json"`
}
Bind string `env:"BIND, default=:9412"`
MetricsPath string `env:"METRICS_PATH, default=/metrics"`
HealthPath string `env:"HEALTH_PATH, default=/healthz"`
UserAgent string `env:"USER_AGENT, default=saml-exporter (go-http-client)"`
}
var (
config = &Config{}
srv *http.Server
promCollector *collector.Collector
)
func init() {
flag.StringVarP(&config.Log.Level, "log-level", "l", "", "Define the log level (default is warning) [debug,info,warn,error]")
flag.StringVarP(&config.Log.Encoding, "log-encoding", "e", "", "Define the log format (default is json) [json,console]")
flag.StringVarP(&config.Bind, "bind", "b", "", "Address to bind http server (default is :9412)")
flag.StringVarP(&config.MetricsPath, "metrics-path", "", "", "Metric path (default is /metrics)")
flag.StringVarP(&config.HealthPath, "health-path", "", "", "Health probe path (default is /healthz)")
flag.StringVarP(&config.UserAgent, "user-agent", "", "", "HTTP client user-agent header")
}
func main() {
ctx := context.Background()
if err := envconfig.Process(ctx, config); err != nil {
log.Fatal(err)
}
flag.Parse()
logger, err := buildLogger()
must(err)
if len(flag.Args()) > 0 {
config.URL = flag.Args()
}
urls, err := buildURL()
must(err)
prometheus.MustRegister(version.NewCollector("saml_exporter"))
client := http.DefaultClient
metricsRoundTripper := transport.NewMetrics(http.DefaultTransport)
client.Transport = transport.NewLogger(logger, transport.NewUserAgent(config.UserAgent, metricsRoundTripper))
promCollector = collector.New(logger, http.DefaultClient, urls, metricsRoundTripper)
prometheus.MustRegister(promCollector)
logger.Info("starting http server...", "bind", config.Bind)
srv = buildHTTPServer(prometheus.DefaultGatherer)
err = srv.ListenAndServe()
// Only panic if we have a net error
if _, ok := err.(*net.OpError); ok {
panic(err)
} else {
os.Stderr.WriteString(err.Error() + "\n")
}
}
func buildURL() ([]*url.URL, error) {
if len(config.URL) == 0 {
return nil, errors.New("at least one url to a saml metadata endpoint is required")
}
var result []*url.URL
for _, u := range config.URL {
res, err := url.Parse(u)
if err != nil {
return nil, err
}
result = append(result, res)
}
return result, nil
}
func buildLogger() (logr.Logger, error) {
logOpts := zap.NewDevelopmentConfig()
logOpts.Encoding = config.Log.Encoding
err := logOpts.Level.UnmarshalText([]byte(config.Log.Level))
if err != nil {
return logr.Discard(), err
}
zapLog, err := logOpts.Build()
if err != nil {
return logr.Discard(), err
}
return zapr.NewLogger(zapLog), nil
}
// Run executes a blocking http server. Starts the http listener with the metrics and healthz endpoints.
func buildHTTPServer(reg prometheus.Gatherer) *http.Server {
mux := http.NewServeMux()
if config.MetricsPath != "/" {
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, fmt.Sprintf("Use the %s endpoint", config.MetricsPath), http.StatusOK)
})
}
mux.HandleFunc(config.HealthPath, func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
mux.HandleFunc(config.MetricsPath, func(w http.ResponseWriter, r *http.Request) {
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(w, r)
})
srv := http.Server{Addr: config.Bind, Handler: mux}
return &srv
}
func must(err error) {
if err != nil {
panic(err)
}
}