|
| 1 | +use prometheus_client::encoding::text::encode; |
| 2 | +use prometheus_client::encoding::Encode; |
| 3 | +use prometheus_client::metrics::counter::Counter; |
| 4 | +use prometheus_client::metrics::family::Family; |
| 5 | +use prometheus_client::registry::Registry; |
| 6 | + |
| 7 | +#[derive(Clone, Hash, PartialEq, Eq, Encode)] |
| 8 | +struct Labels { |
| 9 | + method: Method, |
| 10 | + path: String, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Clone, Hash, PartialEq, Eq, Encode)] |
| 14 | +enum Method { |
| 15 | + Get, |
| 16 | + #[allow(dead_code)] |
| 17 | + Put, |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn basic_flow() { |
| 22 | + let mut registry = Registry::default(); |
| 23 | + |
| 24 | + let family = Family::<Labels, Counter>::default(); |
| 25 | + registry.register("my_counter", "This is my counter", family.clone()); |
| 26 | + |
| 27 | + // Record a single HTTP GET request. |
| 28 | + family |
| 29 | + .get_or_create(&Labels { |
| 30 | + method: Method::Get, |
| 31 | + path: "/metrics".to_string(), |
| 32 | + }) |
| 33 | + .inc(); |
| 34 | + |
| 35 | + // Encode all metrics in the registry in the text format. |
| 36 | + let mut buffer = vec![]; |
| 37 | + encode(&mut buffer, ®istry).unwrap(); |
| 38 | + |
| 39 | + let expected = "# HELP my_counter This is my counter.\n".to_owned() |
| 40 | + + "# TYPE my_counter counter\n" |
| 41 | + + "my_counter_total{method=\"Get\",path=\"/metrics\"} 1\n" |
| 42 | + + "# EOF\n"; |
| 43 | + assert_eq!(expected, String::from_utf8(buffer).unwrap()); |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(feature = "protobuf")] |
| 47 | +mod protobuf { |
| 48 | + use crate::{Labels, Method}; |
| 49 | + use prometheus_client::encoding::proto::encode; |
| 50 | + use prometheus_client::metrics::counter::Counter; |
| 51 | + use prometheus_client::metrics::family::Family; |
| 52 | + use prometheus_client::registry::Registry; |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn structs() { |
| 56 | + let mut registry = Registry::default(); |
| 57 | + let family = Family::<Labels, Counter>::default(); |
| 58 | + registry.register("my_counter", "This is my counter", family.clone()); |
| 59 | + |
| 60 | + // Record a single HTTP GET request. |
| 61 | + family |
| 62 | + .get_or_create(&Labels { |
| 63 | + method: Method::Get, |
| 64 | + path: "/metrics".to_string(), |
| 65 | + }) |
| 66 | + .inc(); |
| 67 | + |
| 68 | + // Encode all metrics in the registry in the OpenMetrics protobuf format. |
| 69 | + let mut metric_set = encode(®istry); |
| 70 | + let mut family: prometheus_client::encoding::proto::MetricFamily = |
| 71 | + metric_set.metric_families.pop().unwrap(); |
| 72 | + let metric: prometheus_client::encoding::proto::Metric = family.metrics.pop().unwrap(); |
| 73 | + |
| 74 | + let method = &metric.labels[0]; |
| 75 | + assert_eq!("method", method.name); |
| 76 | + assert_eq!("Get", method.value); |
| 77 | + |
| 78 | + let path = &metric.labels[1]; |
| 79 | + assert_eq!("path", path.name); |
| 80 | + assert_eq!("/metrics", path.value); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn enums() { |
| 85 | + let mut registry = Registry::default(); |
| 86 | + let family = Family::<Method, Counter>::default(); |
| 87 | + registry.register("my_counter", "This is my counter", family.clone()); |
| 88 | + |
| 89 | + // Record a single HTTP GET request. |
| 90 | + family.get_or_create(&Method::Get).inc(); |
| 91 | + |
| 92 | + // Encode all metrics in the registry in the OpenMetrics protobuf format. |
| 93 | + let mut metric_set = encode(®istry); |
| 94 | + let mut family: prometheus_client::encoding::proto::MetricFamily = |
| 95 | + metric_set.metric_families.pop().unwrap(); |
| 96 | + let metric: prometheus_client::encoding::proto::Metric = family.metrics.pop().unwrap(); |
| 97 | + |
| 98 | + let label = &metric.labels[0]; |
| 99 | + assert_eq!("Method", label.name); |
| 100 | + assert_eq!("Get", label.value); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn remap_keyword_identifiers() { |
| 106 | + #[derive(Encode, Hash, Clone, Eq, PartialEq)] |
| 107 | + struct Labels { |
| 108 | + // `r#type` is problematic as `r#` is not a valid OpenMetrics label name |
| 109 | + // but one needs to use keyword identifier syntax (aka. raw identifiers) |
| 110 | + // as `type` is a keyword. |
| 111 | + // |
| 112 | + // Test makes sure `r#type` is replaced by `type` in the OpenMetrics |
| 113 | + // output. |
| 114 | + r#type: u64, |
| 115 | + } |
| 116 | + |
| 117 | + let labels = Labels { r#type: 42 }; |
| 118 | + |
| 119 | + let mut buffer = vec![]; |
| 120 | + |
| 121 | + { |
| 122 | + use prometheus_client::encoding::text::Encode; |
| 123 | + labels.encode(&mut buffer).unwrap(); |
| 124 | + } |
| 125 | + |
| 126 | + assert_eq!( |
| 127 | + "type=\"42\"".to_string(), |
| 128 | + String::from_utf8(buffer).unwrap() |
| 129 | + ); |
| 130 | +} |
0 commit comments