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

//! Implementation-specific metrics for the internal, pooled Postgres client.

use mz_ore::metric;
use mz_ore::metrics::{Counter, IntCounter, MetricsRegistry, UIntGauge};

/// Metrics specific to [PostgresClient](crate::PostgresClient)'s internal
/// workings.
#[derive(Debug, Clone)]
pub struct PostgresClientMetrics {
    pub(crate) connpool_size: UIntGauge,
    pub(crate) connpool_acquires: IntCounter,
    pub(crate) connpool_acquire_seconds: Counter,
    pub(crate) connpool_available: prometheus::Gauge,
    pub(crate) connpool_connections_created: Counter,
    pub(crate) connpool_connection_errors: Counter,
    pub(crate) connpool_ttl_reconnections: Counter,
}

impl PostgresClientMetrics {
    /// Returns a new [PostgresClientMetrics] instance connected to the given registry.
    pub fn new(registry: &MetricsRegistry, prefix: &str) -> Self {
        Self {
            connpool_size: registry.register(metric!(
                name: format!("{}_postgres_connpool_size", prefix),
                help: "number of connections currently in pool",
            )),
            connpool_acquires: registry.register(metric!(
                name: format!("{}_postgres_connpool_acquires", prefix),
                help: "times a connection has been acquired from pool",
            )),
            connpool_acquire_seconds: registry.register(metric!(
                name: format!("{}_postgres_connpool_acquire_seconds", prefix),
                help: "time spent acquiring connections from pool",
            )),
            connpool_available: registry.register(metric!(
                name: format!("{}_postgres_connpool_available", prefix),
                help: "available connections in the pool",
            )),
            connpool_connections_created: registry.register(metric!(
                name: format!("{}_postgres_connpool_connections_created", prefix),
                help: "times a connection was created",
            )),
            connpool_connection_errors: registry.register(metric!(
                name: format!("{}_postgres_connpool_connection_errors", prefix),
                help: "number of errors when establishing a new connection",
            )),
            connpool_ttl_reconnections: registry.register(metric!(
                name: format!("{}_postgres_connpool_ttl_reconnections", prefix),
                help: "times a connection was recycled due to ttl",
            )),
        }
    }
}