1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

//! Metrics for kafka sinks.

use mz_ore::metric;
use mz_ore::metrics::{DeleteOnDropGauge, IntGaugeVec, MetricsRegistry, UIntGaugeVec};
use mz_repr::GlobalId;
use prometheus::core::{AtomicI64, AtomicU64};

/// Definitions for librdkafka produced metrics used in sinks.
#[derive(Clone, Debug)]
pub(crate) struct KafkaSinkMetricDefs {
    /// The current number of messages in producer queues.
    pub rdkafka_msg_cnt: UIntGaugeVec,
    /// The current total size of messages in producer queues.
    pub rdkafka_msg_size: UIntGaugeVec,
    /// The total number of messages transmitted (produced) to brokers.
    pub rdkafka_txmsgs: IntGaugeVec,
    /// The total number of bytes transmitted (produced) to brokers.
    pub rdkafka_txmsg_bytes: IntGaugeVec,
    /// The total number of requests sent to brokers.
    pub rdkafka_tx: IntGaugeVec,
    /// The total number of bytes transmitted to brokers.
    pub rdkafka_tx_bytes: IntGaugeVec,
    /// The number of requests awaiting transmission across all brokers.
    pub rdkafka_outbuf_cnt: IntGaugeVec,
    /// The number of messages awaiting transmission across all brokers.
    pub rdkafka_outbuf_msg_cnt: IntGaugeVec,
    /// The number of requests in-flight across all brokers that are awaiting a response.
    pub rdkafka_waitresp_cnt: IntGaugeVec,
    /// The number of messages in-flight across all brokers that are awaiting a response.
    pub rdkafka_waitresp_msg_cnt: IntGaugeVec,
    /// The total number of transmission errors across all brokers.
    pub rdkafka_txerrs: UIntGaugeVec,
    /// The total number of request retries across all brokers.
    pub rdkafka_txretries: UIntGaugeVec,
    /// The total number of requests that timed out across all brokers.
    pub rdkafka_req_timeouts: UIntGaugeVec,
    /// The number of connection attempts, including successful and failed attempts, and name
    /// resolution failures across all brokers.
    pub rdkafka_connects: IntGaugeVec,
    /// The number of disconnections, whether triggered by the broker, the network, the load
    /// balancer, or something else across all brokers.
    pub rdkafka_disconnects: IntGaugeVec,
    /// The number of outstanding progress records that need to be read before the sink can resume.
    pub outstanding_progress_records: UIntGaugeVec,
    /// The number of progress records consumed while resuming the sink.
    pub consumed_progress_records: UIntGaugeVec,
    /// The number of partitions this sink is publishing to.
    pub partition_count: UIntGaugeVec,
}

impl KafkaSinkMetricDefs {
    pub(crate) fn register_with(registry: &MetricsRegistry) -> Self {
        Self {
            rdkafka_msg_cnt: registry.register(metric!(
                name: "mz_sink_rdkafka_msg_cnt",
                help: "The current number of messages in producer queues.",
                var_labels: ["sink_id"],
            )),
            rdkafka_msg_size: registry.register(metric!(
                name: "mz_sink_rdkafka_msg_size",
                help: "The current total size of messages in producer queues.",
                var_labels: ["sink_id"],
            )),
            rdkafka_txmsgs: registry.register(metric!(
                name: "mz_sink_rdkafka_txmsgs",
                help: "The total number of messages transmitted (produced) to brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_txmsg_bytes: registry.register(metric!(
                name: "mz_sink_rdkafka_txmsg_bytes",
                help: "The total number of bytes transmitted (produced) to brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_tx: registry.register(metric!(
                name: "mz_sink_rdkafka_tx",
                help: "The total number of requests sent to brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_tx_bytes: registry.register(metric!(
                name: "mz_sink_rdkafka_tx_bytes",
                help: "The total number of bytes transmitted to brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_outbuf_cnt: registry.register(metric!(
                name: "mz_sink_rdkafka_outbuf_cnt",
                help: "The number of requests awaiting transmission across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_outbuf_msg_cnt: registry.register(metric!(
                name: "mz_sink_rdkafka_outbuf_msg_cnt",
                help: "The number of messages awaiting transmission across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_waitresp_cnt: registry.register(metric!(
                name: "mz_sink_rdkafka_waitresp_cnt",
                help: "The number of requests in-flight across all brokers that are awaiting a \
                       response.",
                var_labels: ["sink_id"],
            )),
            rdkafka_waitresp_msg_cnt: registry.register(metric!(
                name: "mz_sink_rdkafka_waitresp_msg_cnt",
                help: "The number of messages in-flight across all brokers that are awaiting a \
                       response.",
                var_labels: ["sink_id"],
            )),
            rdkafka_txerrs: registry.register(metric!(
                name: "mz_sink_rdkafka_txerrs",
                help: "The total number of transmission errors across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_txretries: registry.register(metric!(
                name: "mz_sink_rdkafka_txretries",
                help: "The total number of request retries across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_req_timeouts: registry.register(metric!(
                name: "mz_sink_rdkafka_req_timeouts",
                help: "The total number of requests that timed out across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_connects: registry.register(metric!(
                name: "mz_sink_rdkafka_connects",
                help: "The number of connection attempts, including successful and failed \
                       attempts, and name resolution failures across all brokers.",
                var_labels: ["sink_id"],
            )),
            rdkafka_disconnects: registry.register(metric!(
                name: "mz_sink_rdkafka_disconnects",
                help: "The number of disconnections, whether triggered by the broker, the \
                      network, the load balancer, or something else across all brokers.",
                var_labels: ["sink_id"],
            )),
            outstanding_progress_records: registry.register(metric!(
                name: "mz_sink_oustanding_progress_records",
                help: "The number of outstanding progress records that need to be read before the sink can resume.",
                var_labels: ["sink_id"],
            )),
            consumed_progress_records: registry.register(metric!(
                name: "mz_sink_consumed_progress_records",
                help: "The number of progress records consumed by the sink.",
                var_labels: ["sink_id"],
            )),
            partition_count: registry.register(metric!(
                name: "mz_sink_partition_count",
                help: "The number of partitions this sink is publishing to.",
                var_labels: ["sink_id"],
            )),
        }
    }
}

