Skip to content

Commit 55273f0

Browse files
authored
Fix clippy lints (#510)
1 parent c0d2b75 commit 55273f0

File tree

20 files changed

+132
-132
lines changed

20 files changed

+132
-132
lines changed

clippy.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
too-many-lines-threshold = 150
2+
ignore-interior-mutability = ["metrics::key::Key"]

metrics-exporter-prometheus/src/distribution.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ pub enum Distribution {
3333

3434
impl Distribution {
3535
/// Creates a histogram distribution.
36-
#[warn(clippy::missing_panics_doc)]
36+
///
37+
/// # Panics
38+
///
39+
/// Panics if `buckets` is empty.
3740
pub fn new_histogram(buckets: &[f64]) -> Distribution {
3841
let hist = Histogram::new(buckets).expect("buckets should never be empty");
3942
Distribution::Histogram(hist)
@@ -299,8 +302,11 @@ mod tests {
299302
let snapshot = summary.snapshot(clock.now());
300303

301304
assert_eq!(0, snapshot.count());
302-
assert_eq!(f64::INFINITY, snapshot.min());
303-
assert_eq!(f64::NEG_INFINITY, snapshot.max());
305+
#[allow(clippy::float_cmp)]
306+
{
307+
assert_eq!(f64::INFINITY, snapshot.min());
308+
assert_eq!(f64::NEG_INFINITY, snapshot.max());
309+
}
304310
assert_eq!(None, snapshot.quantile(0.5));
305311
}
306312

@@ -318,8 +324,11 @@ mod tests {
318324

319325
let snapshot = summary.snapshot(clock.now());
320326

321-
assert_eq!(42.0, snapshot.min());
322-
assert_eq!(42.0, snapshot.max());
327+
#[allow(clippy::float_cmp)]
328+
{
329+
assert_eq!(42.0, snapshot.min());
330+
assert_eq!(42.0, snapshot.max());
331+
}
323332
// 42 +/- (42 * 0.0001)
324333
assert!(Some(41.9958) < snapshot.quantile(0.5));
325334
assert!(Some(42.0042) > snapshot.quantile(0.5));

metrics-exporter-prometheus/src/exporter/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,7 @@ mod tests {
560560
gauge1.set(-3.14);
561561
let rendered = handle.render();
562562
let expected_gauge = format!(
563-
"{}# TYPE basic_gauge gauge\nbasic_gauge{{wutang=\"forever\"}} -3.14\n\n",
564-
expected_counter
563+
"{expected_counter}# TYPE basic_gauge gauge\nbasic_gauge{{wutang=\"forever\"}} -3.14\n\n",
565564
);
566565

567566
assert_eq!(rendered, expected_gauge);
@@ -579,7 +578,7 @@ mod tests {
579578
"basic_histogram_count 1\n",
580579
"\n"
581580
);
582-
let expected_histogram = format!("{}{}", expected_gauge, histogram_data);
581+
let expected_histogram = format!("{expected_gauge}{histogram_data}");
583582

584583
assert_eq!(rendered, expected_histogram);
585584
}

metrics-exporter-prometheus/src/exporter/http_listener.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ impl HttpListeningExporter {
5959
continue;
6060
}
6161
};
62-
self.process_tcp_stream(stream).await;
62+
self.process_tcp_stream(stream);
6363
}
6464
}
6565

66-
async fn process_tcp_stream(&self, stream: TcpStream) {
66+
fn process_tcp_stream(&self, stream: TcpStream) {
6767
let is_allowed = self.check_tcp_allowed(&stream);
6868
let handle = self.handle.clone();
6969
let service = service_fn(move |req: Request<body::Incoming>| {
@@ -107,12 +107,12 @@ impl HttpListeningExporter {
107107
continue;
108108
}
109109
};
110-
self.process_uds_stream(stream).await;
110+
self.process_uds_stream(stream);
111111
}
112112
}
113113

114114
#[cfg(feature = "uds-listener")]
115-
async fn process_uds_stream(&self, stream: UnixStream) {
115+
fn process_uds_stream(&self, stream: UnixStream) {
116116
let handle = self.handle.clone();
117117
let service = service_fn(move |req: Request<body::Incoming>| {
118118
let handle = handle.clone();

metrics-exporter-prometheus/src/exporter/push_gateway.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn basic_auth(username: &str, password: Option<&str>) -> HeaderValue {
9494
header
9595
}
9696

97-
#[cfg(all(test))]
97+
#[cfg(test)]
9898
mod tests {
9999
use super::basic_auth;
100100

metrics-exporter-prometheus/src/formatting.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,37 @@ pub fn write_metric_line<T, T2>(
110110
/// [data model]: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
111111
pub fn sanitize_metric_name(name: &str) -> String {
112112
// The first character must be [a-zA-Z_:], and all subsequent characters must be [a-zA-Z0-9_:].
113-
let mut out = String::with_capacity(name.len());
114-
let mut is_invalid: fn(char) -> bool = invalid_metric_name_start_character;
115-
for c in name.chars() {
116-
if is_invalid(c) {
117-
out.push('_');
118-
} else {
119-
out.push(c);
120-
}
121-
is_invalid = invalid_metric_name_character;
122-
}
123-
out
113+
name.chars()
114+
.enumerate()
115+
.map(|(i, c)| {
116+
if i == 0 && valid_metric_name_start_character(c)
117+
|| i != 0 && valid_metric_name_character(c)
118+
{
119+
c
120+
} else {
121+
'_'
122+
}
123+
})
124+
.collect()
124125
}
125126

126127
/// Sanitizes a label key to be valid under the Prometheus [data model].
127128
///
128129
/// [data model]: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
129130
pub fn sanitize_label_key(key: &str) -> String {
130131
// The first character must be [a-zA-Z_], and all subsequent characters must be [a-zA-Z0-9_].
131-
let mut out = String::with_capacity(key.len());
132-
let mut is_invalid: fn(char) -> bool = invalid_label_key_start_character;
133-
for c in key.chars() {
134-
if is_invalid(c) {
135-
out.push('_');
136-
} else {
137-
out.push(c);
138-
}
139-
is_invalid = invalid_label_key_character;
140-
}
141-
out
132+
key.chars()
133+
.enumerate()
134+
.map(|(i, c)| {
135+
if i == 0 && valid_label_key_start_character(c)
136+
|| i != 0 && valid_label_key_character(c)
137+
{
138+
c
139+
} else {
140+
'_'
141+
}
142+
})
143+
.collect()
142144
}
143145

144146
/// Sanitizes a label value to be valid under the Prometheus [data model].
@@ -209,35 +211,35 @@ fn sanitize_label_value_or_description(value: &str, is_desc: bool) -> String {
209211
}
210212

211213
#[inline]
212-
fn invalid_metric_name_start_character(c: char) -> bool {
214+
fn valid_metric_name_start_character(c: char) -> bool {
213215
// Essentially, needs to match the regex pattern of [a-zA-Z_:].
214-
!(c.is_ascii_alphabetic() || c == '_' || c == ':')
216+
c.is_ascii_alphabetic() || c == '_' || c == ':'
215217
}
216218

217219
#[inline]
218-
fn invalid_metric_name_character(c: char) -> bool {
220+
fn valid_metric_name_character(c: char) -> bool {
219221
// Essentially, needs to match the regex pattern of [a-zA-Z0-9_:].
220-
!(c.is_ascii_alphanumeric() || c == '_' || c == ':')
222+
c.is_ascii_alphanumeric() || c == '_' || c == ':'
221223
}
222224

223225
#[inline]
224-
fn invalid_label_key_start_character(c: char) -> bool {
226+
fn valid_label_key_start_character(c: char) -> bool {
225227
// Essentially, needs to match the regex pattern of [a-zA-Z_].
226-
!(c.is_ascii_alphabetic() || c == '_')
228+
c.is_ascii_alphabetic() || c == '_'
227229
}
228230

229231
#[inline]
230-
fn invalid_label_key_character(c: char) -> bool {
232+
fn valid_label_key_character(c: char) -> bool {
231233
// Essentially, needs to match the regex pattern of [a-zA-Z0-9_].
232-
!(c.is_ascii_alphanumeric() || c == '_')
234+
c.is_ascii_alphanumeric() || c == '_'
233235
}
234236

235237
#[cfg(test)]
236238
mod tests {
237239
use crate::formatting::{
238-
invalid_label_key_character, invalid_label_key_start_character,
239-
invalid_metric_name_character, invalid_metric_name_start_character, sanitize_description,
240-
sanitize_label_key, sanitize_label_value, sanitize_metric_name,
240+
sanitize_description, sanitize_label_key, sanitize_label_value, sanitize_metric_name,
241+
valid_label_key_character, valid_label_key_start_character, valid_metric_name_character,
242+
valid_metric_name_start_character,
241243
};
242244
use proptest::prelude::*;
243245

@@ -321,11 +323,11 @@ mod tests {
321323
let as_chars = result.chars().collect::<Vec<_>>();
322324

323325
if let Some(c) = as_chars.first() {
324-
assert_eq!(false, invalid_metric_name_start_character(*c),
326+
assert!(valid_metric_name_start_character(*c),
325327
"first character of metric name was not valid");
326328
}
327329

328-
assert!(!as_chars.iter().any(|c| invalid_metric_name_character(*c)),
330+
assert!(as_chars.iter().all(|c| valid_metric_name_character(*c)),
329331
"invalid character in metric name");
330332
}
331333

@@ -335,7 +337,7 @@ mod tests {
335337
let as_chars = result.chars().collect::<Vec<_>>();
336338

337339
if let Some(c) = as_chars.first() {
338-
assert_eq!(false, invalid_label_key_start_character(*c),
340+
assert!(valid_label_key_start_character(*c),
339341
"first character of label key was not valid");
340342
}
341343

@@ -353,7 +355,7 @@ mod tests {
353355
}
354356
}*/
355357

356-
assert!(!as_chars.iter().any(|c| invalid_label_key_character(*c)),
358+
assert!(as_chars.iter().all(|c| valid_label_key_character(*c)),
357359
"invalid character in label key");
358360
}
359361

@@ -369,7 +371,7 @@ mod tests {
369371
let as_chars = delayered_backslashes.chars().collect::<Vec<_>>();
370372

371373
// If the first character is a double quote, then we messed up.
372-
assert!(as_chars.first().map(|c| *c != '"').unwrap_or(true),
374+
assert!(as_chars.first().map_or(true, |c| *c != '"'),
373375
"first character cannot be a double quote: {}", result);
374376

375377
// Now look for unescaped characters in the rest of the string, in a windowed fashion.

metrics-exporter-prometheus/tests/http_listener_integration_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ mod http_listener_test {
3636
let labels = vec![Label::new("wutang", "forever")];
3737
let key = Key::from_parts("basic_gauge", labels);
3838
let gauge = recorder.register_gauge(&key, &METADATA);
39-
gauge.set(-3.14);
39+
gauge.set(-1.23);
4040

4141
runtime.spawn(exporter); //async { exporter.await});
4242
tokio::time::sleep(Duration::from_millis(200)).await;
@@ -48,7 +48,7 @@ mod http_listener_test {
4848
let (status, body) = read_from(uri).await;
4949

5050
assert_eq!(status, StatusCode::OK);
51-
assert!(body.contains("basic_gauge{wutang=\"forever\"} -3.14"));
51+
assert!(body.contains("basic_gauge{wutang=\"forever\"} -1.23"));
5252
});
5353
}
5454

metrics-tracing-context/benches/layer.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ fn layer_benchmark(c: &mut Criterion) {
1212
let mut group = c.benchmark_group("layer");
1313
group.bench_function("base case", |b| {
1414
let recorder = NoopRecorder;
15-
static KEY_NAME: &'static str = "key";
15+
static KEY_NAME: &str = "key";
1616
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
17-
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
17+
static KEY_DATA: Key = Key::from_static_parts(KEY_NAME, &KEY_LABELS);
1818
static METADATA: metrics::Metadata =
1919
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));
2020

@@ -32,9 +32,9 @@ fn layer_benchmark(c: &mut Criterion) {
3232
let _guard = span.enter();
3333

3434
let recorder = NoopRecorder;
35-
static KEY_NAME: &'static str = "key";
35+
static KEY_NAME: &str = "key";
3636
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
37-
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
37+
static KEY_DATA: Key = Key::from_static_parts(KEY_NAME, &KEY_LABELS);
3838
static METADATA: metrics::Metadata =
3939
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));
4040

@@ -53,9 +53,9 @@ fn layer_benchmark(c: &mut Criterion) {
5353
let _guard = span.enter();
5454

5555
let recorder = NoopRecorder;
56-
static KEY_NAME: &'static str = "key";
56+
static KEY_NAME: &str = "key";
5757
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
58-
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
58+
static KEY_DATA: Key = Key::from_static_parts(KEY_NAME, &KEY_LABELS);
5959
static METADATA: metrics::Metadata =
6060
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));
6161

@@ -75,9 +75,9 @@ fn layer_benchmark(c: &mut Criterion) {
7575

7676
let tracing_layer = TracingContextLayer::all();
7777
let recorder = tracing_layer.layer(NoopRecorder);
78-
static KEY_NAME: &'static str = "key";
78+
static KEY_NAME: &str = "key";
7979
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
80-
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
80+
static KEY_DATA: Key = Key::from_static_parts(KEY_NAME, &KEY_LABELS);
8181
static METADATA: metrics::Metadata =
8282
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));
8383

@@ -97,9 +97,9 @@ fn layer_benchmark(c: &mut Criterion) {
9797

9898
let tracing_layer = TracingContextLayer::all();
9999
let recorder = tracing_layer.layer(NoopRecorder);
100-
static KEY_NAME: &'static str = "key";
100+
static KEY_NAME: &str = "key";
101101
static KEY_LABELS: [Label; 1] = [Label::from_static_parts("foo", "bar")];
102-
static KEY_DATA: Key = Key::from_static_parts(&KEY_NAME, &KEY_LABELS);
102+
static KEY_DATA: Key = Key::from_static_parts(KEY_NAME, &KEY_LABELS);
103103
static METADATA: metrics::Metadata =
104104
metrics::Metadata::new(module_path!(), metrics::Level::INFO, Some(module_path!()));
105105

metrics-tracing-context/benches/visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ struct DebugStruct {
140140

141141
impl DebugStruct {
142142
pub fn new() -> DebugStruct {
143-
DebugStruct { field1: format!("yeehaw!"), field2: 324242343243 }
143+
DebugStruct { field1: "yeehaw!".to_string(), field2: 324242343243 }
144144
}
145145
}
146146

metrics-tracing-context/src/tracing_integration.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl AsRef<Map> for Labels {
9797
/// fields and allows them to be later on used as metrics labels.
9898
#[derive(Default)]
9999
pub struct MetricsLayer {
100+
#[allow(clippy::type_complexity)]
100101
with_labels:
101102
Option<fn(&Dispatch, &Id, f: &mut dyn FnMut(&Labels) -> Option<Key>) -> Option<Key>>,
102103
}

0 commit comments

Comments
 (0)