From a5f5e1b88b2470fa2ae58d54b9333d5db8d60dc5 Mon Sep 17 00:00:00 2001 From: mazrean Date: Tue, 29 Aug 2023 01:32:30 +0900 Subject: [PATCH] =?UTF-8?q?echo=E3=81=AB=E8=A9=A6=E9=A8=93=E7=9A=84?= =?UTF-8?q?=E3=81=AB=E3=82=B7=E3=83=8A=E3=83=AA=E3=82=AA=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E7=94=A8=E3=83=A1=E3=83=88=E3=83=AA=E3=82=AF=E3=82=B9=E5=B0=8E?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/echo.go | 18 +++++++++++++++++- http/prometheus.go | 6 ++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/http/echo.go b/http/echo.go index 95290e4..350cc3f 100644 --- a/http/echo.go +++ b/http/echo.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "strconv" + "strings" "time" "github.com/goccy/go-json" @@ -84,8 +85,23 @@ func EchoMetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc { reqSz := reqSize(c.Request()) + // シナリオ解析用メトリクス + flowCookie, err := c.Cookie("isutools_flow") + if err == nil { + flowMethod, flowPath, ok := strings.Cut(flowCookie.Value, ",") + if ok { + flowCounterVec.WithLabelValues(flowMethod, flowPath, method, path).Inc() + } + } else { + flowCookie = new(http.Cookie) + flowCookie.Name = "isutools_flow" + } + flowCookie.Value = fmt.Sprintf("%s,%s", method, path) + flowCookie.Expires = time.Now().Add(1 * time.Hour) + c.SetCookie(flowCookie) + start := time.Now() - err := next(c) + err = next(c) reqDur := float64(time.Since(start)) / float64(time.Second) // error handlerがDefaultHTTPErrorHandlerでない場合、正しくない可能性あり diff --git a/http/prometheus.go b/http/prometheus.go index b31b691..c033b92 100644 --- a/http/prometheus.go +++ b/http/prometheus.go @@ -56,6 +56,12 @@ var reqSizeHistogramVec = promauto.NewHistogramVec(prometheus.HistogramOpts{ Buckets: reqSzBuckets, }, []string{"code", "method", "url"}) +var flowCounterVec = promauto.NewCounterVec(prometheus.CounterOpts{ + Namespace: prometheusNamespace, + Subsystem: prometheusSubsystem, + Name: "flow_total", +}, []string{"source_method", "source_path", "target_method", "target_path"}) + // reqSize 大まかなリクエストサイズ func reqSize(req *http.Request) float64 { size := 0.0