Skip to main content

mz_sql/session/
hint.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 internal::Private;
11
12/// Prometheus label for [`ApplicationNameHint::Unspecified`].
13const UNSPECIFIED_LABEL: &str = "unspecified";
14/// Prometheus label for [`ApplicationNameHint::Unrecognized`].
15const UNRECOGNIZED_LABEL: &str = "unrecognized";
16/// Prometheus label for [`ApplicationNameHint::Psql`].
17const PSQL_LABEL: &str = "psql";
18/// Prometheus label for [`ApplicationNameHint::Dbt`].
19const DBT_LABEL: &str = "dbt";
20/// Prometheus label for [`ApplicationNameHint::WebConsole`].
21const WEB_CONSOLE_LABEL: &str = "web_console";
22/// Promehteus label for [`ApplicationNameHint::WebConsoleShell`].
23const WEB_CONSOLE_SHELL_LABEL: &str = "web_console_shell";
24/// Prometheus label for [`ApplicationNameHint::MzPsql`].
25const MZ_PSQL_LABEL: &str = "mz_psql";
26/// Prometheus label for [`ApplicationNameHint::MaterializeFivetranDestination`].
27const MATERIALIZE_FIVETRAN_DESTINATION_LABEL: &str = "materialize_fivetran_destination";
28/// Prometheus label for [`ApplicationNameHint::TerraformProviderMaterialize`].
29const TERRAFORM_PROVIDER_MATERIALIZE_LABEL: &str = "terraform_provider_materialize";
30/// Prometheus label for [`ApplicationNameHint::TablePlus`].
31const TABLE_PLUS_LABEL: &str = "table_plus";
32/// Prometheus label for [`ApplicationNameHint::DataGrip`].
33const DATA_GRIP_LABEL: &str = "data_grip";
34/// Prometheus label for [`ApplicationNameHint::DBeaver`].
35const D_BEAVER_LABEL: &str = "dbeaver";
36/// Prometheus label for [`ApplicationNameHint::MzVscode`].
37const MZ_VSCODE_LABEL: &str = "mz_vscode";
38/// Prometheus label for [`ApplicationNameHint::MzGrafanaIntegration`].
39const MZ_GRAFANA_LABEL: &str = "mz_grafana";
40/// Prometheus label for [`ApplicationNameHint::MzMcpAgent`].
41const MZ_MCP_AGENT_LABEL: &str = "mz_mcp_agents";
42/// Prometheus label for [`ApplicationNameHint::MzMcpDeveloper`].
43const MZ_MCP_DEVELOPER_LABEL: &str = "mz_mcp_developer";
44/// Prometheus label for [`ApplicationNameHint::MzMcp`].
45const MZ_MCP_LABEL: &str = "mz_mcp";
46
47/// A hint for what application is making a request to the adapter.
48///
49/// Note: [`ApplicationNameHint`] gets logged as a label in our Prometheus metrics, and for
50/// labels we need to be conscious of the cardinality, so please be careful with how many
51/// variants we add to this enum.
52///
53/// Note: each enum variant contains an `internal::Private` to prevent creating this enum
54/// directly. To create an instance of [`ApplicationNameHint`] please see
55/// [`ApplicationNameHint::from_str`].
56#[derive(Debug, Copy, Clone)]
57pub enum ApplicationNameHint {
58    /// No `application_name` was set.
59    Unspecified(Private),
60    /// An `application_name` was set, but it's not one we recognize.
61    Unrecognized(Private),
62    /// Request came from `psql`.
63    Psql(Private),
64    /// Request came from `dbt`.
65    Dbt(Private),
66    /// Request came from our web console.
67    WebConsole(Private),
68    /// Request came from the SQL shell in our web console.
69    WebConsoleShell(Private),
70    /// Request came from the `psql` shell spawned by `mz`.
71    MzPsql(Private),
72    /// Request came from our Fivetran Destination,
73    MaterializeFivetranDestination(Private),
74    /// Request came from a version of our Terraform provider.
75    TerraformProviderMaterialize(Private),
76    /// Request came from TablePlus.
77    TablePlus(Private),
78    /// Request came from a version of DataGrip.
79    DataGrip(Private),
80    /// Request came from a version of DBeaver.
81    DBeaver(Private),
82    /// Request came from our Visual Studio Code integration.
83    MzVscode(Private),
84    /// Request came from our Grafana integration.
85    MzGrafanaIntegration(Private),
86    /// Request came from the MCP agent endpoint.
87    MzMcpAgent(Private),
88    /// Request came from the MCP developer endpoint.
89    MzMcpDeveloper(Private),
90    /// Request came from the unified MCP endpoint.
91    MzMcp(Private),
92}
93
94impl ApplicationNameHint {
95    pub fn from_str(s: &str) -> Self {
96        match s.to_lowercase().as_str() {
97            "psql" => ApplicationNameHint::Psql(Private),
98            "web_console" => ApplicationNameHint::WebConsole(Private),
99            "web_console_shell" => ApplicationNameHint::WebConsoleShell(Private),
100            "mz_psql" => ApplicationNameHint::MzPsql(Private),
101            // Note: Make sure this is kept in sync with the `fivetran-destination` crate.
102            "materialize_fivetran_destination" => {
103                ApplicationNameHint::MaterializeFivetranDestination(Private)
104            }
105            "tableplus" => ApplicationNameHint::TablePlus(Private),
106            "mz_vscode" => ApplicationNameHint::MzVscode(Private),
107            "mz_grafana_integration" => ApplicationNameHint::MzGrafanaIntegration(Private),
108            "mz_mcp_agents" => ApplicationNameHint::MzMcpAgent(Private),
109            "mz_mcp_developer" => ApplicationNameHint::MzMcpDeveloper(Private),
110            "mz_mcp" => ApplicationNameHint::MzMcp(Private),
111            // Terraform provides the version as a suffix.
112            x if x.starts_with("terraform-provider-materialize") => {
113                ApplicationNameHint::TerraformProviderMaterialize(Private)
114            }
115            // dbt provides the version as a suffix.
116            x if x.starts_with("dbt") => ApplicationNameHint::Dbt(Private),
117            // DataGrip provides the version as a suffix.
118            x if x.starts_with("datagrip") => ApplicationNameHint::DataGrip(Private),
119            // DBeaver provides the version as a suffix.
120            x if x.starts_with("dbeaver") => ApplicationNameHint::DBeaver(Private),
121            "" => ApplicationNameHint::Unspecified(Private),
122            // TODO(parkertimmerman): We should keep some record of these "unrecognized"
123            // names, and possibly support more popular ones in the future.
124            _ => ApplicationNameHint::Unrecognized(Private),
125        }
126    }
127
128    pub fn as_str(&self) -> &'static str {
129        match self {
130            ApplicationNameHint::Unspecified(_) => UNSPECIFIED_LABEL,
131            ApplicationNameHint::Unrecognized(_) => UNRECOGNIZED_LABEL,
132            ApplicationNameHint::Psql(_) => PSQL_LABEL,
133            ApplicationNameHint::Dbt(_) => DBT_LABEL,
134            ApplicationNameHint::WebConsole(_) => WEB_CONSOLE_LABEL,
135            ApplicationNameHint::WebConsoleShell(_) => WEB_CONSOLE_SHELL_LABEL,
136            ApplicationNameHint::MzPsql(_) => MZ_PSQL_LABEL,
137            ApplicationNameHint::MaterializeFivetranDestination(_) => {
138                MATERIALIZE_FIVETRAN_DESTINATION_LABEL
139            }
140            ApplicationNameHint::TablePlus(_) => TABLE_PLUS_LABEL,
141            ApplicationNameHint::MzVscode(_) => MZ_VSCODE_LABEL,
142            ApplicationNameHint::MzGrafanaIntegration(_) => MZ_GRAFANA_LABEL,
143            ApplicationNameHint::MzMcpAgent(_) => MZ_MCP_AGENT_LABEL,
144            ApplicationNameHint::MzMcpDeveloper(_) => MZ_MCP_DEVELOPER_LABEL,
145            ApplicationNameHint::MzMcp(_) => MZ_MCP_LABEL,
146            ApplicationNameHint::TerraformProviderMaterialize(_) => {
147                TERRAFORM_PROVIDER_MATERIALIZE_LABEL
148            }
149            ApplicationNameHint::DataGrip(_) => DATA_GRIP_LABEL,
150            ApplicationNameHint::DBeaver(_) => D_BEAVER_LABEL,
151        }
152    }
153
154    /// Returns whether or not we should trace errors for this requests with this application name.
155    pub fn should_trace_errors(&self) -> bool {
156        // Note(parkmycar): For now we only trace errors for the web console since we contol all of
157        // those queries and in general they should never fail. As opposed to user queries which
158        // are arbitrary.
159        matches!(self, ApplicationNameHint::WebConsole(_))
160    }
161}
162
163mod internal {
164    #[derive(Debug, Copy, Clone)]
165    pub struct Private;
166}