tempest 0.1.1

Realtime message handling framework inspired by Apache Storm and built with Actix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
mod console;
mod file;
mod log;
mod prelude;
mod prometheus;
mod statsd;

use actix::prelude::*;
use std::collections::HashMap;
use std::io::Error;
use std::net::AddrParseError;
use std::time::Duration;

use self::log::{Log, LogActor};
use crate::common::logger::*;
use crate::metric::{AggregateMetrics, Labels, MetricTarget, MetricValue, Metrics, Root};
use crate::service::agent_client::AgentClient;
use crate::service::cli::AgentOpt;
use crate::service::codec::AgentRequest;

use console::{Console, ConsoleActor};
use file::{File, FileActor};
use prometheus::{Prometheus, PrometheusActor};
use statsd::{Statsd, StatsdActor};

/// Type alias for backend results
pub(crate) type BackendResult<T> = Result<T, BackendError>;

/// A set of common backend error kinds
#[derive(Debug)]
#[allow(dead_code)]
pub(crate) enum BackendErrorKind {
    // General std::io::Error
    Io(std::io::Error),
    // Error parsing client address
    AddrParse(&'static str),
    /// Error kind when client connection encounters an error
    Client(String),
    /// Error kind when we just need one
    Other(String),
}

/// A backend error type
#[derive(Debug)]
pub(crate) struct BackendError {
    kind: BackendErrorKind,
}

impl BackendError {
    pub(crate) fn new(kind: BackendErrorKind) -> Self {
        BackendError { kind: kind }
    }

    pub(crate) fn from_other(err: String) -> Self {
        BackendError::new(BackendErrorKind::Other(err))
    }

    pub(crate) fn from_io(err: std::io::Error) -> Self {
        BackendError::new(BackendErrorKind::Io(err))
    }

