scouter_events/queue/
types.rs

1use crate::producer::kafka::KafkaConfig;
2use crate::producer::rabbitmq::RabbitMQConfig;
3use crate::producer::redis::RedisConfig;
4use pyo3::prelude::*;
5use pyo3::IntoPyObjectExt;
6use scouter_settings::HTTPConfig;
7use scouter_types::TransportType;
8
9#[derive(Clone, Debug)]
10pub enum TransportConfig {
11    RabbitMQ(RabbitMQConfig),
12    Kafka(KafkaConfig),
13    Http(HTTPConfig),
14    Redis(RedisConfig),
15}
16
17impl TransportConfig {
18    /// Create a TransportConfig from a python config object.
19    /// Function will extract the transport type and then extract the corresponding config
20    /// before returning the TransportConfig.
21    ///
22    /// # Arguments
23    /// * `config` - Python config object
24    ///
25    /// # Returns
26    /// * `TransportConfig` - TransportConfig object
27    pub fn from_py_config(config: &Bound<'_, PyAny>) -> PyResult<Self> {
28        let transport_type = config
29            .getattr("transport_type")?
30            .extract::<TransportType>()?;
31
32        match transport_type {
33            TransportType::RabbitMQ => {
34                let rabbitmq_config = config.extract::<RabbitMQConfig>()?;
35                Ok(TransportConfig::RabbitMQ(rabbitmq_config))
36            }
37            TransportType::Kafka => {
38                let kafka_config = config.extract::<KafkaConfig>()?;
39                Ok(TransportConfig::Kafka(kafka_config))
40            }
41            TransportType::Http => {
42                let http_config = config.extract::<HTTPConfig>()?;
43                Ok(TransportConfig::Http(http_config))
44            }
45            TransportType::Redis => {
46                let redis_config = config.extract::<RedisConfig>()?;
47                Ok(TransportConfig::Redis(redis_config))
48            }
49        }
50    }
51
52    /// helper method to convert the TransportConfig to a python object
53    pub fn to_py<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
54        match self {
55            TransportConfig::RabbitMQ(config) => config.clone().into_bound_py_any(py),
56            TransportConfig::Kafka(config) => config.clone().into_bound_py_any(py),
57            TransportConfig::Http(config) => config.clone().into_bound_py_any(py),
58            TransportConfig::Redis(config) => config.clone().into_bound_py_any(py),
59        }
60    }
61}