Skip to content

Commit

Permalink
echoに試験的にシナリオ解析用メトリクス導入
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean committed Aug 28, 2023
1 parent e2111c2 commit a5f5e1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 17 additions & 1 deletion http/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -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でない場合、正しくない可能性あり
Expand Down
6 changes: 6 additions & 0 deletions http/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a5f5e1b

Please sign in to comment.