-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
mod.rs
760 lines (662 loc) · 22.1 KB
/
mod.rs
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
use self::proto::{event_wrapper::Event as EventProto, metric::Value as MetricProto, Log};
use bytes::Bytes;
use chrono::{DateTime, SecondsFormat, TimeZone, Utc};
use getset::{Getters, Setters};
use lazy_static::lazy_static;
use metric::{MetricKind, MetricValue};
use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::BTreeMap;
use std::iter::FromIterator;
use string_cache::DefaultAtom as Atom;
pub mod discriminant;
pub mod flatten;
pub mod merge;
pub mod merge_state;
pub mod metric;
mod unflatten;
pub use metric::Metric;
pub use unflatten::Unflatten;
pub mod proto {
include!(concat!(env!("OUT_DIR"), "/event.proto.rs"));
}
pub static LOG_SCHEMA: OnceCell<LogSchema> = OnceCell::new();
lazy_static! {
pub static ref PARTIAL: Atom = Atom::from("_partial");
}
#[derive(PartialEq, Debug, Clone)]
pub enum Event {
Log(LogEvent),
Metric(Metric),
}
#[derive(PartialEq, Debug, Clone)]
pub struct LogEvent {
fields: BTreeMap<Atom, Value>,
}
impl Event {
pub fn new_empty_log() -> Self {
Event::Log(LogEvent {
fields: BTreeMap::new(),
})
}
pub fn as_log(&self) -> &LogEvent {
match self {
Event::Log(log) => log,
_ => panic!("failed type coercion, {:?} is not a log event", self),
}
}
pub fn as_mut_log(&mut self) -> &mut LogEvent {
match self {
Event::Log(log) => log,
_ => panic!("failed type coercion, {:?} is not a log event", self),
}
}
pub fn into_log(self) -> LogEvent {
match self {
Event::Log(log) => log,
_ => panic!("failed type coercion, {:?} is not a log event", self),
}
}
pub fn as_metric(&self) -> &Metric {
match self {
Event::Metric(metric) => metric,
_ => panic!("failed type coercion, {:?} is not a metric", self),
}
}
pub fn as_mut_metric(&mut self) -> &mut Metric {
match self {
Event::Metric(metric) => metric,
_ => panic!("failed type coercion, {:?} is not a metric", self),
}
}
pub fn into_metric(self) -> Metric {
match self {
Event::Metric(metric) => metric,
_ => panic!("failed type coercion, {:?} is not a metric", self),
}
}
}
impl LogEvent {
pub fn get(&self, key: &Atom) -> Option<&Value> {
self.fields.get(key)
}
pub fn get_mut(&mut self, key: &Atom) -> Option<&mut Value> {
self.fields.get_mut(key)
}
pub fn contains(&self, key: &Atom) -> bool {
self.fields.contains_key(key)
}
pub fn insert<K, V>(&mut self, key: K, value: V)
where
K: Into<Atom>,
V: Into<Value>,
{
self.fields.insert(key.into(), value.into());
}
pub fn remove(&mut self, key: &Atom) -> Option<Value> {
self.fields.remove(key)
}
pub fn keys(&self) -> impl Iterator<Item = &Atom> {
self.fields.keys()
}
pub fn all_fields(&self) -> FieldsIter {
FieldsIter {
inner: self.fields.iter(),
}
}
pub fn into_iter(self) -> impl Iterator<Item = (Atom, Value)> {
self.fields.into_iter()
}
pub fn unflatten(self) -> unflatten::Unflatten {
unflatten::Unflatten::from(self.fields)
}
}
impl std::ops::Index<&Atom> for LogEvent {
type Output = Value;
fn index(&self, key: &Atom) -> &Value {
&self.fields[key]
}
}
impl<K: Into<Atom>, V: Into<Value>> Extend<(K, V)> for LogEvent {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I) {
for (k, v) in iter {
self.insert(k.into(), v.into());
}
}
}
// Allow converting any kind of appropriate key/value iterator directly into a LogEvent.
impl<K: Into<Atom>, V: Into<Value>> FromIterator<(K, V)> for LogEvent {
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
let mut log_event = Event::new_empty_log().into_log();
log_event.extend(iter);
log_event
}
}
impl Serialize for LogEvent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_map(self.fields.iter())
}
}
pub fn log_schema() -> &'static LogSchema {
// TODO: Help Rust project support before_each
// Support uninitialized schemas in tests to help our contributors.
// Don't do it in release because that is scary.
#[cfg(debug_assertions)]
{
if LOG_SCHEMA.get().is_none() {
error!("You are not initializing a schema in this test -- This could fail in release");
LOG_SCHEMA.set(LogSchema::default()).ok(); // If this fails it means some other test set it while we were trying to.
}
}
LOG_SCHEMA.get().expect("Schema was not initialized")
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Getters, Setters)]
pub struct LogSchema {
#[getset(get = "pub", set = "pub(crate)")]
message_key: Atom,
#[getset(get = "pub", set = "pub(crate)")]
timestamp_key: Atom,
#[getset(get = "pub", set = "pub(crate)")]
host_key: Atom,
}
impl Default for LogSchema {
fn default() -> Self {
LogSchema {
message_key: Atom::from("message"),
timestamp_key: Atom::from("timestamp"),
host_key: Atom::from("host"),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum Value {
Bytes(Bytes),
Integer(i64),
Float(f64),
Boolean(bool),
Timestamp(DateTime<Utc>),
Map(BTreeMap<Atom, Value>),
Array(Vec<Value>),
}
impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match &self {
Value::Integer(i) => serializer.serialize_i64(*i),
Value::Float(f) => serializer.serialize_f64(*f),
Value::Boolean(b) => serializer.serialize_bool(*b),
Value::Bytes(_) | Value::Timestamp(_) => {
serializer.serialize_str(&self.to_string_lossy())
}
Value::Map(m) => serializer.collect_map(m),
Value::Array(a) => serializer.collect_seq(a),
}
}
}
impl From<Bytes> for Value {
fn from(bytes: Bytes) -> Self {
Value::Bytes(bytes)
}
}
impl From<Vec<u8>> for Value {
fn from(bytes: Vec<u8>) -> Self {
Value::Bytes(bytes.into())
}
}
impl From<&[u8]> for Value {
fn from(bytes: &[u8]) -> Self {
Value::Bytes(bytes.into())
}
}
impl From<String> for Value {
fn from(string: String) -> Self {
Value::Bytes(string.into())
}
}
impl From<&str> for Value {
fn from(s: &str) -> Self {
Value::Bytes(s.into())
}
}
impl From<DateTime<Utc>> for Value {
fn from(timestamp: DateTime<Utc>) -> Self {
Value::Timestamp(timestamp)
}
}
impl From<f32> for Value {
fn from(value: f32) -> Self {
Value::Float(f64::from(value))
}
}
impl From<f64> for Value {
fn from(value: f64) -> Self {
Value::Float(value)
}
}
impl From<BTreeMap<Atom, Value>> for Value {
fn from(value: BTreeMap<Atom, Value>) -> Self {
Value::Map(value)
}
}
impl From<Vec<Value>> for Value {
fn from(value: Vec<Value>) -> Self {
Value::Array(value)
}
}
macro_rules! impl_valuekind_from_integer {
($t:ty) => {
impl From<$t> for Value {
fn from(value: $t) -> Self {
Value::Integer(value as i64)
}
}
};
}
impl_valuekind_from_integer!(i64);
impl_valuekind_from_integer!(i32);
impl_valuekind_from_integer!(i16);
impl_valuekind_from_integer!(i8);
impl_valuekind_from_integer!(isize);
impl From<bool> for Value {
fn from(value: bool) -> Self {
Value::Boolean(value)
}
}
impl Value {
// TODO: return Cow
pub fn to_string_lossy(&self) -> String {
match self {
Value::Bytes(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
Value::Timestamp(timestamp) => timestamp_to_string(timestamp),
Value::Integer(num) => format!("{}", num),
Value::Float(num) => format!("{}", num),
Value::Boolean(b) => format!("{}", b),
Value::Map(map) => serde_json::to_string(map).expect("Cannot serialize map"),
Value::Array(arr) => serde_json::to_string(arr).expect("Cannot serialize array"),
}
}
pub fn as_bytes(&self) -> Bytes {
match self {
Value::Bytes(bytes) => bytes.clone(), // cloning a Bytes is cheap
Value::Timestamp(timestamp) => Bytes::from(timestamp_to_string(timestamp)),
Value::Integer(num) => Bytes::from(format!("{}", num)),
Value::Float(num) => Bytes::from(format!("{}", num)),
Value::Boolean(b) => Bytes::from(format!("{}", b)),
Value::Map(map) => Bytes::from(serde_json::to_vec(map).expect("Cannot serialize map")),
Value::Array(arr) => {
Bytes::from(serde_json::to_vec(arr).expect("Cannot serialize array"))
}
}
}
pub fn into_bytes(self) -> Bytes {
self.as_bytes()
}
pub fn as_timestamp(&self) -> Option<&DateTime<Utc>> {
match &self {
Value::Timestamp(ts) => Some(ts),
_ => None,
}
}
}
fn timestamp_to_string(timestamp: &DateTime<Utc>) -> String {
timestamp.to_rfc3339_opts(SecondsFormat::AutoSi, true)
}
fn decode_map(fields: BTreeMap<String, proto::Value>) -> Option<Value> {
let mut accum: BTreeMap<Atom, Value> = BTreeMap::new();
for (key, value) in fields {
match decode_value(value) {
Some(value) => {
accum.insert(Atom::from(key), value);
}
None => return None,
}
}
Some(Value::Map(accum))
}
fn decode_array(items: Vec<proto::Value>) -> Option<Value> {
let mut accum = Vec::with_capacity(items.len());
for value in items {
match decode_value(value) {
Some(value) => accum.push(value),
None => return None,
}
}
Some(Value::Array(accum))
}
fn decode_value(input: proto::Value) -> Option<Value> {
match input.kind {
Some(proto::value::Kind::RawBytes(data)) => Some(Value::Bytes(data.into())),
Some(proto::value::Kind::Timestamp(ts)) => Some(Value::Timestamp(
chrono::Utc.timestamp(ts.seconds, ts.nanos as u32),
)),
Some(proto::value::Kind::Integer(value)) => Some(Value::Integer(value)),
Some(proto::value::Kind::Float(value)) => Some(Value::Float(value)),
Some(proto::value::Kind::Boolean(value)) => Some(Value::Boolean(value)),
Some(proto::value::Kind::Map(map)) => decode_map(map.fields),
Some(proto::value::Kind::Array(array)) => decode_array(array.items),
None => {
error!("encoded event contains unknown value kind");
None
}
}
}
impl From<proto::EventWrapper> for Event {
fn from(proto: proto::EventWrapper) -> Self {
let event = proto.event.unwrap();
match event {
EventProto::Log(proto) => {
let fields = proto
.fields
.into_iter()
.filter_map(|(k, v)| decode_value(v).map(|value| (Atom::from(k), value)))
.collect::<BTreeMap<_, _>>();
Event::Log(LogEvent { fields })
}
EventProto::Metric(proto) => {
let kind = match proto.kind() {
proto::metric::Kind::Incremental => MetricKind::Incremental,
proto::metric::Kind::Absolute => MetricKind::Absolute,
};
let name = proto.name;
let timestamp = proto
.timestamp
.map(|ts| chrono::Utc.timestamp(ts.seconds, ts.nanos as u32));
let tags = if !proto.tags.is_empty() {
Some(proto.tags)
} else {
None
};
let value = match proto.value.unwrap() {
MetricProto::Counter(counter) => MetricValue::Counter {
value: counter.value,
},
MetricProto::Gauge(gauge) => MetricValue::Gauge { value: gauge.value },
MetricProto::Set(set) => MetricValue::Set {
values: set.values.into_iter().collect(),
},
MetricProto::Distribution(dist) => MetricValue::Distribution {
values: dist.values,
sample_rates: dist.sample_rates,
},
MetricProto::AggregatedHistogram(hist) => MetricValue::AggregatedHistogram {
buckets: hist.buckets,
counts: hist.counts,
count: hist.count,
sum: hist.sum,
},
MetricProto::AggregatedSummary(summary) => MetricValue::AggregatedSummary {
quantiles: summary.quantiles,
values: summary.values,
count: summary.count,
sum: summary.sum,
},
};
Event::Metric(Metric {
name,
timestamp,
tags,
kind,
value,
})
}
}
}
}
fn encode_value(value: Value) -> proto::Value {
proto::Value {
kind: match value {
Value::Bytes(b) => Some(proto::value::Kind::RawBytes(b.to_vec())),
Value::Timestamp(ts) => Some(proto::value::Kind::Timestamp(prost_types::Timestamp {
seconds: ts.timestamp(),
nanos: ts.timestamp_subsec_nanos() as i32,
})),
Value::Integer(value) => Some(proto::value::Kind::Integer(value)),
Value::Float(value) => Some(proto::value::Kind::Float(value)),
Value::Boolean(value) => Some(proto::value::Kind::Boolean(value)),
Value::Map(fields) => Some(proto::value::Kind::Map(encode_map(fields))),
Value::Array(items) => Some(proto::value::Kind::Array(encode_array(items))),
},
}
}
fn encode_map(fields: BTreeMap<Atom, Value>) -> proto::ValueMap {
proto::ValueMap {
fields: fields
.into_iter()
.map(|(key, value)| (key.to_string(), encode_value(value)))
.collect(),
}
}
fn encode_array(items: Vec<Value>) -> proto::ValueArray {
proto::ValueArray {
items: items.into_iter().map(|value| encode_value(value)).collect(),
}
}
impl From<Event> for proto::EventWrapper {
fn from(event: Event) -> Self {
match event {
Event::Log(LogEvent { fields }) => {
let fields = fields
.into_iter()
.map(|(k, v)| (k.to_string(), encode_value(v)))
.collect::<BTreeMap<_, _>>();
let event = EventProto::Log(Log { fields });
proto::EventWrapper { event: Some(event) }
}
Event::Metric(Metric {
name,
timestamp,
tags,
kind,
value,
}) => {
let timestamp = timestamp.map(|ts| prost_types::Timestamp {
seconds: ts.timestamp(),
nanos: ts.timestamp_subsec_nanos() as i32,
});
let tags = tags.unwrap_or_default();
let kind = match kind {
MetricKind::Incremental => proto::metric::Kind::Incremental,
MetricKind::Absolute => proto::metric::Kind::Absolute,
}
.into();
let metric = match value {
MetricValue::Counter { value } => {
MetricProto::Counter(proto::Counter { value })
}
MetricValue::Gauge { value } => MetricProto::Gauge(proto::Gauge { value }),
MetricValue::Set { values } => MetricProto::Set(proto::Set {
values: values.into_iter().collect(),
}),
MetricValue::Distribution {
values,
sample_rates,
} => MetricProto::Distribution(proto::Distribution {
values,
sample_rates,
}),
MetricValue::AggregatedHistogram {
buckets,
counts,
count,
sum,
} => MetricProto::AggregatedHistogram(proto::AggregatedHistogram {
buckets,
counts,
count,
sum,
}),
MetricValue::AggregatedSummary {
quantiles,
values,
count,
sum,
} => MetricProto::AggregatedSummary(proto::AggregatedSummary {
quantiles,
values,
count,
sum,
}),
};
let event = EventProto::Metric(proto::Metric {
name,
timestamp,
tags,
kind,
value: Some(metric),
});
proto::EventWrapper { event: Some(event) }
}
}
}
}
// TODO: should probably get rid of this
impl From<Event> for Vec<u8> {
fn from(event: Event) -> Vec<u8> {
event
.into_log()
.remove(&log_schema().message_key())
.unwrap()
.as_bytes()
.to_vec()
}
}
impl From<Bytes> for Event {
fn from(message: Bytes) -> Self {
let mut event = Event::Log(LogEvent {
fields: BTreeMap::new(),
});
event
.as_mut_log()
.insert(log_schema().message_key().clone(), message);
event
.as_mut_log()
.insert(log_schema().timestamp_key().clone(), Utc::now());
event
}
}
impl From<&str> for Event {
fn from(line: &str) -> Self {
line.to_owned().into()
}
}
impl From<String> for Event {
fn from(line: String) -> Self {
Bytes::from(line).into()
}
}
impl From<LogEvent> for Event {
fn from(log: LogEvent) -> Self {
Event::Log(log)
}
}
impl From<Metric> for Event {
fn from(metric: Metric) -> Self {
Event::Metric(metric)
}
}
#[derive(Clone)]
pub struct FieldsIter<'a> {
inner: std::collections::btree_map::Iter<'a, Atom, Value>,
}
impl<'a> Iterator for FieldsIter<'a> {
type Item = (&'a Atom, &'a Value);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a> Serialize for FieldsIter<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_map(self.clone())
}
}
#[cfg(test)]
mod test {
use super::{Atom, Event, Value};
use regex::Regex;
use std::collections::HashSet;
#[test]
fn serialization() {
let mut event = Event::from("raw log line");
event.as_mut_log().insert("foo", "bar");
event.as_mut_log().insert("bar", "baz");
let expected_all = serde_json::json!({
"message": "raw log line",
"foo": "bar",
"bar": "baz",
"timestamp": event.as_log().get(&super::log_schema().timestamp_key()),
});
let actual_all = serde_json::to_value(event.as_log().all_fields()).unwrap();
assert_eq!(expected_all, actual_all);
let rfc3339_re = Regex::new(r"\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+Z\z").unwrap();
assert!(rfc3339_re.is_match(actual_all.pointer("/timestamp").unwrap().as_str().unwrap()));
}
#[test]
fn type_serialization() {
use serde_json::json;
let mut event = Event::from("hello world");
event.as_mut_log().insert("int", 4);
event.as_mut_log().insert("float", 5.5);
event.as_mut_log().insert("bool", true);
event.as_mut_log().insert("string", "thisisastring");
let map = serde_json::to_value(event.as_log().all_fields()).unwrap();
assert_eq!(map["float"], json!(5.5));
assert_eq!(map["int"], json!(4));
assert_eq!(map["bool"], json!(true));
assert_eq!(map["string"], json!("thisisastring"));
}
#[test]
fn event_iteration() {
let mut event = Event::new_empty_log();
event
.as_mut_log()
.insert("Ke$ha", "It's going down, I'm yelling timber");
event
.as_mut_log()
.insert("Pitbull", "The bigger they are, the harder they fall");
let all = event
.as_log()
.all_fields()
.map(|(k, v)| (k, v.to_string_lossy()))
.collect::<HashSet<_>>();
assert_eq!(
all,
vec![
(
&"Ke$ha".into(),
"It's going down, I'm yelling timber".to_string()
),
(
&"Pitbull".into(),
"The bigger they are, the harder they fall".to_string()
),
]
.into_iter()
.collect::<HashSet<_>>()
);
}
#[test]
fn event_iteration_order() {
let mut event = Event::new_empty_log();
let log = event.as_mut_log();
log.insert(&Atom::from("lZDfzKIL"), Value::from("tOVrjveM"));
log.insert(&Atom::from("o9amkaRY"), Value::from("pGsfG7Nr"));
log.insert(&Atom::from("YRjhxXcg"), Value::from("nw8iM5Jr"));
let collected: Vec<_> = log.all_fields().collect();
assert_eq!(
collected,
vec![
(&Atom::from("YRjhxXcg"), &Value::from("nw8iM5Jr")),
(&Atom::from("lZDfzKIL"), &Value::from("tOVrjveM")),
(&Atom::from("o9amkaRY"), &Value::from("pGsfG7Nr")),
]
);
}
}