Skip to content

Commit

Permalink
Make the router panic on debug builds in addition to logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryn committed Sep 6, 2023
1 parent 723d5dd commit 883e960
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions apollo-router/src/plugins/telemetry/metrics/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ use super::METRIC_PREFIX_HISTOGRAM;
use super::METRIC_PREFIX_MONOTONIC_COUNTER;
use super::METRIC_PREFIX_VALUE;

macro_rules! log_and_panic_in_debug_build {
($($tokens:tt)+) => {{
tracing::debug!($($tokens)+);
#[cfg(debug_assertions)]
panic!("metric type error, see DEBUG log for details. Release builds will not panic but will still emit an debug log message");
}};
}

#[derive(Default)]
pub(crate) struct Instruments {
u64_counter: MetricsMap<Counter<u64>>,
Expand Down Expand Up @@ -164,7 +172,7 @@ impl<'a> MetricVisitor<'a> {
fn set_metric(&mut self, name: &'static str, instrument_type: InstrumentType) {
self.metric = Some((name, instrument_type));
if self.attributes_ignored {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name = name,
"metric attributes must be declared after the metric value. Some attributes have been ignored"
);
Expand All @@ -181,7 +189,7 @@ impl<'a> Visit for MetricVisitor<'a> {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.set_metric(metric_name, InstrumentType::HistogramF64(value));
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
Expand All @@ -197,7 +205,7 @@ impl<'a> Visit for MetricVisitor<'a> {

fn record_i64(&mut self, field: &Field, value: i64) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
Expand All @@ -206,7 +214,7 @@ impl<'a> Visit for MetricVisitor<'a> {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
self.set_metric(metric_name, InstrumentType::HistogramI64(value));
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
Expand All @@ -224,7 +232,7 @@ impl<'a> Visit for MetricVisitor<'a> {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
self.set_metric(metric_name, InstrumentType::CounterU64(value));
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
Expand All @@ -233,7 +241,7 @@ impl<'a> Visit for MetricVisitor<'a> {
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
self.set_metric(metric_name, InstrumentType::GaugeU64(value));
} else if self.metric.is_some() {
tracing::error!(
log_and_panic_in_debug_build!(
name = field.name(),
"metric attribute must be i64, f64, string or bool. This attribute will be ignored"
);
Expand All @@ -244,27 +252,27 @@ impl<'a> Visit for MetricVisitor<'a> {

fn record_i128(&mut self, field: &Field, _value: i128) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"histogram must be u64, i64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
} else if self.metric.is_some() {
tracing::error!(
log_and_panic_in_debug_build!(
name = field.name(),
"metric attribute must be i64, f64, string or bool. This attribute will be ignored"
);
Expand All @@ -275,27 +283,27 @@ impl<'a> Visit for MetricVisitor<'a> {

fn record_u128(&mut self, field: &Field, _value: u128) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"histogram must be u64, i64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
} else if self.metric.is_some() {
tracing::error!(
log_and_panic_in_debug_build!(
name = field.name(),
"metric attribute must be i64, f64, string or bool. This attribute will be ignored"
);
Expand All @@ -306,22 +314,22 @@ impl<'a> Visit for MetricVisitor<'a> {

fn record_bool(&mut self, field: &Field, value: bool) {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"histogram must be u64, i64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
Expand All @@ -338,22 +346,22 @@ impl<'a> Visit for MetricVisitor<'a> {
fn record_str(&mut self, field: &Field, value: &str) {
if field.name() != "message" {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"histogram must be u64, i64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
Expand All @@ -371,22 +379,22 @@ impl<'a> Visit for MetricVisitor<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
if field.name() != "message" {
if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_MONOTONIC_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"monotonic counter must be u64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"counter must be i64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"histogram must be u64, i64 or f64. This metric will be ignored"
);
} else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_VALUE) {
tracing::error!(
log_and_panic_in_debug_build!(
metric_name,
"gauge must be u64. This metric will be ignored"
);
Expand Down

0 comments on commit 883e960

Please sign in to comment.