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
// 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 our Frontegg Authentication client.

use mz_ore::metric;
use mz_ore::metrics::MetricsRegistry;
use mz_ore::stats::histogram_seconds_buckets;
use prometheus::{HistogramVec, IntCounterVec, IntGaugeVec};

#[derive(Debug, Clone)]
pub struct Metrics {
    /// Total number of requests since process start.
    pub http_request_count: IntCounterVec,
    /// How long it takes for a request to Frontegg to complete.
    pub request_duration_seconds: HistogramVec,
    /// The number of active refresh tasks we have running.
    pub refresh_tasks_active: IntGaugeVec,
    /// Number of sessions that have requested to start.
    pub session_request_count: IntCounterVec,
    /// Number of sessions that get refreshed.
    pub session_refresh_count: IntCounterVec,
}

impl Metrics {
    pub(crate) fn register_into(registry: &MetricsRegistry) -> Self {
        Self {
            http_request_count: registry.register(metric!(
                name: "mz_auth_request_count",
                help: "Total number of HTTP requests made to Frontegg for authentication",
                var_labels: ["path", "status"],
            )),
            request_duration_seconds: registry.register(metric!(
                name: "mz_auth_request_duration_seconds",
                help: "How long it takes for a request to Frontegg to complete in seconds.",
                var_labels: ["path"],
                buckets: histogram_seconds_buckets(0.000_128, 8.0),
            )),
            refresh_tasks_active: registry.register(metric!(
                name: "mz_auth_refresh_tasks_active",
                help: "The number of active refresh tasks we have running.",
            )),
            session_request_count: registry.register(metric!(
                name: "mz_auth_session_request_count",
                help: "Total number of session start requests the Authenticator has received.",
                var_labels: ["existing_session"],
            )),
            session_refresh_count: registry.register(metric!(
                name: "mz_auth_session_refresh_count",
                help: "Total number of authentication sessions that get refreshed.",
                var_labels: ["outstanding_receivers", "recent_drop"],
            )),
        }
    }
}