Skip to content

Commit 0358ca3

Browse files
authored
Improve control plane logging (#6003)
1 parent bfed5c6 commit 0358ca3

File tree

12 files changed

+490
-228
lines changed

12 files changed

+490
-228
lines changed

quickwit/quickwit-common/src/metrics.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
// limitations under the License.
1414

1515
use std::collections::{BTreeMap, HashMap};
16-
use std::sync::OnceLock;
16+
use std::sync::{LazyLock, OnceLock};
1717

18-
use once_cell::sync::Lazy;
1918
use prometheus::{Gauge, HistogramOpts, Opts, TextEncoder};
2019
pub use prometheus::{
2120
Histogram, HistogramTimer, HistogramVec as PrometheusHistogramVec, IntCounter,
@@ -438,16 +437,17 @@ impl InFlightDataGauges {
438437
}
439438
}
440439

441-
/// This function returns `index_name` or projects it to `<any>` if per-index metrics are disabled.
442-
pub fn index_label(index_name: &str) -> &str {
443-
static PER_INDEX_METRICS_ENABLED: OnceLock<bool> = OnceLock::new();
444-
let per_index_metrics_enabled: bool = *PER_INDEX_METRICS_ENABLED
445-
.get_or_init(|| !crate::get_bool_from_env("QW_DISABLE_PER_INDEX_METRICS", false));
446-
if per_index_metrics_enabled {
447-
index_name
440+
/// This function returns `index_id` as is if per-index metrics are enabled, or projects it to
441+
/// `"__any__"` otherwise.
442+
pub fn index_label(index_id: &str) -> &str {
443+
static PER_INDEX_METRICS_ENABLED: LazyLock<bool> =
444+
LazyLock::new(|| !crate::get_bool_from_env("QW_DISABLE_PER_INDEX_METRICS", false));
445+
446+
if *PER_INDEX_METRICS_ENABLED {
447+
index_id
448448
} else {
449449
"__any__"
450450
}
451451
}
452452

453-
pub static MEMORY_METRICS: Lazy<MemoryMetrics> = Lazy::new(MemoryMetrics::default);
453+
pub static MEMORY_METRICS: LazyLock<MemoryMetrics> = LazyLock::new(MemoryMetrics::default);

quickwit/quickwit-common/src/pretty.rs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ impl<I> PrettySample<I> {
2323
}
2424
}
2525

26-
impl<I, T> fmt::Debug for PrettySample<I>
26+
impl<I> fmt::Debug for PrettySample<I>
2727
where
28-
I: IntoIterator<Item = T> + Clone,
29-
T: fmt::Debug,
28+
I: IntoIterator + Clone,
29+
I::Item: fmt::Debug,
3030
{
3131
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
3232
write!(formatter, "[")?;
33-
// in general we will get passed a reference (&[...], &HashMap...) or a Map<_> of them.
33+
34+
// In general, we will receive a reference (&[...], &HashMap...) or a Map<_> of them.
3435
// So we either perform a Copy, or a cheap Clone of a simple struct
3536
let mut iter = self.0.clone().into_iter().enumerate();
3637
for (i, item) in &mut iter {
@@ -55,9 +56,9 @@ pub trait PrettyDisplay {
5556
fn pretty_display(&self) -> impl fmt::Display;
5657
}
5758

58-
struct PrettyDurationDisplay<'a>(&'a Duration);
59+
struct DurationPrettyDisplay<'a>(&'a Duration);
5960

60-
impl fmt::Display for PrettyDurationDisplay<'_> {
61+
impl fmt::Display for DurationPrettyDisplay<'_> {
6162
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
6263
// This is enough for my current use cases. To be extended as you see fit.
6364
let duration_millis = self.0.as_millis();
@@ -76,7 +77,37 @@ impl fmt::Display for PrettyDurationDisplay<'_> {
7677

7778
impl PrettyDisplay for Duration {
7879
fn pretty_display(&self) -> impl fmt::Display {
79-
PrettyDurationDisplay(self)
80+
DurationPrettyDisplay(self)
81+
}
82+
}
83+
84+
struct SequencePrettyDisplay<I>(I);
85+
86+
impl<I> fmt::Display for SequencePrettyDisplay<I>
87+
where
88+
I: IntoIterator + Clone,
89+
I::Item: fmt::Display,
90+
{
91+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92+
write!(f, "[")?;
93+
94+
// In general, we will receive a reference (&[...], &HashMap...) or a Map<_> of them.
95+
// So we either perform a Copy, or a cheap Clone of a simple struct
96+
let mut iter = self.0.clone().into_iter().peekable();
97+
98+
while let Some(item) = iter.next() {
99+
write!(f, "{item}")?;
100+
if iter.peek().is_some() {
101+
write!(f, ", ")?;
102+
}
103+
}
104+
write!(f, "]")
105+
}
106+
}
107+
108+
impl<T: fmt::Display> PrettyDisplay for &[T] {
109+
fn pretty_display(&self) -> impl fmt::Display {
110+
SequencePrettyDisplay(*self)
80111
}
81112
}
82113

@@ -103,17 +134,29 @@ mod tests {
103134
}
104135

105136
#[test]
106-
fn test_pretty_duration() {
107-
let pretty_duration = Duration::from_millis(0);
108-
assert_eq!(format!("{}", pretty_duration.pretty_display()), "0ms");
137+
fn test_duration_pretty_display() {
138+
let duration = Duration::from_millis(0);
139+
assert_eq!(format!("{}", duration.pretty_display()), "0ms");
109140

110-
let pretty_duration = Duration::from_millis(125);
111-
assert_eq!(format!("{}", pretty_duration.pretty_display()), "125ms");
141+
let duration = Duration::from_millis(125);
142+
assert_eq!(format!("{}", duration.pretty_display()), "125ms");
143+
144+
let duration = Duration::from_millis(1_000);
145+
assert_eq!(format!("{}", duration.pretty_display()), "1.0s");
146+
147+
let duration = Duration::from_millis(1_125);
148+
assert_eq!(format!("{}", duration.pretty_display()), "1.12s");
149+
}
150+
151+
#[test]
152+
fn test_sequence_pretty_display() {
153+
let empty_slice: &[i32] = &[];
154+
assert_eq!(format!("{}", empty_slice.pretty_display()), "[]");
112155

113-
let pretty_duration = Duration::from_millis(1_000);
114-
assert_eq!(format!("{}", pretty_duration.pretty_display()), "1.0s");
156+
let slice_one: &[i32] = &[1];
157+
assert_eq!(format!("{}", slice_one.pretty_display()), "[1]");
115158

116-
let pretty_duration = Duration::from_millis(1_125);
117-
assert_eq!(format!("{}", pretty_duration.pretty_display()), "1.12s");
159+
let slice_two: &[i32] = &[1, 2];
160+
assert_eq!(format!("{}", slice_two.pretty_display()), "[1, 2]");
118161
}
119162
}

0 commit comments

Comments
 (0)