-
Notifications
You must be signed in to change notification settings - Fork 85
/
protocol.rs
990 lines (889 loc) · 30.7 KB
/
protocol.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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use std::collections::BTreeMap;
use std::fmt;
use std::iter::FusedIterator;
use hash32::{FnvHasher, Hasher};
use serde::{Deserialize, Serialize};
pub use relay_common::UnixTimestamp;
/// Time duration units used in [`MetricUnit::Duration`].
///
/// Defaults to `millisecond`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum DurationUnit {
/// Nanosecond (`"nanosecond"`), 10^-9 seconds.
NanoSecond,
/// Microsecond (`"microsecond"`), 10^-6 seconds.
MicroSecond,
/// Millisecond (`"millisecond"`), 10^-3 seconds.
MilliSecond,
/// Full second (`"second"`).
Second,
/// Minute (`"minute"`), 60 seconds.
Minute,
/// Hour (`"hour"`), 3600 seconds.
Hour,
/// Day (`"day"`), 86,400 seconds.
Day,
/// Week (`"week"`), 604,800 seconds.
Week,
}
impl Default for DurationUnit {
fn default() -> Self {
Self::MilliSecond
}
}
impl fmt::Display for DurationUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NanoSecond => f.write_str("nanosecond"),
Self::MicroSecond => f.write_str("microsecond"),
Self::MilliSecond => f.write_str("millisecond"),
Self::Second => f.write_str("second"),
Self::Minute => f.write_str("minute"),
Self::Hour => f.write_str("hour"),
Self::Day => f.write_str("day"),
Self::Week => f.write_str("week"),
}
}
}
/// Size of information derived from bytes, used in [`MetricUnit::Information`].
///
/// Defaults to `byte`. See also [Units of
/// information](https://en.wikipedia.org/wiki/Units_of_information).
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum InformationUnit {
/// Bit (`"bit"`), corresponding to 1/8 of a byte.
///
/// Note that there are computer systems with a different number of bits per byte.
Bit,
/// Byte (`"byte"`).
Byte,
/// Kilobyte (`"kilobyte"`), 10^3 bytes.
KiloByte,
/// Kibibyte (`"kibibyte"`), 2^10 bytes.
KibiByte,
/// Megabyte (`"megabyte"`), 10^6 bytes.
MegaByte,
/// Mebibyte (`"mebibyte"`), 2^20 bytes.
MebiByte,
/// Gigabyte (`"gigabyte"`), 10^9 bytes.
GigaByte,
/// Gibibyte (`"gibibyte"`), 2^30 bytes.
GibiByte,
/// Terabyte (`"terabyte"`), 10^12 bytes.
TeraByte,
/// Tebibyte (`"tebibyte"`), 2^40 bytes.
TebiByte,
/// Petabyte (`"petabyte"`), 10^15 bytes.
PetaByte,
/// Pebibyte (`"pebibyte"`), 2^50 bytes.
PebiByte,
/// Exabyte (`"exabyte"`), 10^18 bytes.
ExaByte,
/// Exbibyte (`"exbibyte"`), 2^60 bytes.
ExbiByte,
}
impl Default for InformationUnit {
fn default() -> Self {
Self::Byte
}
}
impl fmt::Display for InformationUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bit => f.write_str("bit"),
Self::Byte => f.write_str("byte"),
Self::KiloByte => f.write_str("kilobyte"),
Self::KibiByte => f.write_str("kibibyte"),
Self::MegaByte => f.write_str("megabyte"),
Self::MebiByte => f.write_str("mebibyte"),
Self::GigaByte => f.write_str("gigabyte"),
Self::GibiByte => f.write_str("gibibyte"),
Self::TeraByte => f.write_str("terabyte"),
Self::TebiByte => f.write_str("tebibyte"),
Self::PetaByte => f.write_str("petabyte"),
Self::PebiByte => f.write_str("pebibyte"),
Self::ExaByte => f.write_str("exabyte"),
Self::ExbiByte => f.write_str("exbibyte"),
}
}
}
/// Units of fraction used in [`MetricUnit::Fraction`].
///
/// Defaults to `ratio`.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum FractionUnit {
/// Floating point fraction of `1`.
Ratio,
/// Ratio expressed as a fraction of `100`. `100%` equals a ratio of `1.0`.
Percent,
}
impl Default for FractionUnit {
fn default() -> Self {
Self::Ratio
}
}
impl fmt::Display for FractionUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Ratio => f.write_str("ratio"),
Self::Percent => f.write_str("percent"),
}
}
}
/// Custom user-defined units without builtin conversion.
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct CustomUnit([u8; 15]);
impl CustomUnit {
/// Parses a `CustomUnit` from a string.
pub fn parse(s: &str) -> Result<Self, ParseMetricError> {
if s.len() > 15 || !s.is_ascii() {
return Err(ParseMetricError(()));
}
let mut unit = Self(Default::default());
unit.0.copy_from_slice(s.as_bytes());
unit.0.make_ascii_lowercase();
Ok(unit)
}
/// Returns the string representation of this unit.
#[inline]
pub fn as_str(&self) -> &str {
// Safety: The string is already validated to be of length 32 and valid ASCII when
// constructing `ProjectKey`.
unsafe { std::str::from_utf8_unchecked(&self.0) }
}
}
impl fmt::Debug for CustomUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl fmt::Display for CustomUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl std::str::FromStr for CustomUnit {
type Err = ParseMetricError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
}
}
impl std::ops::Deref for CustomUnit {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
/// The [unit](Metric::unit) of measurement of a metric [value](Metric::value).
///
/// Units augment metric values by giving them a magnitude and semantics. There are certain types of
/// units that are subdivided in their precision, such as the [`DurationUnit`] for time
/// measurements.
///
/// Units and their precisions are uniquely represented by a string identifier.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum MetricUnit {
/// A time duration, defaulting to `"millisecond"`.
Duration(DurationUnit),
/// Size of information derived from bytes, defaulting to `"byte"`.
Information(InformationUnit),
/// Fractions such as percentages, defaulting to `"ratio"`.
Fraction(FractionUnit),
/// user-defined units without builtin conversion or default.
Custom(CustomUnit),
/// Untyped value without a unit (`""`).
None,
}
impl MetricUnit {
/// Returns `true` if the metric_unit is [`None`].
pub fn is_none(&self) -> bool {
matches!(self, Self::None)
}
}
impl Default for MetricUnit {
fn default() -> Self {
MetricUnit::None
}
}
impl fmt::Display for MetricUnit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MetricUnit::Duration(u) => u.fmt(f),
MetricUnit::Information(u) => u.fmt(f),
MetricUnit::Fraction(u) => u.fmt(f),
MetricUnit::Custom(u) => u.fmt(f),
MetricUnit::None => f.write_str("none"),
}
}
}
impl std::str::FromStr for MetricUnit {
type Err = ParseMetricError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"nanosecond" | "ns" => Self::Duration(DurationUnit::NanoSecond),
"microsecond" => Self::Duration(DurationUnit::MicroSecond),
"millisecond" | "ms" => Self::Duration(DurationUnit::MilliSecond),
"second" | "s" => Self::Duration(DurationUnit::Second),
"minute" => Self::Duration(DurationUnit::Minute),
"hour" => Self::Duration(DurationUnit::Hour),
"day" => Self::Duration(DurationUnit::Day),
"week" => Self::Duration(DurationUnit::Week),
"bit" => Self::Information(InformationUnit::Bit),
"byte" => Self::Information(InformationUnit::Byte),
"kilobyte" => Self::Information(InformationUnit::KiloByte),
"kibibyte" => Self::Information(InformationUnit::KibiByte),
"megabyte" => Self::Information(InformationUnit::MegaByte),
"mebibyte" => Self::Information(InformationUnit::MebiByte),
"gigabyte" => Self::Information(InformationUnit::GigaByte),
"gibibyte" => Self::Information(InformationUnit::GibiByte),
"terabyte" => Self::Information(InformationUnit::TeraByte),
"tebibyte" => Self::Information(InformationUnit::TebiByte),
"petabyte" => Self::Information(InformationUnit::PetaByte),
"pebibyte" => Self::Information(InformationUnit::PebiByte),
"exabyte" => Self::Information(InformationUnit::ExaByte),
"exbibyte" => Self::Information(InformationUnit::ExbiByte),
"ratio" => Self::Fraction(FractionUnit::Ratio),
"percent" => Self::Fraction(FractionUnit::Percent),
"" | "unit" | "none" => Self::None,
_ => Self::Custom(CustomUnit::parse(s)?),
})
}
}
relay_common::impl_str_serde!(MetricUnit, "a metric unit string");
/// The [typed value](Metric::value) of a metric.
#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type", content = "value")]
pub enum MetricValue {
/// Counts instances of an event. See [`MetricType::Counter`].
#[serde(rename = "c")]
Counter(f64),
/// Builds a statistical distribution over values reported. See [`MetricType::Distribution`].
#[serde(rename = "d")]
Distribution(f64),
/// Counts the number of unique reported values. See [`MetricType::Set`].
///
/// Set values can be specified as strings in the submission protocol. They are always hashed
/// into a 32-bit value and the original value is dropped. If the submission protocol contains a
/// 32-bit integer, it will be used directly, instead.
#[serde(rename = "s")]
Set(u32),
/// Stores absolute snapshots of values. See [`MetricType::Gauge`].
#[serde(rename = "g")]
Gauge(f64),
}
impl MetricValue {
/// Creates a [`MetricValue::Set`] from the given string.
pub fn set_from_str(string: &str) -> Self {
Self::Set(hash_set_value(string))
}
/// Creates a [`MetricValue::Set`] from any type that implements `Display`.
pub fn set_from_display(display: impl fmt::Display) -> Self {
Self::set_from_str(&display.to_string())
}
/// Returns the type of this value.
pub fn ty(&self) -> MetricType {
match self {
Self::Counter(_) => MetricType::Counter,
Self::Distribution(_) => MetricType::Distribution,
Self::Set(_) => MetricType::Set,
Self::Gauge(_) => MetricType::Gauge,
}
}
}
impl fmt::Display for MetricValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MetricValue::Counter(value) => value.fmt(f),
MetricValue::Distribution(value) => value.fmt(f),
MetricValue::Set(value) => value.fmt(f),
MetricValue::Gauge(value) => value.fmt(f),
}
}
}
/// The type of a [`MetricValue`], determining its aggregation and evaluation.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum MetricType {
/// Counts instances of an event.
///
/// Counters can be incremented and decremented. The default operation is to increment a counter
/// by `1`, although increments by larger values are equally possible.
Counter,
/// Builds a statistical distribution over values reported.
///
/// Based on individual reported values, distributions allow to query the maximum, minimum, or
/// average of the reported values, as well as statistical quantiles. With an increasing number
/// of values in the distribution, its accuracy becomes approximate.
Distribution,
/// Counts the number of unique reported values.
///
/// Sets allow sending arbitrary discrete values, including strings, and store the deduplicated
/// count. With an increasing number of unique values in the set, its accuracy becomes
/// approximate. It is not possible to query individual values from a set.
Set,
/// Stores absolute snapshots of values.
///
/// In addition to plain [counters](Self::Counter), gauges store a snapshot of the maximum,
/// minimum and sum of all values, as well as the last reported value.
Gauge,
}
impl MetricType {
/// Return the shortcode for this metric type.
pub fn as_str(&self) -> &'static str {
match self {
MetricType::Counter => "c",
MetricType::Distribution => "d",
MetricType::Set => "s",
MetricType::Gauge => "g",
}
}
}
impl fmt::Display for MetricType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for MetricType {
type Err = ParseMetricError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"c" | "m" => Self::Counter,
"h" | "d" | "ms" => Self::Distribution,
"s" => Self::Set,
"g" => Self::Gauge,
_ => return Err(ParseMetricError(())),
})
}
}
relay_common::impl_str_serde!(MetricType, "a metric type string");
/// An error returned by [`Metric::parse`] and [`Metric::parse_all`].
#[derive(Clone, Copy, Debug)]
pub struct ParseMetricError(());
impl fmt::Display for ParseMetricError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to parse metric")
}
}
/// Validates a metric name.
///
/// Metric names cannot be empty, must begin with a letter and can consist of ASCII alphanumerics,
/// underscores and periods.
fn is_valid_name(name: &str) -> bool {
let mut iter = name.as_bytes().iter();
if let Some(first_byte) = iter.next() {
if first_byte.is_ascii_alphabetic() {
return iter.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_'));
}
}
false
}
/// Parses the `name[@unit]` part of a metric string.
///
/// Returns [`MetricUnit::None`] if no unit is specified. Returns `None` if the name or value are
/// invalid.
fn parse_name_unit(string: &str) -> Option<(String, MetricUnit)> {
let mut components = string.split('@');
let name = components.next()?;
if !is_valid_name(name) {
return None;
}
let unit = match components.next() {
Some(s) => s.parse().ok()?,
None => MetricUnit::default(),
};
Some((name.to_owned(), unit))
}
/// Hashes the given set value.
///
/// Sets only guarantee 32-bit accuracy, but arbitrary strings are allowed on the protocol. Upon
/// parsing, they are hashed and only used as hashes subsequently.
fn hash_set_value(string: &str) -> u32 {
let mut hasher = FnvHasher::default();
hasher.write(string.as_bytes());
hasher.finish()
}
/// Parses a metric value given its type.
///
/// Returns `None` if the value is invalid for the given type.
fn parse_value(string: &str, ty: MetricType) -> Option<MetricValue> {
Some(match ty {
MetricType::Counter => MetricValue::Counter(string.parse().ok()?),
MetricType::Distribution => MetricValue::Distribution(string.parse().ok()?),
MetricType::Set => {
MetricValue::Set(string.parse().unwrap_or_else(|_| hash_set_value(string)))
}
MetricType::Gauge => MetricValue::Gauge(string.parse().ok()?),
})
}
/// Parses the `name[@unit]:value` part of a metric string.
///
/// Returns [`MetricUnit::None`] if no unit is specified. Returns `None` if any of the components is
/// invalid.
fn parse_name_unit_value(
string: &str,
ty: MetricType,
) -> Option<(String, MetricUnit, MetricValue)> {
let mut components = string.splitn(2, ':');
let (name, unit) = components.next().and_then(parse_name_unit)?;
let value = components.next().and_then(|s| parse_value(s, ty))?;
Some((name, unit, value))
}
/// Parses tags in the format `tag1,tag2:value`.
///
/// Tag values are optional. For tags with missing values, an empty `""` value is assumed.
fn parse_tags(string: &str) -> Option<BTreeMap<String, String>> {
let mut map = BTreeMap::new();
for pair in string.split(',') {
let mut name_value = pair.splitn(2, ':');
let name = name_value.next()?;
let value = name_value.next().unwrap_or_default();
map.insert(name.to_owned(), value.to_owned());
}
Some(map)
}
/// A single metric value representing the payload sent from clients.
///
/// As opposed to bucketed metric aggregations, this single metrics always represent a single
/// submission and cannot store multiple values.
///
/// See the [crate documentation](crate) for general information on Metrics.
///
/// # Submission Protocol
///
/// ```text
/// <name>[@unit]:<value>|<type>|#<tag_key>:<tag_value>,<tag>
/// ```
///
/// See the field documentation on this struct for more information on the components. An example
/// submission looks like this:
///
/// ```text
/// endpoint.response_time@millisecond:57|d|#route:user_index
/// endpoint.hits:1|c|#route:user_index
/// ```
///
/// To parse a submission payload, use [`Metric::parse_all`].
///
/// # JSON Representation
///
/// In addition to the submission protocol, metrics can be represented as structured data in JSON.
/// In addition to the field values from the submission protocol, a timestamp is added to every
/// metric (see [crate documentation](crate)).
///
/// ```json
/// {
/// "name": "endpoint.response_time",
/// "unit": "millisecond",
/// "value": 57,
/// "type": "d",
/// "timestamp": 1615889449,
/// "tags": {
/// "route": "user_index"
/// }
/// }
/// ```
///
/// # Hashing of Sets
///
/// Set values can be specified as strings in the submission protocol. They are always hashed
/// into a 32-bit value and the original value is dropped. If the submission protocol contains a
/// 32-bit integer, it will be used directly, instead.
///
/// **Example**:
///
/// ```text
/// endpoint.users:e2546e4c-ecd0-43ad-ae27-87960e57a658|s
/// ```
///
/// The above submission is represented as:
///
/// ```json
/// {
/// "name": "endpoint.users",
/// "value": 4267882815,
/// "type": "s"
/// }
/// ```
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Metric {
/// The name of the metric without its unit.
///
/// Metric names cannot be empty, must start with a letter and can consist of ASCII
/// alphanumerics, underscores and periods.
pub name: String,
/// The unit of the metric value.
///
/// Units augment metric values by giving them a magnitude and semantics. There are certain
/// types of units that are subdivided in their precision, such as the [`DurationUnit`] for time
/// measurements.
///
/// The unit can be omitted and defaults to [`MetricUnit::None`].
#[serde(default, skip_serializing_if = "MetricUnit::is_none")]
pub unit: MetricUnit,
/// The value of the metric.
///
/// [Distributions](MetricType::Distribution) and [counters](MetricType::Counter) require numeric
/// values which can either be integral or floating point. In contrast, [sets](MetricType::Set)
/// and [gauges](MetricType::Gauge) can store any unique value including custom strings.
#[serde(flatten)]
pub value: MetricValue,
/// The timestamp for this metric value.
///
/// If a timestamp is not supplied in the item header of the envelope, the
/// default timestamp supplied to [`Metric::parse`] or [`Metric::parse_all`]
/// is associated with the metric.
pub timestamp: UnixTimestamp,
/// A list of tags adding dimensions to the metric for filtering and aggregation.
///
/// Tags are preceded with a hash `#` and specified in a comma (`,`) separated list. Each tag
/// can either be a tag name, or a `name:value` combination. For tags with missing values, an
/// empty `""` value is assumed.
///
/// Tags are optional and can be omitted.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub tags: BTreeMap<String, String>,
}
impl Metric {
/// Creates a new metric using the MRI naming format.
///
/// MRI is the metric resource identifier in the format `<type>:<ns>/<name>@<unit>`. This name
/// ensures that just the name determines correct bucketing of metrics with name collisions.
pub fn new_mri(
namespace: impl fmt::Display,
name: impl fmt::Display,
unit: MetricUnit,
value: MetricValue,
timestamp: UnixTimestamp,
tags: BTreeMap<String, String>,
) -> Self {
Self {
name: format!("{}:{}/{}@{}", value.ty(), namespace, name, unit),
unit,
value,
timestamp,
tags,
}
}
fn parse_str(string: &str, timestamp: UnixTimestamp) -> Option<Self> {
let mut components = string.split('|');
let name_value_str = components.next()?;
let ty = components.next().and_then(|s| s.parse().ok())?;
let (name, unit, value) = parse_name_unit_value(name_value_str, ty)?;
let mut metric = Self {
name,
unit,
value,
timestamp,
tags: BTreeMap::new(),
};
for component in components {
if let Some('#') = component.chars().next() {
metric.tags = parse_tags(component.get(1..)?)?;
}
}
Some(metric)
}
/// Parses a single metric value from the raw protocol.
///
/// See the [`Metric`] for more information on the protocol.
///
/// # Example
///
/// ```
/// use relay_metrics::{Metric, UnixTimestamp};
///
/// let metric = Metric::parse(b"response_time@millisecond:57|d", UnixTimestamp::now())
/// .expect("metric should parse");
/// ```
pub fn parse(slice: &[u8], timestamp: UnixTimestamp) -> Result<Self, ParseMetricError> {
let string = std::str::from_utf8(slice).or(Err(ParseMetricError(())))?;
Self::parse_str(string, timestamp).ok_or(ParseMetricError(()))
}
/// Parses a set of metric values from the raw protocol.
///
/// Returns a metric result for each line in `slice`, ignoring empty lines. Both UNIX newlines
/// (`\n`) and Windows newlines (`\r\n`) are supported.
///
/// It is possible to continue consuming the iterator after `Err` is yielded.
///
/// See the [`Metric`] for more information on the protocol.
///
/// # Example
///
/// ```
/// use relay_metrics::{Metric, UnixTimestamp};
///
/// let data = br#"
/// endpoint.response_time@millisecond:57|d
/// endpoint.hits:1|c
/// "#;
///
/// for metric_result in Metric::parse_all(data, UnixTimestamp::now()) {
/// let metric = metric_result.expect("metric should parse");
/// println!("Metric {}: {}", metric.name, metric.value);
/// }
/// ```
pub fn parse_all(slice: &[u8], timestamp: UnixTimestamp) -> ParseMetrics<'_> {
ParseMetrics { slice, timestamp }
}
}
/// Iterator over parsed metrics returned from [`Metric::parse_all`].
#[derive(Clone, Debug)]
pub struct ParseMetrics<'a> {
slice: &'a [u8],
timestamp: UnixTimestamp,
}
impl Default for ParseMetrics<'_> {
fn default() -> Self {
Self {
slice: &[],
// The timestamp will never be returned.
timestamp: UnixTimestamp::from_secs(4711),
}
}
}
impl Iterator for ParseMetrics<'_> {
type Item = Result<Metric, ParseMetricError>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.slice.is_empty() {
return None;
}
let mut split = self.slice.splitn(2, |&b| b == b'\n');
let current = split.next()?;
self.slice = split.next().unwrap_or_default();
let string = match std::str::from_utf8(current) {
Ok(string) => string.strip_suffix('\r').unwrap_or(string),
Err(_) => return Some(Err(ParseMetricError(()))),
};
if !string.is_empty() {
return Some(Metric::parse_str(string, self.timestamp).ok_or(ParseMetricError(())));
}
}
}
}
impl FusedIterator for ParseMetrics<'_> {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sizeof_unit() {
assert_eq!(std::mem::size_of::<MetricUnit>(), 16);
assert_eq!(std::mem::align_of::<MetricUnit>(), 1);
}
#[test]
fn test_parse_garbage() {
let s = "x23-408j17z4232@#34d\nc3456y7^😎";
let timestamp = UnixTimestamp::from_secs(4711);
let result = Metric::parse(s.as_bytes(), timestamp);
assert!(result.is_err());
}
#[test]
fn test_parse_counter() {
let s = "foo:42|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: None,
value: Counter(
42.0,
),
timestamp: UnixTimestamp(4711),
tags: {},
}
"###);
}
#[test]
fn test_parse_distribution() {
let s = "foo:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: None,
value: Distribution(
17.5,
),
timestamp: UnixTimestamp(4711),
tags: {},
}
"###);
}
#[test]
fn test_parse_histogram() {
let s = "foo:17.5|h"; // common alias for distribution
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.value, MetricValue::Distribution(17.5));
}
#[test]
fn test_parse_set() {
let s = "foo:e2546e4c-ecd0-43ad-ae27-87960e57a658|s";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: None,
value: Set(
4267882815,
),
timestamp: UnixTimestamp(4711),
tags: {},
}
"###);
}
#[test]
fn test_parse_gauge() {
let s = "foo:42|g";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: None,
value: Gauge(
42.0,
),
timestamp: UnixTimestamp(4711),
tags: {},
}
"###);
}
#[test]
fn test_parse_unit() {
let s = "foo@second:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.unit, MetricUnit::Duration(DurationUnit::Second));
}
#[test]
fn test_parse_unit_regression() {
let s = "foo@s:17.5|d";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
assert_eq!(metric.unit, MetricUnit::Duration(DurationUnit::Second));
}
#[test]
fn test_parse_tags() {
let s = "foo:17.5|d|#foo,bar:baz";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp).unwrap();
insta::assert_debug_snapshot!(metric.tags, @r###"
{
"bar": "baz",
"foo": "",
}
"###);
}
#[test]
fn test_parse_invalid_name() {
let s = "foo#bar:42|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp);
assert!(metric.is_err());
}
#[test]
fn test_parse_empty_name() {
let s = ":42|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp);
assert!(metric.is_err());
}
#[test]
fn test_parse_invalid_name_with_leading_digit() {
let s = "64bit:42|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric = Metric::parse(s.as_bytes(), timestamp);
assert!(metric.is_err());
}
#[test]
fn test_serde_json() {
let json = r#"{
"name": "foo",
"unit": "second",
"type": "c",
"value": 42.0,
"timestamp": 4711,
"tags": {
"empty": "",
"full": "value"
}
}"#;
let metric = serde_json::from_str::<Metric>(json).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: Duration(
Second,
),
value: Counter(
42.0,
),
timestamp: UnixTimestamp(4711),
tags: {
"empty": "",
"full": "value",
},
}
"###);
let string = serde_json::to_string_pretty(&metric).unwrap();
assert_eq!(string, json);
}
#[test]
fn test_serde_json_defaults() {
// NB: timestamp is required in JSON as opposed to the text representation
let json = r#"{
"name": "foo",
"value": 42,
"type": "c",
"timestamp": 4711
}"#;
let metric = serde_json::from_str::<Metric>(json).unwrap();
insta::assert_debug_snapshot!(metric, @r###"
Metric {
name: "foo",
unit: None,
value: Counter(
42.0,
),
timestamp: UnixTimestamp(4711),
tags: {},
}
"###);
}
#[test]
fn test_parse_all() {
let s = "foo:42|c\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metrics: Vec<Metric> = Metric::parse_all(s.as_bytes(), timestamp)
.collect::<Result<_, _>>()
.unwrap();
assert_eq!(metrics.len(), 2);
}
#[test]
fn test_parse_all_crlf() {
let s = "foo:42|c\r\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metrics: Vec<Metric> = Metric::parse_all(s.as_bytes(), timestamp)
.collect::<Result<_, _>>()
.unwrap();
assert_eq!(metrics.len(), 2);
}
#[test]
fn test_parse_all_empty_lines() {
let s = "foo:42|c\n\n\nbar:17|c";
let timestamp = UnixTimestamp::from_secs(4711);
let metric_count = Metric::parse_all(s.as_bytes(), timestamp).count();
assert_eq!(metric_count, 2);
}
#[test]
fn test_parse_all_trailing() {
let s = "foo:42|c\nbar:17|c\n";
let timestamp = UnixTimestamp::from_secs(4711);
let metric_count = Metric::parse_all(s.as_bytes(), timestamp).count();
assert_eq!(metric_count, 2);
}
}