Skip to content

Commit

Permalink
feat: add protocol-requests-rate metric (#2373)
Browse files Browse the repository at this point in the history
  • Loading branch information
auntan authored Nov 15, 2022
1 parent b436424 commit 978aadd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 42 deletions.
63 changes: 43 additions & 20 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,28 @@ type Broker struct {
responses chan *responsePromise
done chan bool

metricRegistry metrics.Registry
incomingByteRate metrics.Meter
requestRate metrics.Meter
fetchRate metrics.Meter
requestSize metrics.Histogram
requestLatency metrics.Histogram
outgoingByteRate metrics.Meter
responseRate metrics.Meter
responseSize metrics.Histogram
requestsInFlight metrics.Counter
brokerIncomingByteRate metrics.Meter
brokerRequestRate metrics.Meter
brokerFetchRate metrics.Meter
brokerRequestSize metrics.Histogram
brokerRequestLatency metrics.Histogram
brokerOutgoingByteRate metrics.Meter
brokerResponseRate metrics.Meter
brokerResponseSize metrics.Histogram
brokerRequestsInFlight metrics.Counter
brokerThrottleTime metrics.Histogram
metricRegistry metrics.Registry
incomingByteRate metrics.Meter
requestRate metrics.Meter
fetchRate metrics.Meter
requestSize metrics.Histogram
requestLatency metrics.Histogram
outgoingByteRate metrics.Meter
responseRate metrics.Meter
responseSize metrics.Histogram
requestsInFlight metrics.Counter
protocolRequestsRate map[int16]metrics.Meter
brokerIncomingByteRate metrics.Meter
brokerRequestRate metrics.Meter
brokerFetchRate metrics.Meter
brokerRequestSize metrics.Histogram
brokerRequestLatency metrics.Histogram
brokerOutgoingByteRate metrics.Meter
brokerResponseRate metrics.Meter
brokerResponseSize metrics.Histogram
brokerRequestsInFlight metrics.Counter
brokerThrottleTime metrics.Histogram
brokerProtocolRequestsRate map[int16]metrics.Meter

kerberosAuthenticator GSSAPIKerberosAuth
clientSessionReauthenticationTimeMs int64
Expand Down Expand Up @@ -218,6 +220,7 @@ func (b *Broker) Open(conf *Config) error {
b.responseRate = metrics.GetOrRegisterMeter("response-rate", b.metricRegistry)
b.responseSize = getOrRegisterHistogram("response-size", b.metricRegistry)
b.requestsInFlight = metrics.GetOrRegisterCounter("requests-in-flight", b.metricRegistry)
b.protocolRequestsRate = map[int16]metrics.Meter{}
// Do not gather metrics for seeded broker (only used during bootstrap) because they share
// the same id (-1) and are already exposed through the global metrics above
if b.id >= 0 && !metrics.UseNilMetrics {
Expand Down Expand Up @@ -998,6 +1001,7 @@ func (b *Broker) sendInternal(rb protocolBody, promise *responsePromise) error {
b.addRequestInFlightMetrics(1)
bytes, err := b.write(buf)
b.updateOutgoingCommunicationMetrics(bytes)
b.updateProtocolMetrics(rb)
if err != nil {
b.addRequestInFlightMetrics(-1)
return err
Expand Down Expand Up @@ -1606,6 +1610,24 @@ func (b *Broker) updateOutgoingCommunicationMetrics(bytes int) {
}
}

func (b *Broker) updateProtocolMetrics(rb protocolBody) {
protocolRequestsRate := b.protocolRequestsRate[rb.key()]
if protocolRequestsRate == nil {
protocolRequestsRate = metrics.GetOrRegisterMeter(fmt.Sprintf("protocol-requests-rate-%d", rb.key()), b.metricRegistry)
b.protocolRequestsRate[rb.key()] = protocolRequestsRate
}
protocolRequestsRate.Mark(1)

if b.brokerProtocolRequestsRate != nil {
brokerProtocolRequestsRate := b.brokerProtocolRequestsRate[rb.key()]
if brokerProtocolRequestsRate == nil {
brokerProtocolRequestsRate = b.registerMeter(fmt.Sprintf("protocol-requests-rate-%d", rb.key()))
b.brokerProtocolRequestsRate[rb.key()] = brokerProtocolRequestsRate
}
brokerProtocolRequestsRate.Mark(1)
}
}

func (b *Broker) updateThrottleMetric(throttleTime time.Duration) {
if throttleTime != time.Duration(0) {
DebugLogger.Printf(
Expand All @@ -1629,6 +1651,7 @@ func (b *Broker) registerMetrics() {
b.brokerResponseSize = b.registerHistogram("response-size")
b.brokerRequestsInFlight = b.registerCounter("requests-in-flight")
b.brokerThrottleTime = b.registerHistogram("throttle-time-in-ms")
b.brokerProtocolRequestsRate = map[int16]metrics.Meter{}
}

func (b *Broker) registerMeter(name string) metrics.Meter {
Expand Down
48 changes: 26 additions & 22 deletions sarama.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ Metrics are exposed through https://github.com/rcrowley/go-metrics library in a
Broker related metrics:
+----------------------------------------------+------------+---------------------------------------------------------------+
| Name | Type | Description |
+----------------------------------------------+------------+---------------------------------------------------------------+
| incoming-byte-rate | meter | Bytes/second read off all brokers |
| incoming-byte-rate-for-broker-<broker-id> | meter | Bytes/second read off a given broker |
| outgoing-byte-rate | meter | Bytes/second written off all brokers |
| outgoing-byte-rate-for-broker-<broker-id> | meter | Bytes/second written off a given broker |
| request-rate | meter | Requests/second sent to all brokers |
| request-rate-for-broker-<broker-id> | meter | Requests/second sent to a given broker |
| request-size | histogram | Distribution of the request size in bytes for all brokers |
| request-size-for-broker-<broker-id> | histogram | Distribution of the request size in bytes for a given broker |
| request-latency-in-ms | histogram | Distribution of the request latency in ms for all brokers |
| request-latency-in-ms-for-broker-<broker-id> | histogram | Distribution of the request latency in ms for a given broker |
| response-rate | meter | Responses/second received from all brokers |
| response-rate-for-broker-<broker-id> | meter | Responses/second received from a given broker |
| response-size | histogram | Distribution of the response size in bytes for all brokers |
| response-size-for-broker-<broker-id> | histogram | Distribution of the response size in bytes for a given broker |
| requests-in-flight | counter | The current number of in-flight requests awaiting a response |
| | | for all brokers |
| requests-in-flight-for-broker-<broker-id> | counter | The current number of in-flight requests awaiting a response |
| | | for a given broker |
+----------------------------------------------+------------+---------------------------------------------------------------+
+---------------------------------------------------------+------------+---------------------------------------------------------------+
| Name | Type | Description |
+---------------------------------------------------------+------------+---------------------------------------------------------------+
| incoming-byte-rate | meter | Bytes/second read off all brokers |
| incoming-byte-rate-for-broker-<broker-id> | meter | Bytes/second read off a given broker |
| outgoing-byte-rate | meter | Bytes/second written off all brokers |
| outgoing-byte-rate-for-broker-<broker-id> | meter | Bytes/second written off a given broker |
| request-rate | meter | Requests/second sent to all brokers |
| request-rate-for-broker-<broker-id> | meter | Requests/second sent to a given broker |
| request-size | histogram | Distribution of the request size in bytes for all brokers |
| request-size-for-broker-<broker-id> | histogram | Distribution of the request size in bytes for a given broker |
| request-latency-in-ms | histogram | Distribution of the request latency in ms for all brokers |
| request-latency-in-ms-for-broker-<broker-id> | histogram | Distribution of the request latency in ms for a given broker |
| response-rate | meter | Responses/second received from all brokers |
| response-rate-for-broker-<broker-id> | meter | Responses/second received from a given broker |
| response-size | histogram | Distribution of the response size in bytes for all brokers |
| response-size-for-broker-<broker-id> | histogram | Distribution of the response size in bytes for a given broker |
| requests-in-flight | counter | The current number of in-flight requests awaiting a response |
| | | for all brokers |
| requests-in-flight-for-broker-<broker-id> | counter | The current number of in-flight requests awaiting a response |
| | | for a given broker |
| protocol-requests-rate-<api-key> | meter | Number of api requests sent to the brokers for all brokers |
| | | https://kafka.apache.org/protocol.html#protocol_api_keys | |
| protocol-requests-rate-<api-key>-for-broker-<broker-id> | meter | Number of packets sent to the brokers by api-key for a given |
| | | broker |
+---------------------------------------------------------+------------+---------------------------------------------------------------+
Note that we do not gather specific metrics for seed brokers but they are part of the "all brokers" metrics.
Expand Down

0 comments on commit 978aadd

Please sign in to comment.