diff --git a/libdd-data-pipeline-ffi/src/lib.rs b/libdd-data-pipeline-ffi/src/lib.rs index d85002ab40..9e4e4bc278 100644 --- a/libdd-data-pipeline-ffi/src/lib.rs +++ b/libdd-data-pipeline-ffi/src/lib.rs @@ -9,3 +9,38 @@ mod error; mod response; mod trace_exporter; + +#[cfg(all(feature = "catch_panic", panic = "unwind"))] +macro_rules! catch_panic { + ($f:expr, $err:expr) => { + match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| $f)) { + Ok(ret) => ret, + Err(info) => { + if let Some(s) = info.downcast_ref::() { + error!(error = %ErrorCode::Panic, s); + } else if let Some(s) = info.downcast_ref::<&str>() { + error!(error = %ErrorCode::Panic, s); + } else { + error!(error = %ErrorCode::Panic, "Unable to retrieve panic context"); + } + $err + } + } + }; +} + +#[cfg(any(not(feature = "catch_panic"), panic = "abort"))] +macro_rules! catch_panic { + ($f:expr, $err:expr) => { + $f + }; +} + +macro_rules! gen_error { + ($l:expr) => { + Some(Box::new(ExporterError::new($l, &$l.to_string()))) + }; +} + +pub(crate) use catch_panic; +pub(crate) use gen_error; diff --git a/libdd-data-pipeline-ffi/src/trace_exporter.rs b/libdd-data-pipeline-ffi/src/trace_exporter.rs index 20a3f380f0..27226a00dd 100644 --- a/libdd-data-pipeline-ffi/src/trace_exporter.rs +++ b/libdd-data-pipeline-ffi/src/trace_exporter.rs @@ -3,51 +3,18 @@ use crate::error::{ExporterError, ExporterErrorCode as ErrorCode}; use crate::response::ExporterResponse; +use crate::{catch_panic, gen_error}; use libdd_common_ffi::{ CharSlice, {slice::AsBytes, slice::ByteSlice}, }; + use libdd_data_pipeline::trace_exporter::{ TelemetryConfig, TraceExporter, TraceExporterInputFormat, TraceExporterOutputFormat, }; use std::{ptr::NonNull, time::Duration}; use tracing::{debug, error}; -#[cfg(all(feature = "catch_panic", panic = "unwind"))] -use std::panic::{catch_unwind, AssertUnwindSafe}; - -macro_rules! gen_error { - ($l:expr) => { - Some(Box::new(ExporterError::new($l, &$l.to_string()))) - }; -} - -#[cfg(all(feature = "catch_panic", panic = "unwind"))] -macro_rules! catch_panic { - ($f:expr, $err:expr) => { - match catch_unwind(AssertUnwindSafe(|| $f)) { - Ok(ret) => ret, - Err(info) => { - if let Some(s) = info.downcast_ref::() { - error!(error = %ErrorCode::Panic, s); - } else if let Some(s) = info.downcast_ref::<&str>() { - error!(error = %ErrorCode::Panic, s); - } else { - error!(error = %ErrorCode::Panic, "Unable to retrieve panic context"); - } - $err - } - } - }; -} - -#[cfg(any(not(feature = "catch_panic"), panic = "abort"))] -macro_rules! catch_panic { - ($f:expr, $err:expr) => { - $f - }; -} - #[inline] fn sanitize_string(str: CharSlice) -> Result> { match str.try_to_utf8() {