mirror of
https://github.com/openfaas/faas.git
synced 2025-06-13 18:56:46 +00:00
Start logging metrics
This commit is contained in:
parent
35a15cff01
commit
9df92c7f1b
@ -5,6 +5,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
@ -13,6 +16,7 @@ import (
|
|||||||
"github.com/alexellis/faas/gateway/metrics"
|
"github.com/alexellis/faas/gateway/metrics"
|
||||||
"github.com/alexellis/faas/gateway/types"
|
"github.com/alexellis/faas/gateway/types"
|
||||||
"github.com/docker/docker/client"
|
"github.com/docker/docker/client"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
@ -63,12 +67,12 @@ func main() {
|
|||||||
|
|
||||||
reverseProxy := httputil.NewSingleHostReverseProxy(config.FunctionsProviderURL)
|
reverseProxy := httputil.NewSingleHostReverseProxy(config.FunctionsProviderURL)
|
||||||
|
|
||||||
faasHandlers.Proxy = handler(reverseProxy)
|
faasHandlers.Proxy = makeHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.RoutelessProxy = handler(reverseProxy)
|
faasHandlers.RoutelessProxy = makeHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.Alert = handler(reverseProxy)
|
faasHandlers.Alert = makeHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.ListFunctions = handler(reverseProxy)
|
faasHandlers.ListFunctions = makeHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.DeployFunction = handler(reverseProxy)
|
faasHandlers.DeployFunction = makeHandler(reverseProxy, &metricsOptions)
|
||||||
faasHandlers.DeleteFunction = handler(reverseProxy)
|
faasHandlers.DeleteFunction = makeHandler(reverseProxy, &metricsOptions)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
faasHandlers.Proxy = internalHandlers.MakeProxy(metricsOptions, true, dockerClient, &logger)
|
||||||
@ -116,9 +120,68 @@ func main() {
|
|||||||
log.Fatal(s.ListenAndServe())
|
log.Fatal(s.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
func handler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) {
|
// WriteAdapter adapts a ResponseWriter
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
type WriteAdapter struct {
|
||||||
log.Printf("Forwarding [%s] to %s", r.Method, r.URL.String())
|
Writer http.ResponseWriter
|
||||||
p.ServeHTTP(w, r)
|
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())
|
||||||
|
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.
|
||||||
|
}
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user