Add metrics

This commit is contained in:
Alex 2016-12-22 21:27:07 +00:00
parent 191fdb0321
commit 6073cd79f6
4 changed files with 81 additions and 21 deletions

View File

@ -1,6 +1,8 @@
# faas # faas
Functions as a service Functions as a service
Minimum requirements: Docker 1.13
gateway gateway
======= =======

View File

@ -4,12 +4,16 @@ RUN go get -d github.com/docker/docker/api/types \
&& go get -d github.com/docker/docker/api/types/filters \ && go get -d github.com/docker/docker/api/types/filters \
&& go get -d github.com/docker/docker/api/types/swarm \ && go get -d github.com/docker/docker/api/types/swarm \
&& go get -d github.com/docker/docker/client && go get -d github.com/docker/docker/client
RUN go get github.com/gorilla/mux \
&& go get github.com/prometheus/client_golang/prometheus
WORKDIR /go/src/app/ WORKDIR /go/src/github.com/alexellis/faas/gateway
RUN go get github.com/gorilla/mux
COPY metrics metrics
COPY server.go . COPY server.go .
RUN find /go/src/github.com/alexellis/faas/gateway/
RUN go build RUN go build
EXPOSE 8080 EXPOSE 8080
CMD ["./app"] CMD ["./gateway"]

View File

@ -0,0 +1,13 @@
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
// Handler returns the global http.Handler that provides the prometheus
// metrics format on GET requests
func Handler() http.Handler {
return prometheus.Handler()
}

View File

@ -5,17 +5,26 @@ import (
"context" "context"
"log" "log"
"net/http" "net/http"
"time"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"github.com/alexellis/faas/gateway/metrics"
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
) )
type MetricOptions struct {
GatewayRequestsTotal prometheus.Counter
GatewayServerlessServedTotal prometheus.Counter
GatewayFunctions prometheus.Histogram
}
func lookupSwarmService(serviceName string) (bool, error) { func lookupSwarmService(serviceName string) (bool, error) {
var c *client.Client var c *client.Client
var err error var err error
@ -30,7 +39,12 @@ func lookupSwarmService(serviceName string) (bool, error) {
return len(services) > 0, err return len(services) > 0, err
} }
func proxy(w http.ResponseWriter, r *http.Request) { func makeProxy(metrics MetricOptions) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
metrics.GatewayRequestsTotal.Inc()
start := time.Now()
if r.Method == "POST" { if r.Method == "POST" {
log.Println(r.Header) log.Println(r.Header)
header := r.Header["X-Function"] header := r.Header["X-Function"]
@ -42,7 +56,6 @@ func proxy(w http.ResponseWriter, r *http.Request) {
} }
if exists == true { if exists == true {
// client := http.Client{Timeout: time.Second * 2}
requestBody, _ := ioutil.ReadAll(r.Body) requestBody, _ := ioutil.ReadAll(r.Body)
buf := bytes.NewBuffer(requestBody) buf := bytes.NewBuffer(requestBody)
@ -52,12 +65,40 @@ func proxy(w http.ResponseWriter, r *http.Request) {
} }
responseBody, _ := ioutil.ReadAll(response.Body) responseBody, _ := ioutil.ReadAll(response.Body)
w.Write(responseBody) w.Write(responseBody)
metrics.GatewayServerlessServedTotal.Inc()
metrics.GatewayFunctions.Observe(time.Since(start).Seconds())
}
} }
} }
} }
func main() { func main() {
GatewayRequestsTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "gateway_requests_total",
Help: "Total amount of HTTP requests to the gateway",
})
GatewayServerlessServedTotal := prometheus.NewCounter(prometheus.CounterOpts{
Name: "gateway_serverless_invocation_total",
Help: "Total amount of serverless function invocations",
})
GatewayFunctions := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "gateway_functions",
Help: "Gateway functions",
})
prometheus.Register(GatewayRequestsTotal)
prometheus.Register(GatewayServerlessServedTotal)
prometheus.Register(GatewayFunctions)
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", proxy) r.HandleFunc("/", makeProxy(MetricOptions{
GatewayRequestsTotal: GatewayRequestsTotal,
GatewayServerlessServedTotal: GatewayServerlessServedTotal,
GatewayFunctions: GatewayFunctions,
}))
metricsHandler := metrics.Handler()
r.Handle("/metrics", metricsHandler)
log.Fatal(http.ListenAndServe(":8080", r)) log.Fatal(http.ListenAndServe(":8080", r))
} }