mirror of
https://github.com/openfaas/faas.git
synced 2025-06-19 20:46:41 +00:00
Add call-id via middleware
Signed-off-by: Alex Ellis <alexellis2@gmail.com>
This commit is contained in:
@ -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 |
|
||||
|
24
gateway/handlers/callid_middleware.go
Normal file
24
gateway/handlers/callid_middleware.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
Reference in New Issue
Block a user