mz_postgres_client/
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
10//! Implementation-specific metrics for the internal, pooled Postgres client.
11
12use mz_ore::metric;
13use mz_ore::metrics::{Counter, IntCounter, MetricsRegistry, UIntGauge};
14
15/// Metrics specific to [PostgresClient](crate::PostgresClient)'s internal
16/// workings.
17#[derive(Debug, Clone)]
18pub struct PostgresClientMetrics {
19    pub(crate) connpool_size: UIntGauge,
20    pub(crate) connpool_acquires: IntCounter,
21    pub(crate) connpool_acquire_seconds: Counter,
22    pub(crate) connpool_available: prometheus::Gauge,
23    pub(crate) connpool_connections_created: Counter,
24    pub(crate) connpool_connection_errors: Counter,
25    pub(crate) connpool_ttl_reconnections: Counter,
26}
27
28impl PostgresClientMetrics {
29    /// Returns a new [PostgresClientMetrics] instance connected to the given registry.
30    pub fn new(registry: &MetricsRegistry, prefix: &str) -> Self {
31        Self {
32            connpool_size: registry.register(metric!(
33                name: format!("{}_postgres_connpool_size", prefix),
34                help: "number of connections currently in pool",
35            )),
36            connpool_acquires: registry.register(metric!(
37                name: format!("{}_postgres_connpool_acquires", prefix),
38                help: "times a connection has been acquired from pool",
39            )),
40            connpool_acquire_seconds: registry.register(metric!(
41                name: format!("{}_postgres_connpool_acquire_seconds", prefix),
42                help: "time spent acquiring connections from pool",
43            )),
44            connpool_available: registry.register(metric!(
45                name: format!("{}_postgres_connpool_available", prefix),
46                help: "available connections in the pool",
47            )),
48            connpool_connections_created: registry.register(metric!(
49                name: format!("{}_postgres_connpool_connections_created", prefix),
50                help: "times a connection was created",
51            )),
52            connpool_connection_errors: registry.register(metric!(
53                name: format!("{}_postgres_connpool_connection_errors", prefix),
54                help: "number of errors when establishing a new connection",
55            )),
56            connpool_ttl_reconnections: registry.register(metric!(
57                name: format!("{}_postgres_connpool_ttl_reconnections", prefix),
58                help: "times a connection was recycled due to ttl",
59            )),
60        }
61    }
62}