Skip to content
Snippets Groups Projects
Commit 9df92c7f authored by Alex's avatar Alex Committed by Alex Ellis
Browse files

Start logging metrics

parent 35a15cff
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,9 @@ import (
"log"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"time"
"fmt"
......@@ -13,6 +16,7 @@ import (
"github.com/alexellis/faas/gateway/metrics"
"github.com/alexellis/faas/gateway/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/gorilla/mux"
)
......@@ -63,12 +67,12 @@ func main() {
reverseProxy := httputil.NewSingleHostReverseProxy(config.FunctionsProviderURL)
faasHandlers.Proxy = handler(reverseProxy)
faasHandlers.RoutelessProxy = handler(reverseProxy)
faasHandlers.Alert = handler(reverseProxy)
faasHandlers.ListFunctions = handler(reverseProxy)
faasHandlers.DeployFunction = handler(reverseProxy)
faasHandlers.DeleteFunction = handler(reverseProxy)
faasHandlers.Proxy = makeHandler(reverseProxy, &metricsOptions)
faasHandlers.RoutelessProxy = makeHandler(reverseProxy, &metricsOptions)
faasHandlers.Alert = makeHandler(reverseProxy, &metricsOptions)
faasHandlers.ListFunctions = makeHandler(reverseProxy, &metricsOptions)
faasHandlers.DeployFunction = makeHandler(reverseProxy, &metricsOptions)
faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions)
} else {
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
......@@ -116,9 +120,68 @@ func main() {
log.Fatal(s.ListenAndServe())
}
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
// WriteAdapter adapts a ResponseWriter
type WriteAdapter struct {
Writer http.ResponseWriter
HttpResult *HttpResult
}
type HttpResult struct {
HeaderCode int
}
//NewWriteAdapter create a new NewWriteAdapter
func NewWriteAdapter(w http.ResponseWriter) WriteAdapter {
return WriteAdapter{Writer: w, HttpResult: &HttpResult{}}
}
//Header adapts Header
func (w WriteAdapter) Header() http.Header {
return w.Writer.Header()
}
// Write adapts Write
func (w WriteAdapter) Write(data []byte) (int, error) {
return w.Writer.Write(data)
}
// WriteHeader adapts WriteHeader
func (w WriteAdapter) WriteHeader(i int) {
w.Writer.WriteHeader(i)
w.HttpResult.HeaderCode = i
fmt.Println("GetHeaderCode before", w.HttpResult.HeaderCode)
}
// GetHeaderCode result from WriteHeader
func (w *WriteAdapter) GetHeaderCode() int {
return w.HttpResult.HeaderCode
}
func makeHandler(proxy *httputil.ReverseProxy, metrics *metrics.MetricOptions) http.HandlerFunc {
// return func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
uri := r.URL.String()
log.Printf("Forwarding [%s] to %s", r.Method, r.URL.String())
p.ServeHTTP(w, r)
start := time.Now()
writeAdapter := NewWriteAdapter(w)
proxy.ServeHTTP(writeAdapter, r)
seconds := time.Since(start).Seconds()
fmt.Printf("[%d] took %f seconds\n", writeAdapter.GetHeaderCode(), seconds)
forward := "/function/"
// fmt.Println(uri)
if len(uri) > len(forward) && strings.Index(uri, forward) == 0 {
fmt.Println("function=", uri[len(forward):])
service := uri[len(forward):]
metrics.GatewayFunctionsHistogram.WithLabelValues(service).Observe(seconds)
code := writeAdapter.GetHeaderCode()
metrics.GatewayFunctionInvocation.With(prometheus.Labels{"function_name": service, "code": strconv.Itoa(code)}).Inc()
}
// metricsOptions.
}
// }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment