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

use internal::Private;

/// Prometheus label for [`ApplicationNameHint::Unspecified`].
const UNSPECIFIED_LABEL: &str = "unspecified";
/// Prometheus label for [`ApplicationNameHint::Unrecognized`].
const UNRECOGNIZED_LABEL: &str = "unrecognized";
/// Prometheus label for [`ApplicationNameHint::Psql`].
const PSQL_LABEL: &str = "psql";
/// Prometheus label for [`ApplicationNameHint::Dbt`].
const DBT_LABEL: &str = "dbt";
/// Prometheus label for [`ApplicationNameHint::WebConsole`].
const WEB_CONSOLE_LABEL: &str = "web_console";
/// Prometheus label for [`ApplicationNameHint::MzPsql`].
const MZ_PSQL_LABEL: &str = "mz_psql";
/// Prometheus label for [`ApplicationNameHint::MaterializeFivetranDestination`]
const MATERIALIZE_FIVETRAN_DESTINATION: &str = "materialize_fivetran_destination";

/// A hint for what application is making a request to the adapter.
///
/// Note: [`ApplicationNameHint`] gets logged as a label in our Prometheus metrics, and for
/// labels we need to be conscious of the cardinality, so please be careful with how many
/// variants we add to this enum.
///
/// Note: each enum variant contains an `internal::Private` to prevent creating this enum
/// directly. To create an instance of [`ApplicationNameHint`] please see
/// [`ApplicationNameHint::from_str`].
#[derive(Debug, Copy, Clone)]
pub enum ApplicationNameHint {
    /// No `application_name` was set.
    Unspecified(Private),
    /// An `application_name` was set, but it's not one we recognize.
    Unrecognized(Private),
    /// Request came from `psql`.
    Psql(Private),
    /// Request came from `dbt`.
    Dbt(Private),
    /// Request came from our web console.
    WebConsole(Private),
    /// Request came from the `psql` shell spawned by `mz`.
    MzPsql(Private),
    /// Request came from our Fivetran Destination,
    MaterializeFivetranDestination(Private),
}

impl ApplicationNameHint {
    pub fn from_str(s: &str) -> Self {
        match s.to_lowercase().as_str() {
            "psql" => ApplicationNameHint::Psql(Private),
            "dbt" => ApplicationNameHint::Dbt(Private),
            "web_console" => ApplicationNameHint::WebConsole(Private),
            "mz_psql" => ApplicationNameHint::MzPsql(Private),
            // Note: Make sure this is kept in sync with the `fivetran-destination` crate.
            "materialize_fivetran_destination" => {
                ApplicationNameHint::MaterializeFivetranDestination(Private)
            }
            "" => ApplicationNameHint::Unspecified(Private),
            // TODO(parkertimmerman): We should keep some record of these "unrecognized"
            // names, and possibly support more popular ones in the future.
            _ => ApplicationNameHint::Unrecognized(Private),
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            ApplicationNameHint::Unspecified(_) => UNSPECIFIED_LABEL,
            ApplicationNameHint::Unrecognized(_) => UNRECOGNIZED_LABEL,
            ApplicationNameHint::Psql(_) => PSQL_LABEL,
            ApplicationNameHint::Dbt(_) => DBT_LABEL,
            ApplicationNameHint::WebConsole(_) => WEB_CONSOLE_LABEL,
            ApplicationNameHint::MzPsql(_) => MZ_PSQL_LABEL,
            ApplicationNameHint::MaterializeFivetranDestination(_) => {
                MATERIALIZE_FIVETRAN_DESTINATION
            }
        }
    }
}

mod internal {
    #[derive(Debug, Copy, Clone)]
    pub struct Private;
}