-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
180 lines (166 loc) · 3.97 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
//! Serde bridge.
//!
use parking_lot::MappedRwLockReadGuard;
use prometheus_client::{
encoding::text::{Encode, EncodeMetric, Encoder},
metrics::{
family::{Family as InnerFamily, MetricConstructor},
MetricType, TypedMetric,
},
};
use serde::ser::Serialize;
use std::{fmt, hash::Hash, io};
mod error;
mod str;
mod top;
mod value;
/// 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,
/// # registry::Registry,
/// # };
/// # use prometools::{nonstandard::NonstandardUnsuffixedCounter, serde::Family};
/// # use serde::Serialize;
/// #
/// #[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, NonstandardUnsuffixedCounter>>::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: "techworkerscoalition.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{method=\"GET\",host=\"techworkerscoalition.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> {
self.0
.serialize(top::serializer(str::Writer::new(writer)))?;
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)
}
}