Skip to content

Commit

Permalink
🚨 fix scope of rust's feature flag
Browse files Browse the repository at this point in the history
Signed-off-by: David Bernard <[email protected]>
  • Loading branch information
davidB committed Feb 11, 2024
1 parent 5fffc0e commit ec28561
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cdviz-collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
2 changes: 2 additions & 0 deletions cdviz-collector/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
4 changes: 4 additions & 0 deletions cdviz-collector/src/sinks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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;

#[derive(Debug, Deserialize, Serialize)]
#[serde(tag = "type")]
pub(crate) enum Config {
#[cfg(feature = "sink_db")]
#[serde(alias = "postgresql")]
Db(db::Config),
#[serde(alias = "debug")]
Expand All @@ -29,6 +31,7 @@ impl TryFrom<Config> for SinkEnum {

fn try_from(value: Config) -> Result<Self> {
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(),
Expand All @@ -40,6 +43,7 @@ impl TryFrom<Config> for SinkEnum {
#[enum_dispatch]
#[allow(clippy::enum_variant_names)]
enum SinkEnum {
#[cfg(feature = "sink_db")]
DbSink,
DebugSink,
HttpSink,
Expand Down
10 changes: 6 additions & 4 deletions cdviz-collector/src/sources/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 17 additions & 1 deletion cdviz-collector/src/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#[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;

#[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),
}
Expand All @@ -23,8 +33,11 @@ impl TryFrom<Config> for SourceEnum {

fn try_from(value: Config) -> Result<Self> {
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)
}
Expand All @@ -33,7 +46,10 @@ impl TryFrom<Config> for SourceEnum {
#[enum_dispatch]
#[allow(clippy::enum_variant_names)]
enum SourceEnum {
#[cfg(feature = "source_http")]
HttpSource,
NoopSource,
#[cfg(feature = "source_opendal")]
OpendalSource,
}

Expand Down
29 changes: 29 additions & 0 deletions cdviz-collector/src/sources/noop.rs
Original file line number Diff line number Diff line change
@@ -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<Config> for NoopSource {
type Error = crate::errors::Error;

fn try_from(_value: Config) -> Result<Self> {
Ok(Self {})
}
}

pub(crate) struct NoopSource {}

impl Source for NoopSource {
async fn run(&self, _tx: Sender<Message>) -> Result<()> {
loop {
sleep(Duration::MAX).await;
}
}
}

0 comments on commit ec28561

Please sign in to comment.