mirror of
https://github.com/openfaas/faas.git
synced 2025-06-09 08:46:48 +00:00
Add instrumentation to the alert handler
Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
parent
e9cf708cb5
commit
5a1bdcdb91
44
gateway/handlers/notifier_handler.go
Normal file
44
gateway/handlers/notifier_handler.go
Normal 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)
|
||||||
|
}
|
@ -96,7 +96,7 @@ func main() {
|
|||||||
faasHandlers.SecretHandler = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)
|
faasHandlers.SecretHandler = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)
|
||||||
|
|
||||||
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials)
|
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials)
|
||||||
faasHandlers.Alert = handlers.MakeAlertHandler(alertHandler)
|
faasHandlers.Alert = handlers.MakeNotifierWrapper(handlers.MakeAlertHandler(alertHandler), forwardingNotifiers)
|
||||||
|
|
||||||
if config.UseNATS() {
|
if config.UseNATS() {
|
||||||
log.Println("Async enabled: Using NATS Streaming.")
|
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]+}", faasHandlers.QueuedProxy).Methods(http.MethodPost)
|
||||||
r.HandleFunc("/async-function/{name:[-a-zA-Z_0-9]+}/{params:.*}", 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/"))
|
fs := http.FileServer(http.Dir("./assets/"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user