diff --git a/gateway/README.md b/gateway/README.md index bef79fd6119e8d8dbb996dda41f6e7bc993ff0d0..049a79c4122055a32ad3a9bbf6195cc36bc70852 100644 --- a/gateway/README.md +++ b/gateway/README.md @@ -5,7 +5,23 @@ The gateway will scale functions according to demand by altering the service rep Swagger docs: https://github.com/openfaas/faas/tree/master/api-docs -**Environmental overrides:** +## Logs + +Logs are available at the function level and can be accessed through Swarm or Kubernetes using native tooling. You can also install a Docker logging driver to aggregate your logs. By default functions will not write the request and response bodies to stdout. You can toggle this behaviour by setting `read_debug` for the request and `write_debug` for the response. + +## Tracing + +An "X-Call-Id" header is applied to every incoming call through the gateway and is usable for tracing and monitoring calls. We use a UUID for this string. + +Header: + +``` +X-Call-Id +``` + +Within a function this is available as `Http_X_Call_Id`. + +## Environmental overrides The gateway can be configured through the following environment variables: | Option | Usage | diff --git a/gateway/handlers/callid_middleware.go b/gateway/handlers/callid_middleware.go new file mode 100644 index 0000000000000000000000000000000000000000..123148c63af50dd329a727a70ae030f56a5affdd --- /dev/null +++ b/gateway/handlers/callid_middleware.go @@ -0,0 +1,24 @@ +package handlers + +import ( + "fmt" + "net/http" + "time" + + "github.com/docker/distribution/uuid" +) + +// MakeCallIDMiddleware middleware tags a request with a uid +func MakeCallIDMiddleware(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + callID := uuid.Generate().String() + r.Header.Add("X-Call-Id", callID) + w.Header().Add("X-Call-Id", callID) + + r.Header.Add("X-Start-Time", fmt.Sprintf("%d", start.UTC().UnixNano())) + w.Header().Add("X-Start-Time", fmt.Sprintf("%d", start.UTC().UnixNano())) + + next(w, r) + } +} diff --git a/gateway/server.go b/gateway/server.go index 2e01a1681e2b4cd9bd627ffdb679e0f53a1eaf86..5dbd9c2f0694519966a9e15cfb718f5a27be6dda 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -108,7 +108,7 @@ func main() { prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{}) listFunctions := metrics.AddMetricsHandler(faasHandlers.ListFunctions, prometheusQuery) - + faasHandlers.Proxy = internalHandlers.MakeCallIDMiddleware(faasHandlers.Proxy) r := mux.NewRouter() // r.StrictSlash(false) // This didn't work, so register routes twice.