add metrics and health endpoint

This commit is contained in:
Arno Uhlig 2017-12-20 13:23:55 -08:00
parent 3f87687e3d
commit be02044f01
2 changed files with 66 additions and 0 deletions

View file

@ -24,6 +24,9 @@ import (
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
@ -42,12 +45,22 @@ func main() {
if os.Getenv("PATH") != "" {
path = os.Getenv("PATH")
}
http.HandleFunc("/", errorHandler(path))
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "ok")
})
http.ListenAndServe(fmt.Sprintf(":8080"), nil)
}
func errorHandler(path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
ext := "html"
format := r.Header.Get(FormatHeader)
@ -95,5 +108,13 @@ func errorHandler(path string) func(http.ResponseWriter, *http.Request) {
defer f.Close()
log.Printf("serving custom error response for code %v and format %v from file %v\n", code, format, file)
io.Copy(w, f)
duration := time.Now().Sub(start).Seconds()
proto := strconv.Itoa(r.ProtoMajor)
proto = proto + "." + strconv.Itoa(r.ProtoMinor)
requestCount.WithLabelValues(proto).Inc()
requestDuration.WithLabelValues(proto).Observe(duration)
}
}

View file

@ -0,0 +1,45 @@
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Collect and display prometheus metrics
package main
import (
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "default_http_backend"
subsystem = "http"
)
var (
requestCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_count_total",
Help: "Counter of HTTP requests made.",
}, []string{"proto"})
requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "request_duration_milliseconds",
Help: "Histogram of the time (in milliseconds) each request took.",
Buckets: append([]float64{.001, .003}, prometheus.DefBuckets...),
}, []string{"proto"})
)