scouter_events/queue/
types.rs

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