Skip to main content

common_base/
protocol.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16
17/// The protocol or internal subsystem through which a query is received.
18#[derive(Debug, PartialEq, Default, Clone, Copy, strum::FromRepr)]
19#[repr(u8)]
20pub enum Channel {
21    #[default]
22    Unknown = 0,
23
24    Mysql = 1,
25    Postgres = 2,
26    HttpSql = 3,
27    Prometheus = 4,
28    Otlp = 5,
29    Grpc = 6,
30    Influx = 7,
31    Opentsdb = 8,
32    Loki = 9,
33    Elasticsearch = 10,
34    Jaeger = 11,
35    Log = 12,
36    Promql = 13,
37    Splunk = 14,
38    /// Trusted internal requests and local subsystem execution.
39    Internal = 255,
40}
41
42impl From<u32> for Channel {
43    fn from(value: u32) -> Self {
44        u8::try_from(value)
45            .ok()
46            .and_then(Self::from_repr)
47            .unwrap_or_default()
48    }
49}
50
51impl Display for Channel {
52    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
53        write!(f, "{}", self.as_ref())
54    }
55}
56
57impl AsRef<str> for Channel {
58    fn as_ref(&self) -> &str {
59        match self {
60            Self::Unknown => "unknown",
61            Self::Mysql => "mysql",
62            Self::Postgres => "postgres",
63            Self::HttpSql => "httpsql",
64            Self::Prometheus => "prometheus",
65            Self::Otlp => "otlp",
66            Self::Grpc => "grpc",
67            Self::Influx => "influx",
68            Self::Opentsdb => "opentsdb",
69            Self::Loki => "loki",
70            Self::Elasticsearch => "elasticsearch",
71            Self::Jaeger => "jaeger",
72            Self::Log => "log",
73            Self::Promql => "promql",
74            Self::Splunk => "splunk",
75            Self::Internal => "internal",
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::Channel;
83
84    #[test]
85    fn test_channel_name() {
86        let expected = [
87            (1, "mysql"),
88            (2, "postgres"),
89            (3, "httpsql"),
90            (4, "prometheus"),
91            (5, "otlp"),
92            (6, "grpc"),
93            (7, "influx"),
94            (8, "opentsdb"),
95            (9, "loki"),
96            (10, "elasticsearch"),
97            (11, "jaeger"),
98            (12, "log"),
99            (13, "promql"),
100            (14, "splunk"),
101            (255, "internal"),
102        ];
103
104        for (value, name) in expected {
105            assert_eq!(name, Channel::from(value).as_ref());
106        }
107        assert_eq!("unknown", Channel::from(0).as_ref());
108        assert_eq!("unknown", Channel::from(15).as_ref());
109        assert_eq!("unknown", Channel::from(256).as_ref());
110    }
111}