scouter_settings/
events.rs

1use scouter_types::ProfileFuncs;
2use serde::Serialize;
3
4#[derive(Clone, Serialize)]
5pub struct KafkaSettings {
6    pub brokers: String,
7    pub num_workers: usize,
8    pub topics: Vec<String>,
9    pub group_id: String,
10    pub username: Option<String>,
11    pub password: Option<String>,
12    pub security_protocol: String,
13    pub sasl_mechanism: String,
14    pub offset_reset: String,
15    pub cert_location: Option<String>,
16}
17
18impl KafkaSettings {
19    pub fn __str__(&self) -> String {
20        // serialize the struct to a string
21        ProfileFuncs::__str__(self)
22    }
23}
24
25impl std::fmt::Debug for KafkaSettings {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        f.debug_struct("KafkaSettings")
28            .field("brokers", &self.brokers)
29            .field("num_workers", &self.num_workers)
30            .field("topics", &self.topics)
31            .field("group_id", &self.group_id)
32            .field("username", &self.username)
33            .field("password", &self.password.as_ref().map(|_| "***"))
34            .field("security_protocol", &self.security_protocol)
35            .field("offset_reset", &self.offset_reset)
36            .field("sasl_mechanism", &self.sasl_mechanism)
37            .finish()
38    }
39}
40
41impl Default for KafkaSettings {
42    fn default() -> Self {
43        let brokers =
44            std::env::var("KAFKA_BROKERS").unwrap_or_else(|_| "localhost:9092".to_string());
45
46        let num_workers = std::env::var("KAFKA_WORKER_COUNT")
47            .unwrap_or_else(|_| "3".to_string())
48            .parse::<usize>()
49            .unwrap();
50
51        let topics = std::env::var("KAFKA_TOPIC")
52            .unwrap_or_else(|_| "scouter_monitoring".to_string())
53            .split(',')
54            .map(|s| s.to_string())
55            .collect();
56
57        let group_id = std::env::var("KAFKA_GROUP").unwrap_or("scouter".to_string());
58        let offset_reset = std::env::var("KAFKA_OFFSET_RESET")
59            .unwrap_or_else(|_| "earliest".to_string())
60            .to_string();
61        let username: Option<String> = std::env::var("KAFKA_USERNAME").ok();
62        let password: Option<String> = std::env::var("KAFKA_PASSWORD").ok();
63
64        let security_protocol = std::env::var("KAFKA_SECURITY_PROTOCOL")
65            .ok()
66            .unwrap_or_else(|| "SASL_SSL".to_string());
67        let sasl_mechanism = std::env::var("KAFKA_SASL_MECHANISM")
68            .ok()
69            .unwrap_or_else(|| "PLAIN".to_string());
70        let cert_location = std::env::var("KAFKA_CERT_LOCATION").ok();
71
72        Self {
73            brokers,
74            num_workers,
75            topics,
76            group_id,
77            username,
78            password,
79            security_protocol,
80            sasl_mechanism,
81            offset_reset,
82            cert_location,
83        }
84    }
85}
86
87#[derive(Debug, Clone, Serialize)]
88pub struct RabbitMQSettings {
89    pub num_consumers: usize,
90    pub prefetch_count: u16,
91    pub queue: String,
92    pub consumer_tag: String,
93    pub address: String,
94}
95
96impl RabbitMQSettings {
97    pub fn __str__(&self) -> String {
98        // serialize the struct to a string
99        ProfileFuncs::__str__(self)
100    }
101}
102
103impl Default for RabbitMQSettings {
104    fn default() -> Self {
105        let num_consumers = std::env::var("RABBITMQ_CONSUMER_COUNT")
106            .unwrap_or_else(|_| "3".to_string())
107            .parse::<usize>()
108            .unwrap();
109
110        let prefetch_count = std::env::var("RABBITMQ_PREFETCH_COUNT")
111            .unwrap_or_else(|_| "10".to_string())
112            .parse::<u16>()
113            .unwrap();
114
115        let address = std::env::var("RABBITMQ_ADDR")
116            .unwrap_or_else(|_| "amqp://guest:guest@127.0.0.1:5672/%2f".to_string());
117
118        let queue =
119            std::env::var("RABBITMQ_QUEUE").unwrap_or_else(|_| "scouter_monitoring".to_string());
120
121        let consumer_tag =
122            std::env::var("RABBITMQ_CONSUMER_TAG").unwrap_or_else(|_| "scouter".to_string());
123
124        Self {
125            num_consumers,
126            prefetch_count,
127            queue,
128            consumer_tag,
129            address,
130        }
131    }
132}
133
134#[derive(Debug, Clone, Serialize)]
135pub struct RedisSettings {
136    pub num_consumers: usize,
137    pub channel: String,
138    pub address: String,
139}
140
141impl RedisSettings {
142    pub fn __str__(&self) -> String {
143        // serialize the struct to a string
144        ProfileFuncs::__str__(self)
145    }
146}
147
148impl Default for RedisSettings {
149    fn default() -> Self {
150        let num_consumers = std::env::var("REDIS_CONSUMER_COUNT")
151            .unwrap_or_else(|_| "3".to_string())
152            .parse::<usize>()
153            .unwrap();
154        let channel =
155            std::env::var("REDIS_CHANNEL").unwrap_or_else(|_| "scouter_monitoring".to_string());
156
157        let address =
158            std::env::var("REDIS_ADDR").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
159
160        Self {
161            num_consumers,
162            channel,
163            address,
164        }
165    }
166}
167
168#[derive(Debug, Clone)]
169pub struct HttpConsumerSettings {
170    pub num_workers: usize,
171}
172impl Default for HttpConsumerSettings {
173    fn default() -> Self {
174        let num_workers = std::env::var("HTTP_CONSUMER_WORKER_COUNT")
175            .unwrap_or_else(|_| "1".to_string())
176            .parse::<usize>()
177            .unwrap();
178
179        Self { num_workers }
180    }
181}