github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/metrics.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"time"
    11  
    12  	"github.com/hyperledger/fabric/common/metrics"
    13  	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/txmgr"
    14  )
    15  
    16  type stats struct {
    17  	blockProcessingTime            metrics.Histogram
    18  	blockAndPvtdataStoreCommitTime metrics.Histogram
    19  	statedbCommitTime              metrics.Histogram
    20  	transactionsCount              metrics.Counter
    21  }
    22  
    23  func newStats(metricsProvider metrics.Provider) *stats {
    24  	stats := &stats{}
    25  	stats.blockProcessingTime = metricsProvider.NewHistogram(blockProcessingTimeOpts)
    26  	stats.blockAndPvtdataStoreCommitTime = metricsProvider.NewHistogram(blockAndPvtdataStoreCommitTimeOpts)
    27  	stats.statedbCommitTime = metricsProvider.NewHistogram(statedbCommitTimeOpts)
    28  	stats.transactionsCount = metricsProvider.NewCounter(transactionCountOpts)
    29  	return stats
    30  }
    31  
    32  type ledgerStats struct {
    33  	stats    *stats
    34  	ledgerid string
    35  }
    36  
    37  func (s *stats) ledgerStats(ledgerid string) *ledgerStats {
    38  	return &ledgerStats{
    39  		s, ledgerid,
    40  	}
    41  }
    42  
    43  func (s *ledgerStats) updateBlockProcessingTime(timeTaken time.Duration) {
    44  	s.stats.blockProcessingTime.With("channel", s.ledgerid).Observe(timeTaken.Seconds())
    45  }
    46  
    47  func (s *ledgerStats) updateBlockstorageAndPvtdataCommitTime(timeTaken time.Duration) {
    48  	s.stats.blockAndPvtdataStoreCommitTime.With("channel", s.ledgerid).Observe(timeTaken.Seconds())
    49  }
    50  
    51  func (s *ledgerStats) updateStatedbCommitTime(timeTaken time.Duration) {
    52  	s.stats.statedbCommitTime.With("channel", s.ledgerid).Observe(timeTaken.Seconds())
    53  }
    54  
    55  func (s *ledgerStats) updateTransactionsStats(
    56  	txstatsInfo []*txmgr.TxStatInfo,
    57  ) {
    58  	for _, txstat := range txstatsInfo {
    59  		transactionTypeStr := "unknown"
    60  		if txstat.TxType != -1 {
    61  			transactionTypeStr = txstat.TxType.String()
    62  		}
    63  
    64  		chaincodeName := "unknown"
    65  		if txstat.ChaincodeID != nil {
    66  			chaincodeName = txstat.ChaincodeID.Name + ":" + txstat.ChaincodeID.Version
    67  		}
    68  
    69  		s.stats.transactionsCount.With(
    70  			"channel", s.ledgerid,
    71  			"transaction_type", transactionTypeStr,
    72  			"chaincode", chaincodeName,
    73  			"validation_code", txstat.ValidationCode.String(),
    74  		).Add(1)
    75  	}
    76  }
    77  
    78  var (
    79  	blockProcessingTimeOpts = metrics.HistogramOpts{
    80  		Namespace:    "ledger",
    81  		Subsystem:    "",
    82  		Name:         "block_processing_time",
    83  		Help:         "Time taken in seconds for ledger block processing.",
    84  		LabelNames:   []string{"channel"},
    85  		StatsdFormat: "%{#fqname}.%{channel}",
    86  		Buckets:      []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10},
    87  	}
    88  
    89  	blockAndPvtdataStoreCommitTimeOpts = metrics.HistogramOpts{
    90  		Namespace:    "ledger",
    91  		Subsystem:    "",
    92  		Name:         "blockstorage_and_pvtdata_commit_time",
    93  		Help:         "Time taken in seconds for committing the block and private data to storage.",
    94  		LabelNames:   []string{"channel"},
    95  		StatsdFormat: "%{#fqname}.%{channel}",
    96  		Buckets:      []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10},
    97  	}
    98  
    99  	statedbCommitTimeOpts = metrics.HistogramOpts{
   100  		Namespace:    "ledger",
   101  		Subsystem:    "",
   102  		Name:         "statedb_commit_time",
   103  		Help:         "Time taken in seconds for committing block changes to state db.",
   104  		LabelNames:   []string{"channel"},
   105  		StatsdFormat: "%{#fqname}.%{channel}",
   106  		Buckets:      []float64{0.005, 0.01, 0.015, 0.05, 0.1, 1, 10},
   107  	}
   108  
   109  	transactionCountOpts = metrics.CounterOpts{
   110  		Namespace:    "ledger",
   111  		Subsystem:    "",
   112  		Name:         "transaction_count",
   113  		Help:         "Number of transactions processed.",
   114  		LabelNames:   []string{"channel", "transaction_type", "chaincode", "validation_code"},
   115  		StatsdFormat: "%{#fqname}.%{channel}.%{transaction_type}.%{chaincode}.%{validation_code}",
   116  	}
   117  )