From ec28561ae511c6390697c48d8c71a6351c60646a Mon Sep 17 00:00:00 2001 From: David Bernard Date: Sun, 11 Feb 2024 17:10:05 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20fix=20scope=20of=20rust's=20feat?= =?UTF-8?q?ure=20flag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: David Bernard --- cdviz-collector/Cargo.toml | 5 +++-- cdviz-collector/src/errors.rs | 2 ++ cdviz-collector/src/sinks/mod.rs | 4 ++++ cdviz-collector/src/sources/http.rs | 10 ++++++---- cdviz-collector/src/sources/mod.rs | 18 +++++++++++++++++- cdviz-collector/src/sources/noop.rs | 29 +++++++++++++++++++++++++++++ 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 cdviz-collector/src/sources/noop.rs diff --git a/cdviz-collector/Cargo.toml b/cdviz-collector/Cargo.toml index 6bbc924..4020b1c 100644 --- a/cdviz-collector/Cargo.toml +++ b/cdviz-collector/Cargo.toml @@ -29,7 +29,7 @@ opendal = { version = "0.45", default-features = false, features = [ "rustls", "services-fs", "services-s3", -] } +], optional = true } reqwest = "0.11" reqwest-middleware = "0.2" reqwest-tracing = "0.4" @@ -58,6 +58,7 @@ rustainers = "0.11" tracing-subscriber = "0.3" [features] -default = ["source_http", "sink_db"] +default = ["source_http", "source_opendal", "sink_db"] sink_db = ["dep:sqlx"] source_http = ["dep:axum", "dep:axum-tracing-opentelemetry"] +source_opendal = ["dep:opendal"] diff --git a/cdviz-collector/src/errors.rs b/cdviz-collector/src/errors.rs index 8726391..47c604c 100644 --- a/cdviz-collector/src/errors.rs +++ b/cdviz-collector/src/errors.rs @@ -14,6 +14,7 @@ pub(crate) enum Error { NoSink, // #[error(transparent)] // WatchDirectory(#[from] notify::Error), + #[cfg(feature = "sink_db")] #[error(transparent)] Db(#[from] sqlx::Error), #[error(transparent)] @@ -22,6 +23,7 @@ pub(crate) enum Error { Http(#[from] reqwest_middleware::Error), #[error(transparent)] Json(#[from] serde_json::Error), + #[cfg(feature = "source_opendal")] #[error(transparent)] Opendal(#[from] opendal::Error), #[error(transparent)] diff --git a/cdviz-collector/src/sinks/mod.rs b/cdviz-collector/src/sinks/mod.rs index b7cb113..9e5d337 100644 --- a/cdviz-collector/src/sinks/mod.rs +++ b/cdviz-collector/src/sinks/mod.rs @@ -9,6 +9,7 @@ use enum_dispatch::enum_dispatch; use serde::{Deserialize, Serialize}; use tokio::task::JoinHandle; +#[cfg(feature = "sink_db")] use db::DbSink; use debug::DebugSink; use http::HttpSink; @@ -16,6 +17,7 @@ use http::HttpSink; #[derive(Debug, Deserialize, Serialize)] #[serde(tag = "type")] pub(crate) enum Config { + #[cfg(feature = "sink_db")] #[serde(alias = "postgresql")] Db(db::Config), #[serde(alias = "debug")] @@ -29,6 +31,7 @@ impl TryFrom for SinkEnum { fn try_from(value: Config) -> Result { let out = match value { + #[cfg(feature = "sink_db")] Config::Db(config) => DbSink::try_from(config)?.into(), Config::Debug(config) => DebugSink::try_from(config)?.into(), Config::Http(config) => HttpSink::try_from(config)?.into(), @@ -40,6 +43,7 @@ impl TryFrom for SinkEnum { #[enum_dispatch] #[allow(clippy::enum_variant_names)] enum SinkEnum { + #[cfg(feature = "sink_db")] DbSink, DebugSink, HttpSink, diff --git a/cdviz-collector/src/sources/http.rs b/cdviz-collector/src/sources/http.rs index 1464904..641e252 100644 --- a/cdviz-collector/src/sources/http.rs +++ b/cdviz-collector/src/sources/http.rs @@ -97,11 +97,13 @@ async fn cdevents_collect( } impl IntoResponse for Error { + //TODO report the trace_id into the message to help to debug fn into_response(self) -> axum::response::Response { - let (status, error_message) = match self { - Error::Db(e) => (http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), - _ => (http::StatusCode::INTERNAL_SERVER_ERROR, "".to_string()), - }; + // let (status, error_message) = match self { + // Error::Db(e) => (http::StatusCode::INTERNAL_SERVER_ERROR, e.to_string()), + // _ => (http::StatusCode::INTERNAL_SERVER_ERROR, "".to_string()), + // }; + let (status, error_message) = (http::StatusCode::INTERNAL_SERVER_ERROR, "".to_string()); tracing::warn!(?error_message); let body = Json(json!({ "error": error_message, diff --git a/cdviz-collector/src/sources/mod.rs b/cdviz-collector/src/sources/mod.rs index 751bc67..4b8fde1 100644 --- a/cdviz-collector/src/sources/mod.rs +++ b/cdviz-collector/src/sources/mod.rs @@ -1,10 +1,16 @@ +#[cfg(feature = "source_http")] pub(crate) mod http; +pub(crate) mod noop; +#[cfg(feature = "source_opendal")] pub(crate) mod opendal; use crate::errors::Result; use crate::{Message, Sender}; use enum_dispatch::enum_dispatch; +#[cfg(feature = "source_http")] use http::HttpSource; +use noop::NoopSource; +#[cfg(feature = "source_opendal")] use opendal::OpendalSource; use serde::{Deserialize, Serialize}; use tokio::task::JoinHandle; @@ -12,8 +18,12 @@ use tokio::task::JoinHandle; #[derive(Debug, Deserialize, Serialize)] #[serde(tag = "type")] pub(crate) enum Config { + #[cfg(feature = "source_http")] #[serde(alias = "http")] Http(http::Config), + #[serde(alias = "noop")] + Noop(noop::Config), + #[cfg(feature = "source_opendal")] #[serde(alias = "opendal")] Opendal(opendal::Config), } @@ -23,8 +33,11 @@ impl TryFrom for SourceEnum { fn try_from(value: Config) -> Result { let out = match value { - Config::Opendal(config) => OpendalSource::try_from(config)?.into(), + #[cfg(feature = "source_http")] Config::Http(config) => HttpSource::try_from(config)?.into(), + Config::Noop(config) => NoopSource::try_from(config)?.into(), + #[cfg(feature = "source_opendal")] + Config::Opendal(config) => OpendalSource::try_from(config)?.into(), }; Ok(out) } @@ -33,7 +46,10 @@ impl TryFrom for SourceEnum { #[enum_dispatch] #[allow(clippy::enum_variant_names)] enum SourceEnum { + #[cfg(feature = "source_http")] HttpSource, + NoopSource, + #[cfg(feature = "source_opendal")] OpendalSource, } diff --git a/cdviz-collector/src/sources/noop.rs b/cdviz-collector/src/sources/noop.rs new file mode 100644 index 0000000..f24e8be --- /dev/null +++ b/cdviz-collector/src/sources/noop.rs @@ -0,0 +1,29 @@ +use crate::errors::Result; +use crate::{Message, Sender}; +use serde::Deserialize; +use serde::Serialize; +use std::time::Duration; +use tokio::time::sleep; + +use super::Source; + +#[derive(Debug, Deserialize, Serialize)] +pub(crate) struct Config {} + +impl TryFrom for NoopSource { + type Error = crate::errors::Error; + + fn try_from(_value: Config) -> Result { + Ok(Self {}) + } +} + +pub(crate) struct NoopSource {} + +impl Source for NoopSource { + async fn run(&self, _tx: Sender) -> Result<()> { + loop { + sleep(Duration::MAX).await; + } + } +}