Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http関連のメトリクス修正 #97

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func newResponseWriterWithMetrics(w http.ResponseWriter) *responseWriterWithMetr
return &responseWriterWithMetrics{
ResponseWriter: w,
reponseWriterMetrics: reponseWriterMetrics{
statusCode: -1,
statusCode: 200,
resSize: 0,
},
}
Expand Down Expand Up @@ -173,10 +173,6 @@ func StdMetricsMiddleware(next http.Handler) http.Handler {
next.ServeHTTP(wrappedRes, req)
reqDur := float64(time.Since(start)) / float64(time.Second)

if metrics.statusCode == -1 {
return
}

statusCode := strconv.Itoa(metrics.statusCode)

reqSizeHistogramVec.WithLabelValues(statusCode, method, path).Observe(reqSz)
Expand Down
59 changes: 30 additions & 29 deletions http/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package isuhttp

import (
"net/http"
"strconv"
"strings"
"regexp"
"sync"

"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/valyala/fasthttp"
Expand Down Expand Up @@ -102,38 +101,40 @@ func fastHTTPReqSize(req *fasthttp.Request) float64 {
return size
}

type replacePair struct {
old, new string
}
var (
filterCacheLocker = &sync.RWMutex{}
filterCache = make(map[string]string, 50)
filterReList = []struct {
re *regexp.Regexp
to string
}{{
// uuid
re: regexp.MustCompile(`[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}`),
to: "<uuid>",
}, {
// number
re: regexp.MustCompile(`\d+`),
to: "<number>",
}}
)

var FilterFunc = func(path string) string {
pathList := strings.Split(path, "/")

replacePairs := []replacePair{}
for _, v := range pathList {
_, err := uuid.Parse(v)
if err == nil {
replacePairs = append(replacePairs, replacePair{
old: v,
new: "<uuid>",
})

continue
}
newPath, ok := func() (string, bool) {
filterCacheLocker.RLock()
defer filterCacheLocker.RUnlock()

_, err = strconv.Atoi(v)
if err == nil {
replacePairs = append(replacePairs, replacePair{
old: v,
new: "<int>",
})

continue
if v, ok := filterCache[path]; ok {
return v, true
}

return "", false
}()
if ok {
return newPath
}

for _, pair := range replacePairs {
path = strings.Replace(path, pair.old, pair.new, 1)
for _, re := range filterReList {
path = re.re.ReplaceAllString(path, re.to)
}

return path
Expand Down
Loading