diff --git a/gateway/handlers/notifier_handler.go b/gateway/handlers/notifier_handler.go new file mode 100644 index 00000000..8a407ce3 --- /dev/null +++ b/gateway/handlers/notifier_handler.go @@ -0,0 +1,44 @@ +// Copyright (c) OpenFaaS Author(s) 2018. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +package handlers + +import ( + "net/http" + "time" +) + +// MakeNotifierWrapper wraps a http.HandlerFunc in an interceptor to pass to HTTPNotifier +func MakeNotifierWrapper(next http.HandlerFunc, notifiers []HTTPNotifier) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + then := time.Now() + + writer := newCustomWriter(w) + next(w, r) + + url := r.URL.String() + for _, notifier := range notifiers { + notifier.Notify(r.Method, url, url, writer.CapturedStatusCode, time.Since(then)) + } + } +} + +func newCustomWriter(w http.ResponseWriter) customWriter { + return customWriter{ + w: w, + } +} + +type customWriter struct { + CapturedStatusCode int + w http.ResponseWriter +} + +func (c *customWriter) Write(data []byte) (int, error) { + return c.w.Write(data) +} + +func (c *customWriter) WriteHeader(code int) { + c.CapturedStatusCode = code + c.w.WriteHeader(code) +} diff --git a/gateway/server.go b/gateway/server.go index e3072e76..7bf0c3fc 100644 --- a/gateway/server.go +++ b/gateway/server.go @@ -96,7 +96,7 @@ func main() { faasHandlers.SecretHandler = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer) alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials) - faasHandlers.Alert = handlers.MakeAlertHandler(alertHandler) + faasHandlers.Alert = handlers.MakeNotifierWrapper(handlers.MakeAlertHandler(alertHandler), forwardingNotifiers) if config.UseNATS() { log.Println("Async enabled: Using NATS Streaming.") @@ -174,7 +174,7 @@ func main() { r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}", faasHandlers.QueuedProxy).Methods(http.MethodPost) r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}/{params:.*}", faasHandlers.QueuedProxy).Methods(http.MethodPost) - r.HandleFunc("/system/async-report", faasHandlers.AsyncReport) + r.HandleFunc("/system/async-report", handlers.MakeNotifierWrapper(faasHandlers.AsyncReport, forwardingNotifiers)) } fs := http.FileServer(http.Dir("./assets/"))