blob: a305aa53180b9dab323169bf8c92c682e17f653e [file] [log] [blame]
khenaidoo26721882021-08-11 17:42:52 -04001// 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
15package mvcc
16
17import (
18 "github.com/coreos/etcd/lease"
19)
20
21type metricsTxnWrite struct {
22 TxnWrite
23 ranges uint
24 puts uint
25 deletes uint
26 putSize int64
27}
28
29func newMetricsTxnRead(tr TxnRead) TxnRead {
30 return &metricsTxnWrite{&txnReadWrite{tr}, 0, 0, 0, 0}
31}
32
33func newMetricsTxnWrite(tw TxnWrite) TxnWrite {
34 return &metricsTxnWrite{tw, 0, 0, 0, 0}
35}
36
37func (tw *metricsTxnWrite) Range(key, end []byte, ro RangeOptions) (*RangeResult, error) {
38 tw.ranges++
39 return tw.TxnWrite.Range(key, end, ro)
40}
41
42func (tw *metricsTxnWrite) DeleteRange(key, end []byte) (n, rev int64) {
43 tw.deletes++
44 return tw.TxnWrite.DeleteRange(key, end)
45}
46
47func (tw *metricsTxnWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) {
48 tw.puts++
49 size := int64(len(key) + len(value))
50 tw.putSize += size
51 return tw.TxnWrite.Put(key, value, lease)
52}
53
54func (tw *metricsTxnWrite) End() {
55 defer tw.TxnWrite.End()
56 if sum := tw.ranges + tw.puts + tw.deletes; sum > 1 {
57 txnCounter.Inc()
58 }
59 rangeCounter.Add(float64(tw.ranges))
60 putCounter.Add(float64(tw.puts))
61 totalPutSizeGauge.Add(float64(tw.putSize))
62 deleteCounter.Add(float64(tw.deletes))
63}