diff --git a/watchdog/main.go b/watchdog/main.go index 224bdbeb..387b00df 100644 --- a/watchdog/main.go +++ b/watchdog/main.go @@ -42,6 +42,8 @@ func debugHeaders(source *http.Header, direction string) { } func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) { + startTime := time.Now() + parts := strings.Split(config.faasProcess, " ") if config.debugHeaders { @@ -103,6 +105,9 @@ func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) } } + execTime := time.Since(startTime).Seconds() + w.Header().Set("X-Duration-Seconds", fmt.Sprintf("%f", execTime)) + w.WriteHeader(200) w.Write(out) diff --git a/watchdog/requesthandler_test.go b/watchdog/requesthandler_test.go index 7c9b03d3..87adebaa 100644 --- a/watchdog/requesthandler_test.go +++ b/watchdog/requesthandler_test.go @@ -20,6 +20,33 @@ func TestHandler_make(t *testing.T) { } } +func TestHandler_HasXDurationSecondsHeader(t *testing.T) { + rr := httptest.NewRecorder() + + body := "hello" + req, err := http.NewRequest("POST", "/", bytes.NewBufferString(body)) + if err != nil { + t.Fatal(err) + } + + config := WatchdogConfig{ + faasProcess: "cat", + } + handler := makeRequestHandler(&config) + handler(rr, req) + + required := http.StatusOK + if status := rr.Code; status != required { + t.Errorf("handler returned wrong status code: got %v, but wanted %v", + status, required) + } + + seconds := rr.Header().Get("X-Duration-Seconds") + if len(seconds) == 0 { + t.Errorf("Exec of cat should have given a duration as an X-Duration-Seconds header\n") + } +} + func TestHandler_StatusOKAllowed_ForPOST(t *testing.T) { rr := httptest.NewRecorder()