...

Source file src/go.etcd.io/etcd/server/v3/mvcc/metrics_txn.go

Documentation: go.etcd.io/etcd/server/v3/mvcc

     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  
    15  package mvcc
    16  
    17  import (
    18  	"context"
    19  
    20  	"go.etcd.io/etcd/server/v3/lease"
    21  )
    22  
    23  type metricsTxnWrite struct {
    24  	TxnWrite
    25  	ranges  uint
    26  	puts    uint
    27  	deletes uint
    28  	putSize int64
    29  }
    30  
    31  func newMetricsTxnRead(tr TxnRead) TxnRead {
    32  	return &metricsTxnWrite{&txnReadWrite{tr}, 0, 0, 0, 0}
    33  }
    34  
    35  func newMetricsTxnWrite(tw TxnWrite) TxnWrite {
    36  	return &metricsTxnWrite{tw, 0, 0, 0, 0}
    37  }
    38  
    39  func (tw *metricsTxnWrite) Range(ctx context.Context, key, end []byte, ro RangeOptions) (*RangeResult, error) {
    40  	tw.ranges++
    41  	return tw.TxnWrite.Range(ctx, key, end, ro)
    42  }
    43  
    44  func (tw *metricsTxnWrite) DeleteRange(key, end []byte) (n, rev int64) {
    45  	tw.deletes++
    46  	return tw.TxnWrite.DeleteRange(key, end)
    47  }
    48  
    49  func (tw *metricsTxnWrite) Put(key, value []byte, lease lease.LeaseID) (rev int64) {
    50  	tw.puts++
    51  	size := int64(len(key) + len(value))
    52  	tw.putSize += size
    53  	return tw.TxnWrite.Put(key, value, lease)
    54  }
    55  
    56  func (tw *metricsTxnWrite) End() {
    57  	defer tw.TxnWrite.End()
    58  	if sum := tw.ranges + tw.puts + tw.deletes; sum > 1 {
    59  		txnCounter.Inc()
    60  	}
    61  
    62  	ranges := float64(tw.ranges)
    63  	rangeCounter.Add(ranges)
    64  	rangeCounterDebug.Add(ranges) // TODO: remove in 3.5 release
    65  
    66  	puts := float64(tw.puts)
    67  	putCounter.Add(puts)
    68  	totalPutSizeGauge.Add(float64(tw.putSize))
    69  
    70  	deletes := float64(tw.deletes)
    71  	deleteCounter.Add(deletes)
    72  }
    73  

View as plain text