mz_pgwire/
metrics.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use mz_ore::metric;
11use mz_ore::metrics::raw::IntCounterVec;
12use mz_ore::metrics::{IntCounter, MetricsRegistry};
13
14#[derive(Clone, Debug)]
15pub struct MetricsConfig {
16    connection_status: IntCounterVec,
17}
18
19impl MetricsConfig {
20    pub fn register_into(registry: &MetricsRegistry) -> Self {
21        Self {
22            connection_status: registry.register(metric! {
23                name: "mz_connection_status",
24                help: "Count of completed network connections, by status",
25                var_labels: ["source", "status"],
26            }),
27        }
28    }
29}
30
31#[derive(Clone, Debug)]
32pub struct Metrics {
33    inner: MetricsConfig,
34    label: &'static str,
35}
36
37impl Metrics {
38    pub fn new(inner: MetricsConfig, label: &'static str) -> Self {
39        let self_ = Self { inner, label };
40
41        // pre-initialize labels we are planning to use to ensure they are all
42        // always emitted as time series
43        self_.connection_status(false);
44        self_.connection_status(true);
45
46        self_
47    }
48
49    pub fn connection_status(&self, is_ok: bool) -> IntCounter {
50        self.inner
51            .connection_status
52            .with_label_values(&[self.source_label(), Self::status_label(is_ok)])
53    }
54
55    fn status_label(is_ok: bool) -> &'static str {
56        if is_ok { "success" } else { "error" }
57    }
58
59    fn source_label(&self) -> &'static str {
60        self.label
61    }
62}