scouter_types/alert/
dispatch.rs

1use crate::drift::DriftArgs;
2use crate::error::TypeError;
3use pyo3::{prelude::*, IntoPyObjectExt};
4use serde::{Deserialize, Serialize};
5use std::fmt::Display;
6use std::str::FromStr;
7#[pyclass]
8#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
9pub struct ConsoleDispatchConfig {
10    #[pyo3(get, set)]
11    pub enabled: bool,
12}
13
14#[pyclass]
15#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
16pub struct SlackDispatchConfig {
17    #[pyo3(get, set)]
18    pub channel: String,
19}
20
21#[pymethods]
22impl SlackDispatchConfig {
23    #[new]
24    pub fn new(channel: String) -> PyResult<Self> {
25        Ok(SlackDispatchConfig { channel })
26    }
27}
28
29#[pyclass]
30#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
31pub struct OpsGenieDispatchConfig {
32    #[pyo3(get, set)]
33    pub team: String,
34
35    #[pyo3(get, set)]
36    pub priority: String,
37}
38
39#[pymethods]
40impl OpsGenieDispatchConfig {
41    #[new]
42    #[pyo3(signature = (team, priority="P5"))]
43    pub fn new(team: &str, priority: &str) -> PyResult<Self> {
44        Ok(OpsGenieDispatchConfig {
45            team: team.to_string(),
46            priority: priority.to_string(),
47        })
48    }
49}
50
51#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
52pub enum AlertDispatchConfig {
53    Slack(SlackDispatchConfig),
54    OpsGenie(OpsGenieDispatchConfig),
55    Console(ConsoleDispatchConfig),
56}
57
58impl AlertDispatchConfig {
59    pub fn dispatch_type(&self) -> AlertDispatchType {
60        match self {
61            AlertDispatchConfig::Slack(_) => AlertDispatchType::Slack,
62            AlertDispatchConfig::OpsGenie(_) => AlertDispatchType::OpsGenie,
63            AlertDispatchConfig::Console(_) => AlertDispatchType::Console,
64        }
65    }
66
67    pub fn config<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
68        match self {
69            AlertDispatchConfig::Slack(config) => config.clone().into_bound_py_any(py),
70            AlertDispatchConfig::OpsGenie(config) => config.clone().into_bound_py_any(py),
71            AlertDispatchConfig::Console(config) => config.clone().into_bound_py_any(py),
72        }
73    }
74}
75
76impl Default for AlertDispatchConfig {
77    fn default() -> Self {
78        AlertDispatchConfig::Console(ConsoleDispatchConfig { enabled: true })
79    }
80}
81
82#[pyclass(eq)]
83#[derive(Debug, PartialEq, Serialize, Deserialize, Clone, Default)]
84pub enum AlertDispatchType {
85    Slack,
86    #[default]
87    Console,
88    OpsGenie,
89}
90
91#[pymethods]
92impl AlertDispatchType {
93    pub fn to_string(&self) -> &str {
94        match self {
95            AlertDispatchType::Slack => "Slack",
96            AlertDispatchType::Console => "Console",
97            AlertDispatchType::OpsGenie => "OpsGenie",
98        }
99    }
100}
101
102impl Display for AlertDispatchType {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        match self {
105            AlertDispatchType::Slack => write!(f, "Slack"),
106            AlertDispatchType::Console => write!(f, "Console"),
107            AlertDispatchType::OpsGenie => write!(f, "OpsGenie"),
108        }
109    }
110}
111
112pub trait DispatchAlertDescription {
113    fn create_alert_description(&self, dispatch_type: AlertDispatchType) -> String;
114}
115
116pub trait DispatchDriftConfig {
117    fn get_drift_args(&self) -> DriftArgs;
118}
119
120#[pyclass]
121#[derive(Debug, PartialEq, Clone, Serialize)]
122pub enum TransportType {
123    Kafka,
124    RabbitMQ,
125    Http,
126    Redis,
127    Mock,
128}
129
130#[pyclass(eq)]
131#[derive(PartialEq, Clone, Debug, Serialize)]
132pub enum CompressionType {
133    NA,
134    Gzip,
135    Snappy,
136    Lz4,
137    Zstd,
138}
139
140impl FromStr for CompressionType {
141    type Err = TypeError;
142
143    fn from_str(s: &str) -> Result<Self, Self::Err> {
144        match s {
145            "none" => Ok(CompressionType::NA),
146            "gzip" => Ok(CompressionType::Gzip),
147            "snappy" => Ok(CompressionType::Snappy),
148            "lz4" => Ok(CompressionType::Lz4),
149            "zstd" => Ok(CompressionType::Zstd),
150            _ => Err(TypeError::InvalidCompressionTypeError),
151        }
152    }
153}
154
155// impl display
156impl std::fmt::Display for CompressionType {
157    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
158        match self {
159            CompressionType::NA => write!(f, "none"),
160            CompressionType::Gzip => write!(f, "gzip"),
161            CompressionType::Snappy => write!(f, "snappy"),
162            CompressionType::Lz4 => write!(f, "lz4"),
163            CompressionType::Zstd => write!(f, "zstd"),
164        }
165    }
166}
167
168impl CompressionType {
169    pub fn to_otel_compression(&self) -> Result<opentelemetry_otlp::Compression, TypeError> {
170        match self {
171            CompressionType::Gzip => Ok(opentelemetry_otlp::Compression::Gzip),
172            CompressionType::Zstd => Ok(opentelemetry_otlp::Compression::Zstd),
173            _ => Err(TypeError::CompressionTypeNotSupported(self.to_string())),
174        }
175    }
176}