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
// 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 MySQL.

use std::sync::Arc;
use std::sync::Mutex;

use mz_ore::metric;
use mz_ore::metrics::{
    DeleteOnDropCounter, DeleteOnDropGauge, GaugeVec, IntCounterVec, MetricsRegistry, UIntGaugeVec,
};
use mz_repr::GlobalId;
use prometheus::core::{AtomicF64, AtomicU64};

#[derive(Clone, Debug)]
pub(crate) struct MySqlSourceMetricDefs {
    pub(crate) total_messages: IntCounterVec,
    pub(crate) ignored_messages: IntCounterVec,
    pub(crate) insert_rows: IntCounterVec,
    pub(crate) update_rows: IntCounterVec,
    pub(crate) delete_rows: IntCounterVec,
    pub(crate) tables: UIntGaugeVec,
    pub(crate) gtid_txids: UIntGaugeVec,

    pub(crate) snapshot_defs: MySqlSnapshotMetricDefs,
}

impl MySqlSourceMetricDefs {
    pub(crate) fn register_with(registry: &MetricsRegistry) -> Self {
        Self {
            total_messages: registry.register(metric!(
                name: "mz_mysql_per_source_messages_total",
                help: "The total number of replication messages for this source, not expected to be the sum of the other values.",
                var_labels: ["source_id"],
            )),
            ignored_messages: registry.register(metric!(
                name: "mz_mysql_per_source_ignored_messages",
                help: "The number of messages ignored because of an irrelevant type or relation_id",
                var_labels: ["source_id"],
            )),
            insert_rows: registry.register(metric!(
                name: "mz_mysql_per_source_inserts",
                help: "The number of inserts for all tables in this source",
                var_labels: ["source_id"],
            )),
            update_rows: registry.register(metric!(
                name: "mz_mysql_per_source_updates",
                help: "The number of updates for all tables in this source",
                var_labels: ["source_id"],
            )),
            delete_rows: registry.register(metric!(
                name: "mz_mysql_per_source_deletes",
                help: "The number of deletes for all tables in this source",
                var_labels: ["source_id"],
            )),
            tables: registry.register(metric!(
                name: "mz_mysql_per_source_tables_count",
                help: "The number of upstream tables for this source",
                var_labels: ["source_id"],
            )),
            gtid_txids: registry.register(metric!(
                name: "mz_mysql_sum_gtid_txns",
                help: "The sum of all transaction-ids committed for each GTID Source-ID UUID seen for this source",
                var_labels: ["source_id"],
            )),
            snapshot_defs: MySqlSnapshotMetricDefs::register_with(registry),
        }
    }
}

/// Metrics for MySql sources.
pub(crate) struct MySqlSourceMetrics {
    pub(crate) inserts: DeleteOnDropCounter<'static, AtomicU64, Vec<String>>,
    pub(crate) updates: DeleteOnDropCounter<'static, AtomicU64, Vec<String>>,
    pub(crate) deletes: DeleteOnDropCounter<'static, AtomicU64, Vec<String>>,
    pub(crate) ignored: DeleteOnDropCounter<'static, AtomicU64, Vec<String>>,
    pub(crate) total: DeleteOnDropCounter<'static, AtomicU64, Vec<String>>,
    pub(crate) tables: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    pub(crate) gtid_txids: DeleteOnDropGauge<'static, AtomicU64, Vec<String>>,
    pub(crate) snapshot_metrics: MySqlSnapshotMetrics,
}

impl MySqlSourceMetrics {
    /// Create a `MySqlSourceMetrics` from the `MySqlSourceMetricDefs`.
    pub(crate) fn new(defs: &MySqlSourceMetricDefs, source_id: GlobalId) -> Self {
        let labels = &[source_id.to_string()];
        Self {
            inserts: defs.insert_rows.get_delete_on_drop_metric(labels.to_vec()),
            updates: defs.update_rows.get_delete_on_drop_metric(labels.to_vec()),
            deletes: defs.delete_rows.get_delete_on_drop_metric(labels.to_vec()),
            ignored: defs
                .ignored_messages
                .get_delete_on_drop_metric(labels.to_vec()),
            total: defs
                .total_messages
                .get_delete_on_drop_metric(labels.to_vec()),
            tables: defs.tables.get_delete_on_drop_metric(labels.to_vec()),
            gtid_txids: defs.gtid_txids.get_delete_on_drop_metric(labels.to_vec()),
            snapshot_metrics: MySqlSnapshotMetrics {
                source_id,
                gauges: Default::default(),
                defs: defs.snapshot_defs.clone(),
            },
        }
    }
}

#[derive(Clone, Debug)]
pub(crate) struct MySqlSnapshotMetricDefs {
    pub(crate) table_count_latency: GaugeVec,
}

impl MySqlSnapshotMetricDefs {
    pub(crate) fn register_with(registry: &MetricsRegistry) -> Self {
        Self {
            table_count_latency: registry.register(metric!(
                name: "mz_mysql_snapshot_count_latency",
                help: "The wall time used to obtain snapshot sizes.",
                var_labels: ["source_id", "table_name", "schema"],
            )),
        }
    }
}

#[derive(Clone)]
pub(crate) struct MySqlSnapshotMetrics {
    source_id: GlobalId,
    // This has to be shared between tokio tasks and the replication operator, as the collection
    // of these metrics happens once in those tasks, which do not live long enough to keep them
    // alive.
    gauges: Arc<Mutex<Vec<DeleteOnDropGauge<'static, AtomicF64, Vec<String>>>>>,
    defs: MySqlSnapshotMetricDefs,
}

impl MySqlSnapshotMetrics {
    pub(crate) fn record_table_count_latency(
        &self,
        table_name: String,
        schema: String,
        latency: f64,
    ) {
        let latency_gauge = self
            .defs
            .table_count_latency
            .get_delete_on_drop_metric(vec![self.source_id.to_string(), table_name, schema]);
        latency_gauge.set(latency);
        self.gauges.lock().expect("poisoned").push(latency_gauge)
    }
}