/// Metrics reported by librdkafka
pub(crate) struct KafkaSinkMetrics {
    /// The current number of messages in producer queues.
    pub rdkafka_msg_cnt: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The current total size of messages in producer queues.
    pub rdkafka_msg_size: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The total number of messages transmitted (produced) to brokers.
    pub rdkafka_txmsgs: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The total number of bytes transmitted (produced) to brokers.
    pub rdkafka_txmsg_bytes: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The total number of requests sent to brokers.
    pub rdkafka_tx: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The total number of bytes transmitted to brokers.
    pub rdkafka_tx_bytes: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of requests awaiting transmission across all brokers.
    pub rdkafka_outbuf_cnt: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of messages awaiting transmission across all brokers.
    pub rdkafka_outbuf_msg_cnt: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of requests in-flight across all brokers that are awaiting a response.
    pub rdkafka_waitresp_cnt: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of messages in-flight across all brokers that are awaiting a response.
    pub rdkafka_waitresp_msg_cnt: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The total number of transmission errors across all brokers.
    pub rdkafka_txerrs: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The total number of request retries across all brokers.
    pub rdkafka_txretries: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The total number of requests that timed out across all brokers.
    pub rdkafka_req_timeouts: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The number of connection attempts, including successful and failed attempts, and name
    /// resolution failures across all brokers.
    pub rdkafka_connects: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of disconnections, whether triggered by the broker, the network, the load
    /// balancer, or something else across all brokers.
    pub rdkafka_disconnects: DeleteOnDropGauge<'static, AtomicI64, Vec<String>>,
    /// The number of outstanding progress records that need to be read before the sink can resume.
    pub outstanding_progress_records: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The number of progress records consumed while resuming the sink.
    pub consumed_progress_records: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    /// The number of partitions this sink is publishing to.
    pub partition_count: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
}

impl KafkaSinkMetrics {
    /// Initializes source metrics for a given (source_id, worker_id)
    pub fn new(defs: &KafkaSinkMetricDefs, sink_id: GlobalId) -> Self {
        let labels = &[sink_id.to_string()];
        Self {
            rdkafka_msg_cnt: defs
                .rdkafka_msg_cnt
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_msg_size: defs
                .rdkafka_msg_size
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_txmsgs: defs
                .rdkafka_txmsgs
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_txmsg_bytes: defs
                .rdkafka_txmsg_bytes
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_tx: defs.rdkafka_tx.get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_tx_bytes: defs
                .rdkafka_tx_bytes
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_outbuf_cnt: defs
                .rdkafka_outbuf_cnt
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_outbuf_msg_cnt: defs
                .rdkafka_outbuf_msg_cnt
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_waitresp_cnt: defs
                .rdkafka_waitresp_cnt
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_waitresp_msg_cnt: defs
                .rdkafka_waitresp_msg_cnt
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_txerrs: defs
                .rdkafka_txerrs
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_txretries: defs
                .rdkafka_txretries
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_req_timeouts: defs
                .rdkafka_req_timeouts
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_connects: defs
                .rdkafka_connects
                .get_delete_on_drop_metric(labels.to_vec()),
            rdkafka_disconnects: defs
                .rdkafka_disconnects
                .get_delete_on_drop_metric(labels.to_vec()),
            outstanding_progress_records: defs
                .outstanding_progress_records
                .get_delete_on_drop_metric(labels.to_vec()),
            consumed_progress_records: defs
                .consumed_progress_records
                .get_delete_on_drop_metric(labels.to_vec()),
            partition_count: defs
                .partition_count
                .get_delete_on_drop_metric(labels.to_vec()),
        }
    }
}