/
bridge.rs
174 lines (161 loc) · 3.83 KB
/
bridge.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
use parking_lot::MappedRwLockReadGuard;
use prometheus_client::{
encoding::text::{Encode, EncodeMetric, Encoder},
metrics::{
family::{Family as InnerFamily, MetricConstructor},
MetricType, TypedMetric,
},
};
use serde::Serialize;
use std::hash::Hash;
use std::{fmt, io};
/// A wrapper around [`prometheus_client::metrics::family::Family`] which
/// encodes its labels with [`Serialize`] instead of [`Encode`].
///
/// #### Examples
///
/// Basic usage:
///
/// ```rust
/// # use prometheus_client::{
/// # encoding::text::encode,
/// # metrics::counter::Counter,
/// # registry::Registry,
/// # };
/// # use serde::Serialize;
/// # use serde_prometheus_labels::Family;
/// #
/// #[derive(Clone, Eq, Hash, PartialEq, Serialize)]
/// struct Labels {
/// method: Method,
/// host: String,
/// }
///
/// #[derive(Clone, Eq, Hash, PartialEq, Serialize)]
/// enum Method {
/// #[serde(rename = "GET")]
/// Get,
/// }
///
/// let family = <Family<Labels, Counter>>::default();
/// let mut registry = Registry::with_prefix("http");
///
/// registry.register(
/// "Incoming requests",
/// "Number of requests per method and per host",
/// family.clone(),
/// );
///
/// family
/// .get_or_create(&Labels {
/// method: Method::Get,
/// host: "unionize.org".to_string(),
/// })
/// .inc();
///
/// let mut serialized = String::new();
///
/// // SAFETY: We know prometheus-client only writes UTF-8 slices.
/// unsafe {
/// encode(&mut serialized.as_mut_vec(), ®istry).unwrap();
/// }
///
/// assert_eq!(
/// serialized,
/// concat!(
/// "# HELP http_Incoming requests Number of requests per method and per host.\n",
/// "# TYPE http_Incoming requests counter\n",
/// "http_Incoming requests_total{method=\"GET\",host=\"unionize.org\"} 1\n",
/// "# EOF\n",
/// ),
/// );
/// ```
#[derive(Debug)]
pub struct Family<S, M, C = fn() -> M> {
inner: InnerFamily<Bridge<S>, M, C>,
}
impl<S, M, C> Family<S, M, C>
where
S: Clone + Eq + Hash,
{
pub fn new_with_constructor(constructor: C) -> Self {
Self {
inner: InnerFamily::new_with_constructor(constructor),
}
}
}
impl<S, M> Default for Family<S, M>
where
S: Clone + Eq + Hash,
M: Default,
{
fn default() -> Self {
Self {
inner: Default::default(),
}
}
}
impl<S, M, C> Family<S, M, C>
where
S: Clone + Eq + Hash,
C: MetricConstructor<M>,
{
pub fn get_or_create(&self, label_set: &S) -> MappedRwLockReadGuard<M> {
self.inner.get_or_create(Bridge::from_ref(label_set))
}
}
impl<S, M, C> EncodeMetric for Family<S, M, C>
where
S: Clone + Eq + Hash + Serialize,
M: EncodeMetric + TypedMetric,
C: MetricConstructor<M>,
{
fn encode(&self, encoder: Encoder) -> io::Result<()> {
self.inner.encode(encoder)
}
fn metric_type(&self) -> MetricType {
M::TYPE
}
}
impl<S, M, C> TypedMetric for Family<S, M, C>
where
M: TypedMetric,
{
const TYPE: MetricType = <M as TypedMetric>::TYPE;
}
impl<S, M, C> Clone for Family<S, M, C>
where
C: Clone,
{
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
}
}
}
#[derive(Clone, Eq, Hash, PartialEq)]
#[repr(transparent)]
struct Bridge<S>(S);
impl<S> Bridge<S> {
fn from_ref(label_set: &S) -> &Self {
// SAFETY: `Self` is a transparent newtype wrapper.
unsafe { &*(label_set as *const S as *const Bridge<S>) }
}
}
impl<S> Encode for Bridge<S>
where
S: Serialize,
{
fn encode(&self, writer: &mut dyn io::Write) -> Result<(), std::io::Error> {
crate::to_writer(writer, &self.0)?;
Ok(())
}
}
impl<S> fmt::Debug for Bridge<S>
where
S: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}