options.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package mon
  2. import (
  3. "time"
  4. "github.com/wuntsong-org/go-zero-plus/core/syncx"
  5. mopt "go.mongodb.org/mongo-driver/mongo/options"
  6. )
  7. const defaultTimeout = time.Second * 3
  8. var (
  9. slowThreshold = syncx.ForAtomicDuration(defaultSlowThreshold)
  10. logMon = syncx.ForAtomicBool(true)
  11. logSlowMon = syncx.ForAtomicBool(true)
  12. )
  13. type (
  14. options = mopt.ClientOptions
  15. // Option defines the method to customize a mongo model.
  16. Option func(opts *options)
  17. )
  18. // DisableLog disables logging of mongo commands, includes info and slow logs.
  19. func DisableLog() {
  20. logMon.Set(false)
  21. logSlowMon.Set(false)
  22. }
  23. // DisableInfoLog disables info logging of mongo commands, but keeps slow logs.
  24. func DisableInfoLog() {
  25. logMon.Set(false)
  26. }
  27. // SetSlowThreshold sets the slow threshold.
  28. func SetSlowThreshold(threshold time.Duration) {
  29. slowThreshold.Set(threshold)
  30. }
  31. func defaultTimeoutOption() Option {
  32. return func(opts *options) {
  33. opts.SetTimeout(defaultTimeout)
  34. }
  35. }
  36. // WithTimeout set the mon client operation timeout.
  37. func WithTimeout(timeout time.Duration) Option {
  38. return func(opts *options) {
  39. opts.SetTimeout(timeout)
  40. }
  41. }