From dfa0f7c6ab275fc8cc712c46235705e7461e2f33 Mon Sep 17 00:00:00 2001 From: Andrew Martinez Date: Thu, 6 Jan 2022 14:02:36 -0500 Subject: [PATCH 1/3] adds metrics for endpoints with keys of path[method] --- controller/server/client-api.go | 3 +++ controller/server/management-api.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/controller/server/client-api.go b/controller/server/client-api.go index f52b3f40a..c1a9b17ca 100644 --- a/controller/server/client-api.go +++ b/controller/server/client-api.go @@ -143,6 +143,8 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler { innerClientHandler := ae.ClientApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s[%s]", r.URL.Path, r.Method)) + start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) //if not /edge prefix and not /fabric, translate to "/edge/client/v", this is a hack @@ -175,6 +177,7 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler { response.AddHeaders(rc) innerClientHandler.ServeHTTP(rw, r) + timer.UpdateSince(start) }) return api.TimeoutHandler(api.WrapCorsHandler(handler), 10*time.Second, apierror.NewTimeoutError(), response.EdgeResponseMapper{}) diff --git a/controller/server/management-api.go b/controller/server/management-api.go index 0f4ddd16c..c6ff62fc8 100644 --- a/controller/server/management-api.go +++ b/controller/server/management-api.go @@ -108,6 +108,8 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle innerManagementHandler := ae.ManagementApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s[%s]", r.URL.Path, r.Method)) + start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) if r.URL.Path == controller.ManagementRestApiSpecUrl { @@ -131,6 +133,7 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle response.AddHeaders(rc) innerManagementHandler.ServeHTTP(rw, r) + timer.UpdateSince(start) }) return api.TimeoutHandler(api.WrapCorsHandler(handler), 10*time.Second, apierror.NewTimeoutError(), response.EdgeResponseMapper{}) From 65702f502a104a12600b38f6a9816ac7160f5396 Mon Sep 17 00:00:00 2001 From: Andrew Martinez Date: Thu, 6 Jan 2022 14:11:01 -0500 Subject: [PATCH 2/3] swap from brackets to period seperators --- controller/server/client-api.go | 2 +- controller/server/management-api.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/controller/server/client-api.go b/controller/server/client-api.go index c1a9b17ca..cc80ea96c 100644 --- a/controller/server/client-api.go +++ b/controller/server/client-api.go @@ -143,7 +143,7 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler { innerClientHandler := ae.ClientApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s[%s]", r.URL.Path, r.Method)) + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s.%s", r.URL.Path, r.Method)) start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) diff --git a/controller/server/management-api.go b/controller/server/management-api.go index c6ff62fc8..8ced56b68 100644 --- a/controller/server/management-api.go +++ b/controller/server/management-api.go @@ -108,7 +108,7 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle innerManagementHandler := ae.ManagementApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s[%s]", r.URL.Path, r.Method)) + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s.%s", r.URL.Path, r.Method)) start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) From 8b20f0278ac9680af55faf30a5623f2614f7e662 Mon Sep 17 00:00:00 2001 From: Andrew Martinez Date: Thu, 6 Jan 2022 14:42:31 -0500 Subject: [PATCH 3/3] normalize URL ids for metric names --- controller/server/client-api.go | 2 +- controller/server/management-api.go | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/controller/server/client-api.go b/controller/server/client-api.go index cc80ea96c..ad69ade36 100644 --- a/controller/server/client-api.go +++ b/controller/server/client-api.go @@ -143,7 +143,6 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler { innerClientHandler := ae.ClientApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s.%s", r.URL.Path, r.Method)) start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) @@ -177,6 +176,7 @@ func (clientApi ClientApiHandler) newHandler(ae *env.AppEnv) http.Handler { response.AddHeaders(rc) innerClientHandler.ServeHTTP(rw, r) + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(getMetricTimerName(r)) timer.UpdateSince(start) }) diff --git a/controller/server/management-api.go b/controller/server/management-api.go index 8ced56b68..b557503d8 100644 --- a/controller/server/management-api.go +++ b/controller/server/management-api.go @@ -108,7 +108,6 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle innerManagementHandler := ae.ManagementApi.Serve(nil) handler := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(fmt.Sprintf("%s.%s", r.URL.Path, r.Method)) start := time.Now() rw.Header().Set(ZitiInstanceId, ae.InstanceId) @@ -133,8 +132,30 @@ func (managementApi ManagementApiHandler) newHandler(ae *env.AppEnv) http.Handle response.AddHeaders(rc) innerManagementHandler.ServeHTTP(rw, r) + timer := ae.GetHostController().GetNetwork().GetMetricsRegistry().Timer(getMetricTimerName(r)) timer.UpdateSince(start) }) return api.TimeoutHandler(api.WrapCorsHandler(handler), 10*time.Second, apierror.NewTimeoutError(), response.EdgeResponseMapper{}) } + +// getMetricTimerName returns a metric timer name based on the incoming HTTP request's URL and method. +// Unique ids are removed from the URL and replaced with :id and :subid to group metrics from the same +// endpoint that happen to be working on different ids. +func getMetricTimerName(r *http.Request) string { + cleanUrl := r.URL.Path + + rc, _ := api.GetRequestContextFromHttpContext(r) + + if rc != nil { + if id, err := rc.GetEntityId(); err == nil && id != "" { + cleanUrl = strings.Replace(cleanUrl, id, ":id", -1) + } + + if subid, err := rc.GetEntitySubId(); err == nil && subid != "" { + cleanUrl = strings.Replace(cleanUrl, subid, ":subid", -1) + } + } + + return fmt.Sprintf("%s.%s", cleanUrl, r.Method) +}