Skip to content

Commit

Permalink
Merge branch 'main' into fix/typo
Browse files Browse the repository at this point in the history
  • Loading branch information
mazrean authored Oct 24, 2023
2 parents bd34f03 + 33c1b2e commit 399b924
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
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,
responseWriterMetrics: responseWriterMetrics{
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

0 comments on commit 399b924

Please sign in to comment.