metrics.go 4.96 KB
Newer Older
yacovm's avatar
yacovm committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package cluster

import (
	"time"

	"github.com/hyperledger/fabric/common/metrics"
)

var (
	EgressQueueLengthOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "egress_queue_length",
20
		Help:         "Length of the egress queue.",
yacovm's avatar
yacovm committed
21
22
23
24
25
26
27
28
		LabelNames:   []string{"host", "msg_type", "channel"},
		StatsdFormat: "%{#fqname}.%{host}.%{msg_type}.%{channel}",
	}

	EgressQueueCapacityOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "egress_queue_capacity",
29
		Help:         "Capacity of the egress queue.",
yacovm's avatar
yacovm committed
30
31
32
33
34
35
36
37
		LabelNames:   []string{"host", "msg_type", "channel"},
		StatsdFormat: "%{#fqname}.%{host}.%{msg_type}.%{channel}",
	}

	EgressWorkersOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "egress_queue_workers",
38
		Help:         "Count of egress queue workers.",
yacovm's avatar
yacovm committed
39
40
41
42
43
44
45
46
		LabelNames:   []string{"channel"},
		StatsdFormat: "%{#fqname}.%{channel}",
	}

	IngressStreamsCountOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "ingress_stream_count",
47
		Help:         "Count of streams from other nodes.",
yacovm's avatar
yacovm committed
48
49
50
51
52
53
54
		StatsdFormat: "%{#fqname}",
	}

	EgressStreamsCountOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "egress_stream_count",
55
		Help:         "Count of streams to other nodes.",
yacovm's avatar
yacovm committed
56
57
58
59
60
61
62
63
		LabelNames:   []string{"channel"},
		StatsdFormat: "%{#fqname}.%{channel}",
	}

	EgressTLSConnectionCountOpts = metrics.GaugeOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "egress_tls_connection_count",
64
		Help:         "Count of TLS connections to other nodes.",
yacovm's avatar
yacovm committed
65
66
67
68
69
70
71
		StatsdFormat: "%{#fqname}",
	}

	MessageSendTimeOpts = metrics.HistogramOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "msg_send_time",
72
		Help:         "The time it takes to send a message in seconds.",
yacovm's avatar
yacovm committed
73
74
75
76
77
78
79
80
		LabelNames:   []string{"host", "channel"},
		StatsdFormat: "%{#fqname}.%{host}.%{channel}",
	}

	MessagesDroppedCountOpts = metrics.CounterOpts{
		Namespace:    "cluster",
		Subsystem:    "comm",
		Name:         "msg_dropped_count",
81
		Help:         "Count of messages dropped.",
yacovm's avatar
yacovm committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
		LabelNames:   []string{"host", "channel"},
		StatsdFormat: "%{#fqname}.%{host}.%{channel}",
	}
)

// Metrics defines the metrics for the cluster.
type Metrics struct {
	EgressQueueLength        metrics.Gauge
	EgressQueueCapacity      metrics.Gauge
	EgressWorkerCount        metrics.Gauge
	IngressStreamsCount      metrics.Gauge
	EgressStreamsCount       metrics.Gauge
	EgressTLSConnectionCount metrics.Gauge
	MessageSendTime          metrics.Histogram
	MessagesDroppedCount     metrics.Counter
}

// A MetricsProvider is an abstraction for a metrics provider. It is a factory for
// Counter, Gauge, and Histogram meters.
type MetricsProvider interface {
	// NewCounter creates a new instance of a Counter.
	NewCounter(opts metrics.CounterOpts) metrics.Counter
	// NewGauge creates a new instance of a Gauge.
	NewGauge(opts metrics.GaugeOpts) metrics.Gauge
	// NewHistogram creates a new instance of a Histogram.
	NewHistogram(opts metrics.HistogramOpts) metrics.Histogram
}

//go:generate mockery -dir . -name MetricsProvider -case underscore -output ./mocks/

// NewMetrics initializes new metrics for the cluster infrastructure.
func NewMetrics(provider MetricsProvider) *Metrics {
	return &Metrics{
		EgressQueueLength:        provider.NewGauge(EgressQueueLengthOpts),
		EgressQueueCapacity:      provider.NewGauge(EgressQueueCapacityOpts),
		EgressStreamsCount:       provider.NewGauge(EgressStreamsCountOpts),
		EgressTLSConnectionCount: provider.NewGauge(EgressTLSConnectionCountOpts),
		EgressWorkerCount:        provider.NewGauge(EgressWorkersOpts),
		IngressStreamsCount:      provider.NewGauge(IngressStreamsCountOpts),
		MessagesDroppedCount:     provider.NewCounter(MessagesDroppedCountOpts),
		MessageSendTime:          provider.NewHistogram(MessageSendTimeOpts),
	}
}

func (m *Metrics) reportMessagesDropped(host, channel string) {
	m.MessagesDroppedCount.With("host", host, "channel", channel).Add(1)
}

func (m *Metrics) reportQueueOccupancy(host string, msgType string, channel string, length, capacity int) {
	m.EgressQueueLength.With("host", host, "msg_type", msgType, "channel", channel).Set(float64(length))
	m.EgressQueueCapacity.With("host", host, "msg_type", msgType, "channel", channel).Set(float64(capacity))
}

func (m *Metrics) reportWorkerCount(channel string, count uint32) {
	m.EgressWorkerCount.With("channel", channel).Set(float64(count))
}

func (m *Metrics) reportMsgSendTime(host string, channel string, duration time.Duration) {
140
	m.MessageSendTime.With("host", host, "channel", channel).Observe(float64(duration.Seconds()))
yacovm's avatar
yacovm committed
141
142
143
144
145
146
147
148
149
}

func (m *Metrics) reportEgressStreamCount(channel string, count uint32) {
	m.EgressStreamsCount.With("channel", channel).Set(float64(count))
}

func (m *Metrics) reportStreamCount(count uint32) {
	m.IngressStreamsCount.Set(float64(count))
}