-
Notifications
You must be signed in to change notification settings - Fork 0
/
fiber.go
111 lines (95 loc) · 2.54 KB
/
fiber.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package isuhttp
import (
"errors"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
isutools "github.com/mazrean/isucon-go-tools"
)
func FiberNew(conf ...fiber.Config) *fiber.App {
if enableGoJson {
if len(conf) == 0 {
conf = []fiber.Config{
{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
},
}
} else {
conf[0].JSONEncoder = json.Marshal
conf[0].JSONDecoder = json.Unmarshal
}
}
app := fiber.New(conf...)
app.Use(FiberMetricsMiddleware)
listener, ok, err := newUnixDomainSockListener()
if err != nil {
log.Printf("failed to init unix domain socket: %s\n", err)
}
if ok {
err := app.Listener(listener)
if err != nil {
log.Printf("failed to set listener: %s\n", err)
}
}
return app
}
func FiberMetricsMiddleware(next fiber.Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
if !isutools.Enable {
return next(c)
}
path := c.Route().Path
host := c.Hostname()
method := c.Method()
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)
// error handlerがDefaultHTTPErrorHandlerでない場合、正しくない可能性あり
var (
statusCode int
resSize float64
)
if err == nil {
statusCode = c.Response().StatusCode()
resSize = float64(c.Response().Header.ContentLength())
} else {
var httpError *fiber.Error
if errors.As(err, &httpError) {
statusCode = httpError.Code
} else {
statusCode = fiber.StatusInternalServerError
}
if method == http.MethodHead {
resSize = 0
} else {
resSize = float64(len(err.Error()))
}
}
strStatusCode := strconv.Itoa(statusCode)
reqSizeHistogramVec.WithLabelValues(strStatusCode, method, path).Observe(reqSz)
reqDurHistogramVec.WithLabelValues(strStatusCode, method, path).Observe(reqDur)
reqCounterVec.WithLabelValues(strStatusCode, method, host, path).Inc()
resSizeHistogramVec.WithLabelValues(strStatusCode, method, path).Observe(resSize)
return nil
}
}