metrics_txn.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 (
  16. "github.com/coreos/etcd/lease"
  17. )
  18. type metricsTxnWrite struct {
  19. TxnWrite
  20. ranges uint
  21. puts uint
  22. deletes uint
  23. }
  24. func newMetricsTxnRead(tr TxnRead) TxnRead {
  25. return &metricsTxnWrite{&txnReadWrite{tr}, 0, 0, 0}
  26. }
  27. func newMetricsTxnWrite(tw TxnWrite) TxnWrite {
  28. return &metricsTxnWrite{tw, 0, 0, 0}
  29. }
  30. func (tw *metricsTxnWrite) Range(key, end []byte, ro RangeOptions) (*RangeResult, error) {
  31. tw.ranges++
  32. return tw.TxnWrite.Range(key, end, ro)
  33. }
  34. func (tw *metricsTxnWrite) DeleteRange(key, end []byte) (n, rev int64) {
  35. tw.deletes++
  36. return tw.TxnWrite.DeleteRange(key, end)
  37. }
  38. func (tw *metricsTxnWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) {
  39. tw.puts++
  40. return tw.TxnWrite.Put(key, value, lease)
  41. }
  42. func (tw *metricsTxnWrite) End() {
  43. defer tw.TxnWrite.End()
  44. if sum := tw.ranges + tw.puts + tw.deletes; sum > 1 {
  45. txnCounter.Inc()
  46. }
  47. rangeCounter.Add(float64(tw.ranges))
  48. putCounter.Add(float64(tw.puts))
  49. deleteCounter.Add(float64(tw.deletes))
  50. }