mirror of
https://github.com/openfaas/faas.git
synced 2025-06-10 09:16:48 +00:00
Start logging metrics
This commit is contained in:
parent
35a15cff01
commit
9df92c7f1b
@ -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) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Forwarding [%s] to %s", r.Method, r.URL.String())
|
||||
p.ServeHTTP(w, r)
|
||||
}
|
||||
// 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())
|
||||
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