init
This commit is contained in:
1 parent
7014f56cbb
commit
fe6f1f5e5a
25 files changed
+1918
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
JournalMethodsMatcher MethodMatcher = &cacheableMethodsMatcher{}
|
||||
)
|
||||
|
||||
type MethodMatcher interface {
|
||||
Match(method string) bool
|
||||
}
|
||||
|
||||
type cacheableMethodsMatcher struct {
|
||||
cache map[string]bool
|
||||
lock sync.RWMutex
|
||||
rm runtimeMatcher
|
||||
}
|
||||
|
||||
func (mm *cacheableMethodsMatcher) Match(method string) bool {
|
||||
if len(mm.rm.patterns) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
mm.lock.RLock()
|
||||
matched, ok := mm.cache[method]
|
||||
mm.lock.RUnlock()
|
||||
if ok {
|
||||
return matched
|
||||
}
|
||||
|
||||
mm.lock.Lock()
|
||||
defer mm.lock.Unlock()
|
||||
if matched, ok := mm.cache[method]; ok {
|
||||
return matched
|
||||
}
|
||||
matched = mm.rm.Match(method)
|
||||
mm.cache[method] = matched
|
||||
return matched
|
||||
}
|
||||
|
||||
func NewCacheableMethodMatcher(patterns []string) MethodMatcher {
|
||||
return &cacheableMethodsMatcher{
|
||||
cache: make(map[string]bool, 0),
|
||||
rm: runtimeMatcher{patterns: patterns},
|
||||
}
|
||||
}
|
||||
|
||||
type runtimeMatcher struct {
|
||||
patterns []string
|
||||
}
|
||||
|
||||
func (mm runtimeMatcher) Match(method string) bool {
|
||||
matched := false
|
||||
for _, pattern := range mm.patterns {
|
||||
matched, _ = path.Match(pattern, method)
|
||||
if matched {
|
||||
break
|
||||
}
|
||||
}
|
||||
return matched
|
||||
}
|
||||
|
||||
func NewRuntimeMethodMatcher(patterns []string) MethodMatcher {
|
||||
return &runtimeMatcher{
|
||||
patterns: patterns,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import "testing"
|
||||
|
||||
var (
|
||||
patterns = []string{
|
||||
"mdm-master/group/method",
|
||||
"mdm-master/group2/method",
|
||||
"mdm-master/group3/*",
|
||||
}
|
||||
cases = []struct {
|
||||
Method string
|
||||
Result bool
|
||||
}{
|
||||
{Method: "mdm-master/group/method", Result: true},
|
||||
{Method: "mdm-master/group/method2", Result: false},
|
||||
{Method: "mdm-master/group2/method3", Result: false},
|
||||
{Method: "mdm-master/group2/method", Result: true},
|
||||
{Method: "mdm-master/group3/some", Result: true},
|
||||
{Method: "mdm-master/group3/", Result: true},
|
||||
}
|
||||
)
|
||||
|
||||
func TestCacheableMethodsMatcher_Match(t *testing.T) {
|
||||
matcher := NewCacheableMethodMatcher(patterns)
|
||||
for _, c := range cases {
|
||||
res := Match(c.Method)
|
||||
if c.Result != res {
|
||||
t.Error(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheableMethodsMatcher_Match(b *testing.B) {
|
||||
matcher := NewCacheableMethodMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = Match(c.Method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRuntimeMatcher_Match(b *testing.B) {
|
||||
matcher := NewRuntimeMethodMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = Match(c.Method)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/integration-system/isp-lib/metric"
|
||||
"github.com/rcrowley/go-metrics"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultSampleSize = 2048
|
||||
)
|
||||
|
||||
var Metrics = metricService{mh: nil}
|
||||
|
||||
type (
|
||||
metricService struct {
|
||||
mh *metricHolder
|
||||
}
|
||||
|
||||
metricHolder struct {
|
||||
methodHistograms map[string]metrics.Histogram
|
||||
methodLock sync.RWMutex
|
||||
statusCounters map[int]metrics.Counter
|
||||
statusLock sync.RWMutex
|
||||
routerResponseTime metrics.Histogram
|
||||
responseTime metrics.Histogram
|
||||
}
|
||||
)
|
||||
|
||||
func (m metricService) Init() {
|
||||
if m.mh == nil {
|
||||
m.mh = &metricHolder{
|
||||
methodHistograms: make(map[string]metrics.Histogram),
|
||||
statusCounters: make(map[int]metrics.Counter),
|
||||
responseTime: metrics.GetOrRegisterHistogram(
|
||||
"http.response.time", metric.GetRegistry(), metrics.NewUniformSample(defaultSampleSize),
|
||||
),
|
||||
routerResponseTime: metrics.GetOrRegisterHistogram(
|
||||
"grpc.router.response.time", metric.GetRegistry(), metrics.NewUniformSample(defaultSampleSize),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m metricService) UpdateMethodResponseTime(uri string, time time.Duration) {
|
||||
m.getOrRegisterHistogram(uri).Update(int64(time))
|
||||
}
|
||||
|
||||
func (m metricService) UpdateResponseTime(time time.Duration) {
|
||||
m.mh.responseTime.Update(int64(time))
|
||||
}
|
||||
|
||||
func (m metricService) UpdateRouterResponseTime(time time.Duration) {
|
||||
m.mh.routerResponseTime.Update(int64(time))
|
||||
}
|
||||
|
||||
func (m metricService) UpdateStatusCounter(status int) {
|
||||
m.getOrRegisterCounter(status).Inc(1)
|
||||
}
|
||||
|
||||
func (m metricService) getOrRegisterHistogram(uri string) metrics.Histogram {
|
||||
m.mh.methodLock.RLock()
|
||||
histogram, ok := m.mh.methodHistograms[uri]
|
||||
m.mh.methodLock.RUnlock()
|
||||
if ok {
|
||||
return histogram
|
||||
}
|
||||
|
||||
m.mh.methodLock.Lock()
|
||||
defer m.mh.methodLock.Unlock()
|
||||
if d, ok := m.mh.methodHistograms[uri]; ok {
|
||||
return d
|
||||
}
|
||||
histogram = metrics.GetOrRegisterHistogram(
|
||||
"http.response.time_"+uri,
|
||||
metric.GetRegistry(),
|
||||
metrics.NewUniformSample(defaultSampleSize),
|
||||
)
|
||||
m.mh.methodHistograms[uri] = histogram
|
||||
return histogram
|
||||
}
|
||||
|
||||
func (m metricService) getOrRegisterCounter(status int) metrics.Counter {
|
||||
m.mh.statusLock.RLock()
|
||||
d, ok := m.mh.statusCounters[status]
|
||||
m.mh.statusLock.RUnlock()
|
||||
if ok {
|
||||
return d
|
||||
}
|
||||
|
||||
m.mh.statusLock.Lock()
|
||||
defer m.mh.statusLock.Unlock()
|
||||
if d, ok := m.mh.statusCounters[status]; ok {
|
||||
return d
|
||||
}
|
||||
d = metrics.GetOrRegisterCounter("http.response.count."+strconv.Itoa(status), metric.GetRegistry())
|
||||
m.mh.statusCounters[status] = d
|
||||
return d
|
||||
}
|
||||
Reference in new issue
Block a user