github.com/lzy4123/fabric@v2.1.1+incompatible/orderer/common/cluster/metrics.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package cluster 8 9 import ( 10 "time" 11 12 "github.com/hyperledger/fabric/common/metrics" 13 ) 14 15 var ( 16 EgressQueueLengthOpts = metrics.GaugeOpts{ 17 Namespace: "cluster", 18 Subsystem: "comm", 19 Name: "egress_queue_length", 20 Help: "Length of the egress queue.", 21 LabelNames: []string{"host", "msg_type", "channel"}, 22 StatsdFormat: "%{#fqname}.%{host}.%{msg_type}.%{channel}", 23 } 24 25 EgressQueueCapacityOpts = metrics.GaugeOpts{ 26 Namespace: "cluster", 27 Subsystem: "comm", 28 Name: "egress_queue_capacity", 29 Help: "Capacity of the egress queue.", 30 LabelNames: []string{"host", "msg_type", "channel"}, 31 StatsdFormat: "%{#fqname}.%{host}.%{msg_type}.%{channel}", 32 } 33 34 EgressWorkersOpts = metrics.GaugeOpts{ 35 Namespace: "cluster", 36 Subsystem: "comm", 37 Name: "egress_queue_workers", 38 Help: "Count of egress queue workers.", 39 LabelNames: []string{"channel"}, 40 StatsdFormat: "%{#fqname}.%{channel}", 41 } 42 43 IngressStreamsCountOpts = metrics.GaugeOpts{ 44 Namespace: "cluster", 45 Subsystem: "comm", 46 Name: "ingress_stream_count", 47 Help: "Count of streams from other nodes.", 48 StatsdFormat: "%{#fqname}", 49 } 50 51 EgressStreamsCountOpts = metrics.GaugeOpts{ 52 Namespace: "cluster", 53 Subsystem: "comm", 54 Name: "egress_stream_count", 55 Help: "Count of streams to other nodes.", 56 LabelNames: []string{"channel"}, 57 StatsdFormat: "%{#fqname}.%{channel}", 58 } 59 60 EgressTLSConnectionCountOpts = metrics.GaugeOpts{ 61 Namespace: "cluster", 62 Subsystem: "comm", 63 Name: "egress_tls_connection_count", 64 Help: "Count of TLS connections to other nodes.", 65 StatsdFormat: "%{#fqname}", 66 } 67 68 MessageSendTimeOpts = metrics.HistogramOpts{ 69 Namespace: "cluster", 70 Subsystem: "comm", 71 Name: "msg_send_time", 72 Help: "The time it takes to send a message in seconds.", 73 LabelNames: []string{"host", "channel"}, 74 StatsdFormat: "%{#fqname}.%{host}.%{channel}", 75 } 76 77 MessagesDroppedCountOpts = metrics.CounterOpts{ 78 Namespace: "cluster", 79 Subsystem: "comm", 80 Name: "msg_dropped_count", 81 Help: "Count of messages dropped.", 82 LabelNames: []string{"host", "channel"}, 83 StatsdFormat: "%{#fqname}.%{host}.%{channel}", 84 } 85 ) 86 87 // Metrics defines the metrics for the cluster. 88 type Metrics struct { 89 EgressQueueLength metrics.Gauge 90 EgressQueueCapacity metrics.Gauge 91 EgressWorkerCount metrics.Gauge 92 IngressStreamsCount metrics.Gauge 93 EgressStreamsCount metrics.Gauge 94 EgressTLSConnectionCount metrics.Gauge 95 MessageSendTime metrics.Histogram 96 MessagesDroppedCount metrics.Counter 97 } 98 99 // A MetricsProvider is an abstraction for a metrics provider. It is a factory for 100 // Counter, Gauge, and Histogram meters. 101 type MetricsProvider interface { 102 // NewCounter creates a new instance of a Counter. 103 NewCounter(opts metrics.CounterOpts) metrics.Counter 104 // NewGauge creates a new instance of a Gauge. 105 NewGauge(opts metrics.GaugeOpts) metrics.Gauge 106 // NewHistogram creates a new instance of a Histogram. 107 NewHistogram(opts metrics.HistogramOpts) metrics.Histogram 108 } 109 110 //go:generate mockery -dir . -name MetricsProvider -case underscore -output ./mocks/ 111 112 // NewMetrics initializes new metrics for the cluster infrastructure. 113 func NewMetrics(provider MetricsProvider) *Metrics { 114 return &Metrics{ 115 EgressQueueLength: provider.NewGauge(EgressQueueLengthOpts), 116 EgressQueueCapacity: provider.NewGauge(EgressQueueCapacityOpts), 117 EgressStreamsCount: provider.NewGauge(EgressStreamsCountOpts), 118 EgressTLSConnectionCount: provider.NewGauge(EgressTLSConnectionCountOpts), 119 EgressWorkerCount: provider.NewGauge(EgressWorkersOpts), 120 IngressStreamsCount: provider.NewGauge(IngressStreamsCountOpts), 121 MessagesDroppedCount: provider.NewCounter(MessagesDroppedCountOpts), 122 MessageSendTime: provider.NewHistogram(MessageSendTimeOpts), 123 } 124 } 125 126 func (m *Metrics) reportMessagesDropped(host, channel string) { 127 m.MessagesDroppedCount.With("host", host, "channel", channel).Add(1) 128 } 129 130 func (m *Metrics) reportQueueOccupancy(host string, msgType string, channel string, length, capacity int) { 131 m.EgressQueueLength.With("host", host, "msg_type", msgType, "channel", channel).Set(float64(length)) 132 m.EgressQueueCapacity.With("host", host, "msg_type", msgType, "channel", channel).Set(float64(capacity)) 133 } 134 135 func (m *Metrics) reportWorkerCount(channel string, count uint32) { 136 m.EgressWorkerCount.With("channel", channel).Set(float64(count)) 137 } 138 139 func (m *Metrics) reportMsgSendTime(host string, channel string, duration time.Duration) { 140 m.MessageSendTime.With("host", host, "channel", channel).Observe(float64(duration.Seconds())) 141 } 142 143 func (m *Metrics) reportEgressStreamCount(channel string, count uint32) { 144 m.EgressStreamsCount.With("channel", channel).Set(float64(count)) 145 } 146 147 func (m *Metrics) reportStreamCount(count uint32) { 148 m.IngressStreamsCount.Set(float64(count)) 149 }