|
6 | 6 | "crypto/tls" |
7 | 7 | "crypto/x509" |
8 | 8 | "fmt" |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | + "github.com/prometheus/client_golang/prometheus/promauto" |
| 11 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
9 | 12 | "io/ioutil" |
10 | 13 | "log" |
11 | 14 | "net/http" |
@@ -138,6 +141,24 @@ func start_app(config Config) { |
138 | 141 | config.Web_Path_Prefix = web_path_prefix |
139 | 142 | } |
140 | 143 |
|
| 144 | + // Counter for login requests |
| 145 | + loginCounter := promauto.NewCounterVec( |
| 146 | + prometheus.CounterOpts{ |
| 147 | + Name: "dex_k8s_authenticator_login_requests_total", |
| 148 | + Help: "Total number of login requests by HTTP status code, method and cluster.", |
| 149 | + }, |
| 150 | + []string{"code", "cluster"}, |
| 151 | + ) |
| 152 | + |
| 153 | + // Counter for callback response |
| 154 | + callbackCounter := promauto.NewCounterVec( |
| 155 | + prometheus.CounterOpts{ |
| 156 | + Name: "dex_k8s_authenticator_callback_response_total", |
| 157 | + Help: "Total number of callback response by HTTP status code, method and cluster.", |
| 158 | + }, |
| 159 | + []string{"code", "cluster"}, |
| 160 | + ) |
| 161 | + |
141 | 162 | // Generate handlers for each cluster |
142 | 163 | for i, _ := range config.Clusters { |
143 | 164 | cluster := config.Clusters[i] |
@@ -199,24 +220,57 @@ func start_app(config Config) { |
199 | 220 | os.Exit(1) |
200 | 221 | } |
201 | 222 |
|
202 | | - // Each cluster gets a different login and callback URL |
203 | | - http.HandleFunc(base_redirect_uri.Path, cluster.handleCallback) |
| 223 | + // Cluster Label for Metrics |
| 224 | + clusterLabel := prometheus.Labels{"cluster": cluster.Name} |
| 225 | + |
| 226 | + // Each cluster gets a different callback URL |
| 227 | + http.HandleFunc(base_redirect_uri.Path, |
| 228 | + promhttp.InstrumentHandlerCounter( |
| 229 | + loginCounter.MustCurryWith(clusterLabel), |
| 230 | + http.HandlerFunc(cluster.handleCallback), |
| 231 | + ), |
| 232 | + ) |
204 | 233 | log.Printf("Registered callback handler at: %s", base_redirect_uri.Path) |
205 | 234 |
|
| 235 | + // Each cluster gets a different login URL |
206 | 236 | login_uri := path.Join(config.Web_Path_Prefix, "login", cluster.Name) |
207 | | - http.HandleFunc(login_uri, cluster.handleLogin) |
| 237 | + http.HandleFunc(login_uri, |
| 238 | + promhttp.InstrumentHandlerCounter( |
| 239 | + callbackCounter.MustCurryWith(clusterLabel), |
| 240 | + http.HandlerFunc(cluster.handleLogin), |
| 241 | + ), |
| 242 | + ) |
208 | 243 | log.Printf("Registered login handler at: %s", login_uri) |
209 | 244 | } |
210 | 245 |
|
211 | 246 | // Index page |
212 | | - http.HandleFunc(config.Web_Path_Prefix, config.handleIndex) |
| 247 | + indexHandler := promhttp.InstrumentHandlerCounter( |
| 248 | + promauto.NewCounterVec( |
| 249 | + prometheus.CounterOpts{ |
| 250 | + Name: "dex_k8s_authenticator_index_requests_total", |
| 251 | + Help: "Total number of index requests by HTTP status code.", |
| 252 | + }, |
| 253 | + []string{"code", "method"}, |
| 254 | + ), |
| 255 | + http.HandlerFunc(config.handleIndex), |
| 256 | + ) |
| 257 | + http.HandleFunc(config.Web_Path_Prefix, indexHandler) |
| 258 | + log.Printf("Registered index handler at: %s", config.Web_Path_Prefix) |
213 | 259 |
|
214 | 260 | // Serve static html assets |
215 | 261 | fs := http.FileServer(http.Dir("html/static/")) |
216 | 262 | static_uri := path.Join(config.Web_Path_Prefix, "static") + "/" |
| 263 | + http.Handle(static_uri, http.StripPrefix(static_uri, fs)) |
217 | 264 | log.Printf("Registered static assets handler at: %s", static_uri) |
218 | 265 |
|
219 | | - http.Handle(static_uri, http.StripPrefix(static_uri, fs)) |
| 266 | + // Export metrics for prometheus |
| 267 | + http.Handle("/metrics", promhttp.Handler()) |
| 268 | + log.Printf("Registered metrics handler at: %s", "/metrics") |
| 269 | + |
| 270 | + // Register health check endpoint |
| 271 | + healthzPattern := path.Join(config.Web_Path_Prefix, "healthz") |
| 272 | + http.HandleFunc(healthzPattern, config.handleHealthz) |
| 273 | + log.Printf("Registered healthz handler at: %s", healthzPattern) |
220 | 274 |
|
221 | 275 | // Determine whether to use TLS or not |
222 | 276 | switch listenURL.Scheme { |
@@ -302,7 +356,6 @@ var RootCmd = &cobra.Command{ |
302 | 356 |
|
303 | 357 | // Fallback if no args specified |
304 | 358 | cmd.HelpFunc()(cmd, args) |
305 | | - |
306 | 359 | }, |
307 | 360 | } |
308 | 361 |
|
|
0 commit comments