feat: slow threshold customizable in mongo (#1186)
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/timex"
|
"github.com/tal-tech/go-zero/core/timex"
|
||||||
)
|
)
|
||||||
|
|
||||||
const slowThreshold = time.Millisecond * 500
|
const defaultSlowThreshold = time.Millisecond * 500
|
||||||
|
|
||||||
// ErrNotFound is an alias of mgo.ErrNotFound.
|
// ErrNotFound is an alias of mgo.ErrNotFound.
|
||||||
var ErrNotFound = mgo.ErrNotFound
|
var ErrNotFound = mgo.ErrNotFound
|
||||||
@@ -203,7 +203,7 @@ func (c *decoratedCollection) logDuration(method string, duration time.Duration,
|
|||||||
if e != nil {
|
if e != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - fail(%s) - %s",
|
logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - fail(%s) - %s",
|
||||||
c.name, method, err.Error(), string(content))
|
c.name, method, err.Error(), string(content))
|
||||||
} else {
|
} else {
|
||||||
@@ -211,7 +211,7 @@ func (c *decoratedCollection) logDuration(method string, duration time.Duration,
|
|||||||
c.name, method, err.Error(), string(content))
|
c.name, method, err.Error(), string(content))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if duration > slowThreshold {
|
if duration > slowThreshold.Load() {
|
||||||
logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - ok - %s",
|
logx.WithDuration(duration).Slowf("[MONGO] mongo(%s) - slowcall - %s - ok - %s",
|
||||||
c.name, method, string(content))
|
c.name, method, string(content))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -8,23 +8,14 @@ import (
|
|||||||
"github.com/tal-tech/go-zero/core/breaker"
|
"github.com/tal-tech/go-zero/core/breaker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
// A Model is a mongo model.
|
||||||
options struct {
|
type Model struct {
|
||||||
timeout time.Duration
|
session *concurrentSession
|
||||||
}
|
db *mgo.Database
|
||||||
|
collection string
|
||||||
// Option defines the method to customize a mongo model.
|
brk breaker.Breaker
|
||||||
Option func(opts *options)
|
opts []Option
|
||||||
|
}
|
||||||
// A Model is a mongo model.
|
|
||||||
Model struct {
|
|
||||||
session *concurrentSession
|
|
||||||
db *mgo.Database
|
|
||||||
collection string
|
|
||||||
brk breaker.Breaker
|
|
||||||
opts []Option
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// MustNewModel returns a Model, exits on errors.
|
// MustNewModel returns a Model, exits on errors.
|
||||||
func MustNewModel(url, collection string, opts ...Option) *Model {
|
func MustNewModel(url, collection string, opts ...Option) *Model {
|
||||||
|
|||||||
14
core/stores/mongo/model_test.go
Normal file
14
core/stores/mongo/model_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package mongo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWithTimeout(t *testing.T) {
|
||||||
|
o := defaultOptions()
|
||||||
|
WithTimeout(time.Second)(o)
|
||||||
|
assert.Equal(t, time.Second, o.timeout)
|
||||||
|
}
|
||||||
28
core/stores/mongo/options.go
Normal file
28
core/stores/mongo/options.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package mongo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/tal-tech/go-zero/core/syncx"
|
||||||
|
)
|
||||||
|
|
||||||
|
var slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
|
||||||
|
|
||||||
|
type (
|
||||||
|
options struct {
|
||||||
|
timeout time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Option defines the method to customize a mongo model.
|
||||||
|
Option func(opts *options)
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetSlowThreshold(threshold time.Duration) {
|
||||||
|
slowThreshold.Set(threshold)
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultOptions() *options {
|
||||||
|
return &options{
|
||||||
|
timeout: defaultTimeout,
|
||||||
|
}
|
||||||
|
}
|
||||||
14
core/stores/mongo/options_test.go
Normal file
14
core/stores/mongo/options_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package mongo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetSlowThreshold(t *testing.T) {
|
||||||
|
assert.Equal(t, defaultSlowThreshold, slowThreshold.Load())
|
||||||
|
SetSlowThreshold(time.Second)
|
||||||
|
assert.Equal(t, time.Second, slowThreshold.Load())
|
||||||
|
}
|
||||||
@@ -57,9 +57,7 @@ func (cs *concurrentSession) putSession(session *mgo.Session) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (cs *concurrentSession) takeSession(opts ...Option) (*mgo.Session, error) {
|
func (cs *concurrentSession) takeSession(opts ...Option) (*mgo.Session, error) {
|
||||||
o := &options{
|
o := defaultOptions()
|
||||||
timeout: defaultTimeout,
|
|
||||||
}
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
opt(o)
|
opt(o)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user