Skip to content

Commit

Permalink
Merge pull request #77 from mazrean/feature/senario_analysis
Browse files Browse the repository at this point in the history
Feature/senario analysis
  • Loading branch information
mazrean committed Aug 30, 2023
2 parents a5f5e1b + 31f50b3 commit 805d546
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions http/fasthttp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package isuhttp

import (
"fmt"
"strconv"
"strings"
"time"

isutools "github.com/mazrean/isucon-go-tools"
Expand Down Expand Up @@ -99,6 +101,20 @@ func FastMetricsMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler

reqSz := fastHTTPReqSize(&ctx.Request)

// シナリオ解析用メトリクス
flowCookieValue := ctx.Request.Header.Cookie("isutools_flow")
if flowCookieValue != nil {
flowMethod, flowPath, ok := strings.Cut(string(flowCookieValue), ",")
if ok {
flowCounterVec.WithLabelValues(flowMethod, flowPath, method, path).Inc()
}
}
flowCookie := new(fasthttp.Cookie)
flowCookie.SetKey("isutools_flow")
flowCookie.SetValue(fmt.Sprintf("%s,%s", method, path))
flowCookie.SetExpire(time.Now().Add(1 * time.Hour))
ctx.Response.Header.SetCookie(flowCookie)

start := time.Now()
next(ctx)
reqDur := float64(time.Since(start)) / float64(time.Second)
Expand Down
16 changes: 16 additions & 0 deletions http/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package isuhttp

import (
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -57,6 +59,20 @@ func FiberMetricsMiddleware(next fiber.Handler) fiber.Handler {

reqSz := fastHTTPReqSize(c.Request())

// シナリオ解析用メトリクス
flowCookieValue := c.Cookies("isutools_flow")
if flowCookieValue != "" {
flowMethod, flowPath, ok := strings.Cut(flowCookieValue, ",")
if ok {
flowCounterVec.WithLabelValues(flowMethod, flowPath, method, path).Inc()
}
}
flowCookie := new(fiber.Cookie)
flowCookie.Name = "isutools_flow"
flowCookie.Value = fmt.Sprintf("%s,%s", method, path)
flowCookie.Expires = time.Now().Add(1 * time.Hour)
c.Cookie(flowCookie)

start := time.Now()
err := next(c)
reqDur := float64(time.Since(start)) / float64(time.Second)
Expand Down
12 changes: 12 additions & 0 deletions http/gin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package isuhttp

import (
"fmt"
"log"
"strconv"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -54,6 +56,16 @@ func GinMetricsMiddleware(c *gin.Context) {

reqSz := reqSize(c.Request)

// シナリオ解析用メトリクス
flowCookieValue, err := c.Cookie("isutools_flow")
if err == nil {
flowMethod, flowPath, ok := strings.Cut(flowCookieValue, ",")
if ok {
flowCounterVec.WithLabelValues(flowMethod, flowPath, method, path).Inc()
}
}
c.SetCookie("isutools_flow", fmt.Sprintf("%s,%s", method, path), int(1*time.Hour), "", "", false, true)

start := time.Now()
c.Next()
reqDur := float64(time.Since(start)) / float64(time.Second)
Expand Down
16 changes: 16 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/http"
"strconv"
"strings"
"time"

isutools "github.com/mazrean/isucon-go-tools"
Expand Down Expand Up @@ -123,6 +124,21 @@ func StdMetricsMiddleware(next http.Handler) http.Handler {

reqSz := reqSize(req)

// シナリオ解析用メトリクス
flowCookie, err := req.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)
http.SetCookie(res, flowCookie)

start := time.Now()
next.ServeHTTP(wrappedRes, req)
reqDur := float64(time.Since(start)) / float64(time.Second)
Expand Down

0 comments on commit 805d546

Please sign in to comment.