    pub(crate) fn from_addr() -> Self {
        BackendError::new(BackendErrorKind::AddrParse("Address parsing error"))
    }
}

impl From<AddrParseError> for BackendError {
    fn from(_: AddrParseError) -> BackendError {
        BackendError::from_addr()
    }
}

impl From<Error> for BackendError {
    fn from(err: Error) -> BackendError {
        BackendError::from_io(err)
    }
}

/// Message type for sending metrics to backend targets
#[derive(Message, Clone)]
pub struct Msg {
    ///  Root metrics prefix
    pub root_prefix: String,
    ///  Root metrics labels
    pub root_labels: Labels,
    ///  Flushed metrics
    pub metrics: Metrics,
}

/// Structures that implement `Metrics` should subscribe to
/// `Flush` messages sent from the `MetricsBackendActor`.
// Subscribe(actor_name, recipient<Flush>)
#[derive(Message)]
pub struct Subscribe(pub &'static str, pub Recipient<Flush>);

/// MetricsBackendActor broadcasts this message type
/// to all subscribers.
#[derive(Message)]
pub struct Flush();

/// Backend actor for routing metrics to configured metrics targets
pub struct MetricsBackendActor {
    /// Console backend actor
    consoles: Vec<Addr<ConsoleActor>>,
    /// Log backend actor
    logs: Vec<Addr<LogActor>>,
    /// Prometheus backend actor
    proms: Vec<Addr<PrometheusActor>>,
    /// Statsd backend actor
    statsd: Vec<Addr<StatsdActor>>,
    /// File backend actor
    files: Vec<Addr<FileActor>>,
    /// Subscribers: Vec<(&'static str, Recipient<Flush>)>
    subscribers: Vec<Subscribe>,
    /// Topology.toml `[metric.flush_interval]`
    probe_interval: Duration,
    /// MetricsAggregateActor address
    pub aggregate: Option<Addr<MetricsAggregateActor>>,
}

impl Default for MetricsBackendActor {
    fn default() -> Self {
        Self {
            consoles: Vec::new(),
            logs: Vec::new(),
            proms: Vec::new(),
            statsd: Vec::new(),
            files: Vec::new(),
            subscribers: Vec::new(),
            probe_interval: Duration::from_millis(5000),
            aggregate: None,
        }
    }
}

impl MetricsBackendActor {
    pub fn subscribe(actor_name: &'static str, recipient: Recipient<Flush>) {
        let metric_backend = MetricsBackendActor::from_registry();
        if metric_backend.connected() {
            metric_backend.do_send(Subscribe(actor_name, recipient));
        }
    }

    // Probes
    fn probe(&mut self, ctx: &mut Context<Self>) {
        // send a message to each Actor subscribed to FlushMsg
        for subscriber in &self.subscribers {
            let _ = subscriber.1.do_send(Flush());
        }
        ctx.run_later(self.probe_interval, Self::probe);
    }

    fn start_console(&mut self, target: &MetricTarget) {
        info!("Starting metric console backend: {:?}", target);
        self.consoles.push(
            ConsoleActor {
                console: Console::new(target.clone()),
            }
            .start(),
        )
    }

    fn start_log(&mut self, target: &MetricTarget) {
        info!("Starting metric log backend: {:?}", target);
        self.logs.push(
            LogActor {
                log: Log::new(target.clone()),
            }
            .start(),
        )
    }

    fn start_prometheus(&mut self, target: &MetricTarget) {
        match Prometheus::new(target.clone()) {
            Ok(prom) => {
                info!("Starting metric prometheus backend: {:?}", target);
                self.proms
                    .push(PrometheusActor { prometheus: prom }.start())
            }
            Err(err) => {
                error!("Failed to start init prometheus backend: {:?}", err);
            }
        }
    }

    fn start_statsd(&mut self, target: &MetricTarget) {
        // Try parsing the configuration
        // host to make sure it's a valid address
        match Statsd::new(target.clone()) {
            Ok(statsd) => {
                info!("Starting metric statsd backend: {:?}", target);
                self.statsd.push(StatsdActor { statsd: statsd }.start())
            }
            Err(err) => {
                error!("Failed to start statsd client: {:?}", err);
            }
        }
    }

    fn start_file(&mut self, target: &MetricTarget, target_name: String) {
        match File::new(target.clone(), target_name) {
            Ok(file) => {
                info!("Starting metric file backend: {:?}", target);
                self.files.push(FileActor { file: file }.start())
            }
            Err(err) => {
                error!("Failed to start file backend: {:?}", err);
            }
        }
    }
}

impl Supervised for MetricsBackendActor {}
impl SystemService for MetricsBackendActor {}

impl Actor for MetricsBackendActor {
    type Context = Context<Self>;
    fn started(&mut self, ctx: &mut Context<Self>) {
        // This actor should read the root configuration values
        for target in Root::get_targets().iter() {
            match target {
                MetricTarget::Console { .. } => {
                    self.start_console(target);
                }
                MetricTarget::Log { .. } => {
                    self.start_log(target);
                }
                MetricTarget::Prometheus { .. } => {
                    self.start_prometheus(target);
                }
                MetricTarget::Statsd { .. } => {
                    self.start_statsd(target);
                }
                MetricTarget::File { .. } => {
                    let target_name = Root::get_target_name();
                    self.start_file(target, target_name);
                }
            }
        }

        // Set probe_interval using root.flush_interval
        self.probe_interval = Duration::from_millis(Root::get_flush_interval());
        debug!("Metrics probe_interval is {:?}", self.probe_interval);

        // Fire the probe
        self.probe(ctx);
    }
}

impl Handler<Msg> for MetricsBackendActor {
    type Result = ();

    fn handle(&mut self, msg: Msg, _ctx: &mut Context<Self>) {
        // println!("MetricsBackendActor#Handle");
        for addr in &self.consoles {
            addr.do_send(msg.clone());
        }
        for addr in &self.logs {
            addr.do_send(msg.clone());
        }
        for addr in &self.proms {
            addr.do_send(msg.clone());
        }
        for addr in &self.statsd {
            addr.do_send(msg.clone());
        }
        for addr in &self.files {
            addr.do_send(msg.clone());
        }

        // Check if MetricsAggregateActor is connected
        // and send this msg to it
        // This isn't a `backend` which is
        // configurable as a MetricTarget
        if let Some(aggregate) = &self.aggregate {
            aggregate.do_send(msg.clone());
        };
    }
}

impl Handler<Subscribe> for MetricsBackendActor {
    type Result = ();

    fn handle(&mut self, msg: Subscribe, _: &mut Self::Context) {
        // This should try and replace the exiting subscription
        let mut replaced = false;
        for subscriber in &mut self.subscribers {
            if subscriber.0 == msg.0 {
                subscriber.0 = msg.0;
                replaced = true;
                debug!("Re-subscribed {} to metrics backend", &msg.0);
            }
        }

        if !replaced {
            debug!("Subscribing {} to metrics backend", &msg.0);
            self.subscribers.push(msg);
        }
    }
}

/// Trait for writing backend metrics
#[allow(patterns_in_fns_without_body)]
pub trait Backend {
    // remove {} which stops mut warning here
    // and triggers patterns_in_fns_without_body warning
    // which is globally disallowed

    /// Write should implement sending metrics the end target.
    fn write(&mut self, mut msg: Msg);
}

/// Merge Labels into Map
pub fn merge_labels(map: &mut HashMap<String, String>, labels: Labels) {
    if labels.is_some() {
        for el in &labels.unwrap() {
            map.insert(el.0.clone(), el.1.clone());
        }
    }
}

/// MetricsAggregateActor collects metrics
/// sent to the MetricBackendActor.
/// This is currently only used for testing topology I/O
/// and isn't technically a backend (but more of
/// and plugin at runtime).
///
/// This actor only aggregates Counters.
pub struct MetricsAggregateActor {
    metrics: Metrics,
    agent_client: Addr<AgentClient>,
}

impl Default for MetricsAggregateActor {
    fn default() -> Self {
        Self {
            metrics: Metrics::default(),
            agent_client: AgentClient::connect(AgentOpt::default()),
        }
    }
}

impl MetricsAggregateActor {
    pub(crate) fn new(opts: AgentOpt) -> Self {
        Self {
            metrics: Metrics::default(),
            agent_client: AgentClient::connect(opts),
        }
    }
}

impl Supervised for MetricsAggregateActor {}
impl SystemService for MetricsAggregateActor {}

impl Actor for MetricsAggregateActor {
    type Context = Context<Self>;
    fn started(&mut self, _ctx: &mut Context<Self>) {
        trace!("MetricsAggregateActor started");
    }
}

impl Handler<Msg> for MetricsAggregateActor {
    type Result = ();

    fn handle(&mut self, mut msg: Msg, _ctx: &mut Context<Self>) {
        let mut labels_map = HashMap::new();

        // apply root first
        merge_labels(&mut labels_map, msg.root_labels);

        // apply metrics labels (overriding root)
        merge_labels(&mut labels_map, msg.metrics.labels);

        let mut name = vec![];

        if let Some(topology_name) = labels_map.get("topology_name") {
            name.push(topology_name.clone());
        }

        if let Some(task_name) = labels_map.get("task_name") {
            name.push(task_name.clone());
        }

        if msg.metrics.names.len() > 0 {
            name.append(&mut msg.metrics.names.clone());
        }

        let sep = ".";
        let name = name.join(&sep);
        let mut has_counters = false;

        for (_, metric) in msg.metrics.bucket.map.iter_mut() {
            match &metric.value {
                MetricValue::Counter(counter) => {
                    let mut parts = vec![name.clone(), metric.names.join(&sep)];
                    if metric.labels.is_some() {
                        let mut metric_labels = HashMap::new();
                        merge_labels(&mut metric_labels, metric.labels.clone());
                        if let Some(status) = &metric_labels.get("status") {
                            parts.push(status.to_string());
                        };
                    }
                    let key = parts.join(&sep);
                    warn!("Aggregate counter: {:?} {:?}", &key, &counter);
                    self.metrics.counter(vec![&key[..]], counter.value);
                    has_counters = true;
                }
                _ => {}
            }
        }

        // Convert metrics to Aggregate metrics
        // and send them to the AgentServer
        if has_counters {
            let mut aggregate = AggregateMetrics::default();
            for (_, metric) in self.metrics.bucket.map.iter() {
                match &metric.value {
                    MetricValue::Counter(counter) => {
                        aggregate.insert(metric.names[0].clone(), counter.value.clone());
                    }
                    _ => {}
                }
            }
            // println!("{:?}", &aggregate);
            self.agent_client
                .do_send(AgentRequest::AggregateMetricsPut(aggregate));
        }
    }
}