github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/common/multichannel/metrics.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package multichannel
     8  
     9  import (
    10  	"github.com/hechain20/hechain/common/metrics"
    11  	"github.com/hechain20/hechain/orderer/common/types"
    12  )
    13  
    14  var (
    15  	statusOpts = metrics.GaugeOpts{
    16  		Namespace:    "participation",
    17  		Subsystem:    "",
    18  		Name:         "status",
    19  		Help:         "The channel participation status of the node: 0 if inactive, 1 if active, 2 if onboarding, 3 if failed.",
    20  		LabelNames:   []string{"channel"},
    21  		StatsdFormat: "%{#fqname}.%{channel}",
    22  	}
    23  
    24  	consensusRelationOpts = metrics.GaugeOpts{
    25  		Namespace:    "participation",
    26  		Subsystem:    "",
    27  		Name:         "consensus_relation",
    28  		Help:         "The channel participation consensus relation of the node: 0 if other, 1 if consenter, 2 if follower, 3 if config-tracker.",
    29  		LabelNames:   []string{"channel"},
    30  		StatsdFormat: "%{#fqname}.%{channel}",
    31  	}
    32  )
    33  
    34  // Metrics defines the metrics for the cluster.
    35  type Metrics struct {
    36  	Status            metrics.Gauge
    37  	ConsensusRelation metrics.Gauge
    38  }
    39  
    40  // A MetricsProvider is an abstraction for a metrics provider. It is a factory for
    41  // Counter, Gauge, and Histogram meters.
    42  type MetricsProvider interface {
    43  	// NewCounter creates a new instance of a Counter.
    44  	NewCounter(opts metrics.CounterOpts) metrics.Counter
    45  	// NewGauge creates a new instance of a Gauge.
    46  	NewGauge(opts metrics.GaugeOpts) metrics.Gauge
    47  	// NewHistogram creates a new instance of a Histogram.
    48  	NewHistogram(opts metrics.HistogramOpts) metrics.Histogram
    49  }
    50  
    51  //go:generate mockery -dir . -name MetricsProvider -case underscore -output ./mocks/
    52  
    53  // NewMetrics initializes new metrics for the channel participation API.
    54  func NewMetrics(m MetricsProvider) *Metrics {
    55  	return &Metrics{
    56  		Status:            m.NewGauge(statusOpts),
    57  		ConsensusRelation: m.NewGauge(consensusRelationOpts),
    58  	}
    59  }
    60  
    61  func (m *Metrics) reportStatus(channel string, status types.Status) {
    62  	var s int
    63  	switch status {
    64  	case types.StatusInactive:
    65  		s = 0
    66  	case types.StatusActive:
    67  		s = 1
    68  	case types.StatusOnBoarding:
    69  		s = 2
    70  	case types.StatusFailed:
    71  		s = 3
    72  	default:
    73  		logger.Panicf("Programming error: unexpected status %s", status)
    74  	}
    75  	m.Status.With("channel", channel).Set(float64(s))
    76  }
    77  
    78  func (m *Metrics) reportConsensusRelation(channel string, relation types.ConsensusRelation) {
    79  	var r int
    80  	switch relation {
    81  	case types.ConsensusRelationOther:
    82  		r = 0
    83  	case types.ConsensusRelationConsenter:
    84  		r = 1
    85  	case types.ConsensusRelationFollower:
    86  		r = 2
    87  	case types.ConsensusRelationConfigTracker:
    88  		r = 3
    89  	default:
    90  		logger.Panicf("Programming error: unexpected relation %s", relation)
    91  
    92  	}
    93  	m.ConsensusRelation.With("channel", channel).Set(float64(r))
    94  }