metrics_txn.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mvcc
  15. import "go.etcd.io/etcd/lease"
  16. type metricsTxnWrite struct {
  17. TxnWrite
  18. ranges uint
  19. puts uint
  20. deletes uint
  21. }
  22. func newMetricsTxnRead(tr TxnRead) TxnRead {
  23. return &metricsTxnWrite{&txnReadWrite{tr}, 0, 0, 0}
  24. }
  25. func newMetricsTxnWrite(tw TxnWrite) TxnWrite {
  26. return &metricsTxnWrite{tw, 0, 0, 0}
  27. }
  28. func (tw *metricsTxnWrite) Range(key, end []byte, ro RangeOptions) (*RangeResult, error) {
  29. tw.ranges++
  30. return tw.TxnWrite.Range(key, end, ro)
  31. }
  32. func (tw *metricsTxnWrite) DeleteRange(key, end []byte) (n, rev int64) {
  33. tw.deletes++
  34. return tw.TxnWrite.DeleteRange(key, end)
  35. }
  36. func (tw *metricsTxnWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) {
  37. tw.puts++
  38. return tw.TxnWrite.Put(key, value, lease)
  39. }
  40. func (tw *metricsTxnWrite) End() {
  41. defer tw.TxnWrite.End()
  42. if sum := tw.ranges + tw.puts + tw.deletes; sum > 1 {
  43. txnCounter.Inc()
  44. txnCounterDebug.Inc() // TODO: remove in 3.5 release
  45. }
  46. ranges := float64(tw.ranges)
  47. rangeCounter.Add(ranges)
  48. rangeCounterDebug.Add(ranges) // TODO: remove in 3.5 release
  49. puts := float64(tw.puts)
  50. putCounter.Add(puts)
  51. putCounterDebug.Add(puts) // TODO: remove in 3.5 release
  52. deletes := float64(tw.deletes)
  53. deleteCounter.Add(deletes)
  54. deleteCounterDebug.Add(deletes) // TODO: remove in 3.5 release
  55. }