Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions foundations-macros/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ fn metric_init(foundations: &Path, fn_: &ItemFn) -> proc_macro2::TokenStream {
#registry,
::std::stringify!(#field_name),
str::trim(#doc),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
#foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -519,7 +519,7 @@ mod tests {
registry,
::std::stringify!(connections_total),
str::trim(" Total number of connections"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
tarmac::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -576,7 +576,7 @@ mod tests {
opt_registry,
::std::stringify!(connections_total),
str::trim(" Total number of connections"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -638,7 +638,7 @@ mod tests {
registry,
::std::stringify!(requests_total),
str::trim(" Total number of requests"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand All @@ -650,7 +650,7 @@ mod tests {
opt_registry,
::std::stringify!(connections_total),
str::trim(" Total number of connections"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -742,7 +742,7 @@ mod tests {
registry,
::std::stringify!(connections_errors_total),
str::trim(" Total number of connection errors"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -840,7 +840,7 @@ mod tests {
registry,
::std::stringify!(connections_latency),
str::trim(" Latency of connections"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand All @@ -854,7 +854,7 @@ mod tests {
registry,
::std::stringify!(requests_per_connection),
str::trim(" Number of requests per connection"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down Expand Up @@ -943,7 +943,7 @@ mod tests {
registry,
::std::stringify!(requests_total),
str::trim(" Total number of requests"),
::std::boxed::Box::new(::std::clone::Clone::clone(&metric))
::foundations::telemetry::metrics::internal::wrap_metric(::std::clone::Clone::clone(&metric)),
);

metric
Expand Down
61 changes: 52 additions & 9 deletions foundations/src/telemetry/metrics/internal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::{ExtraProducer, InfoMetric, info_metric};
use super::rewind::{RewindState, RewindTo};
use super::{ExtraProducer, InfoMetric, info_metric, report_nonfatal_collect_error};
use crate::telemetry::settings::{MetricsSettings, ServiceNameFormat};
use crate::{Result, ServiceInfo};
use prometheus_client::encoding::text::{EncodeMetric, encode};
use prometheus_client::encoding::text::{EncodeMetric, Encoder, SendSyncEncodeMetric, encode};
use prometheus_client::metrics::MetricType;
use prometheus_client::registry::Registry;
use prometools::serde::InfoGauge;
use std::any::TypeId;
Expand Down Expand Up @@ -90,6 +92,7 @@ impl Registries {

for info_metric in info_registry.values() {
let info_gauge = InfoGauge::new(&**info_metric);
let info_gauge = RewindErrorEncode(info_gauge);

registry.register(info_metric.name(), info_metric.help(), info_gauge)
}
Expand Down Expand Up @@ -176,19 +179,59 @@ where
}
}

pub(super) fn encode_registry(
buffer: &mut Vec<u8>,
registry: &Registry<impl EncodeMetric>,
) -> Result<()> {
encode(buffer, registry)?;
std::thread_local! {
static ENCODER_REWIND_STATE: RewindState = const { RewindState::new() };
}

struct RewindErrorEncode<M>(M);

impl<M: EncodeMetric> EncodeMetric for RewindErrorEncode<M> {
fn encode(&self, encoder: Encoder) -> std::io::Result<()> {
let mut res = self.0.encode(encoder);
Comment thread
TheJokr marked this conversation as resolved.

// If encoding the metric failed, and we are inside a rewindable encoder,
// discard the error and rewind the encoder to the last newline to avoid
// garbage output.
if res.is_err() {
let _ = ENCODER_REWIND_STATE.try_with(|s| {
if s.is_active() {
s.rewind_to(RewindTo::LastNewline);
let err = std::mem::replace(&mut res, Ok(())).unwrap_err();
report_nonfatal_collect_error(&format_args!(
"encoding metric or family: {err}"
));
}
});
}

res
}

#[inline]
fn metric_type(&self) -> MetricType {
self.0.metric_type()
}
}

/// Wraps a metric in our private error-handling type, without making the type public.
pub fn wrap_metric(metric: impl SendSyncEncodeMetric + 'static) -> Box<dyn SendSyncEncodeMetric> {
Box::new(RewindErrorEncode(metric))
}

fn encode_registry(buffer: &mut Vec<u8>, registry: &Registry<impl EncodeMetric>) -> Result<()> {
ENCODER_REWIND_STATE.with(|s| {
let mut writer = s.activate(buffer);
encode(&mut writer, registry)
})?;

truncate_eof(buffer);

Ok(())
}

fn truncate_eof(buffer: &mut Vec<u8>) {
if buffer.ends_with(b"# EOF\n") {
buffer.truncate(buffer.len() - b"# EOF\n".len());
const EOF_MARKER: &[u8] = b"# EOF\n";
if buffer.ends_with(EOF_MARKER) {
buffer.truncate(buffer.len() - EOF_MARKER.len());
}
}
18 changes: 17 additions & 1 deletion foundations/src/telemetry/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use crate::Result;
use prometheus::{Encoder, TextEncoder};
use serde::Serialize;
use std::any::TypeId;
use std::fmt::Display;

mod gauge;
mod rewind;

pub(super) mod init;

Expand Down Expand Up @@ -44,7 +46,21 @@ pub fn collect(settings: &MetricsSettings) -> Result<String> {

buffer.extend_from_slice(b"# EOF\n");

Ok(String::from_utf8(buffer)?)
let metrics_str = String::from_utf8(buffer).unwrap_or_else(|err| {
report_nonfatal_collect_error(&format_args!("converting raw metrics to string: {err}"));
String::from_utf8_lossy(err.as_bytes()).into_owned()
});
Ok(metrics_str)
}

#[inline]
#[track_caller]
fn report_nonfatal_collect_error(err: &dyn Display) {
#[cfg(feature = "logging")]
crate::telemetry::log::warn!("non-fatal error while collecting metrics"; "error" => %err);

#[cfg(not(feature = "logging"))]
eprintln!("non-fatal error while collecting metrics: {err}");
}

/// A macro that allows to define Prometheus metrics.
Expand Down
161 changes: 161 additions & 0 deletions foundations/src/telemetry/metrics/rewind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
use std::cell::Cell;
use std::io::{self, Write};

#[derive(Default, Clone, Copy)]
pub(crate) enum RewindTo {
#[default]
None,
LastNewline,
}

pub(crate) struct RewindState {
active: Cell<bool>,
rewind_to: Cell<RewindTo>,
}

impl RewindState {
pub(crate) const fn new() -> Self {
Self {
active: Cell::new(false),
rewind_to: Cell::new(RewindTo::None),
}
}

#[track_caller]
pub(crate) fn activate<'a>(&'a self, buf: &'a mut Vec<u8>) -> RewindableWriter<'a> {
let was_active = self.active.replace(true);
assert!(!was_active, "this state already has an associated writer");

self.rewind_to.set(RewindTo::None);
RewindableWriter {
out: buf,
state: self,
}
}

#[inline]
pub(crate) fn is_active(&self) -> bool {
self.active.get()
}

#[inline]
pub(crate) fn rewind_to(&self, to: RewindTo) {
debug_assert!(self.is_active(), "rewind attempted without writer");
self.rewind_to.set(to);
}

fn reset(&self) {
self.active.set(false);
self.rewind_to.set(RewindTo::None);
}
}

pub(crate) struct RewindableWriter<'a> {
out: &'a mut Vec<u8>,
state: &'a RewindState,
}

impl<'a> RewindableWriter<'a> {
fn apply_rewind(&mut self) {
match self.state.rewind_to.take() {
RewindTo::None => {}
RewindTo::LastNewline => {
if let Some(newline_idx) = self.out.iter().rposition(|v| *v == b'\n') {
// Keep the newline itself in the buffer
self.out.truncate(newline_idx + 1);
}
}
}
}
}

impl Write for RewindableWriter<'_> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.apply_rewind();
Write::write(self.out, buf)
}

fn write_vectored(&mut self, bufs: &[io::IoSlice]) -> io::Result<usize> {
self.apply_rewind();
Write::write_vectored(self.out, bufs)
}

fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.apply_rewind();
Write::write_all(self.out, buf)
}

fn flush(&mut self) -> io::Result<()> {
self.apply_rewind();
Write::flush(self.out)
}
}

impl Drop for RewindableWriter<'_> {
fn drop(&mut self) {
self.apply_rewind();
self.state.reset();
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Write;

#[test]
#[should_panic(expected = "this state already has an associated writer")]
fn second_writer_panics() {
let state = RewindState::new();
let mut buf = Vec::new();
let mut other_buf = Vec::new();

let _writer = state.activate(&mut buf);
let _other_writer = state.activate(&mut other_buf);
// The second activate call should panic
}

#[test]
fn rewind_newline() {
let state = RewindState::new();
let mut buf = b"line 1\nline 2".to_vec();

{
// No rewind initially
let mut writer = state.activate(&mut buf);
writer.write_all(b" before rewind").unwrap();
assert_eq!(writer.out, b"line 1\nline 2 before rewind");

// Rewind is applied on next write
state.rewind_to(RewindTo::LastNewline);
writer.write_all(b"different line\nafter rewind").unwrap();
assert_eq!(writer.out, b"line 1\ndifferent line\nafter rewind");

// Rewind is applied on flush
state.rewind_to(RewindTo::LastNewline);
writer.flush().unwrap();
assert_eq!(writer.out, b"line 1\ndifferent line\n");

// Rewind is cleared after being applied above
writer.write_all(b"after clear").unwrap();
}

assert_eq!(buf, b"line 1\ndifferent line\nafter clear");
}

#[test]
fn rewind_newline_on_drop() {
let state = RewindState::new();
let mut buf = Vec::new();

{
let mut writer = state.activate(&mut buf);
writer.write_all(b"line 1\nline 2").unwrap();

// Rewind is also applied when writer drops
state.rewind_to(RewindTo::LastNewline);
}

assert_eq!(buf, b"line 1\n");
}
}
Loading
Loading