Add instrumentation to the alert handler

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware) 2019-01-04 19:34:19 +00:00 committed by Alex Ellis
parent e9cf708cb5
commit 5a1bdcdb91
2 changed files with 46 additions and 2 deletions

View File

@ -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)
}

View File

@ -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/"))