metrics_txn.go 1.6 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 (
  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. if sum > 1 {
  46. txnCounter.Inc()
  47. }
  48. return
  49. }
  50. switch {
  51. case tw.ranges == 1:
  52. rangeCounter.Inc()
  53. case tw.puts == 1:
  54. putCounter.Inc()
  55. case tw.deletes == 1:
  56. deleteCounter.Inc()
  57. }
  58. }