diff --git a/Cargo.toml b/Cargo.toml index 6a0221b..6877eb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,7 @@ exclude = [ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["epoll", "asyncexec"] +default = ["epoll", "asyncexec", "sensor_datadog"] docs = ["store_rocksdb", "sink_elastic", "sink_postgres"] # IO systems iouring = ["nuclei/iouring"] @@ -59,6 +59,13 @@ store_rocksdb = ["rocksdb"] # Sinks sink_elastic = ["tokio", "elasticsearch"] sink_postgres = ["tokio", "deadpool-postgres", "deadpool"] +# Sensors +sensor_datadog = [ + "opentelemetry/rt-async-std", + "opentelemetry/rt-tokio", + "opentelemetry-datadog", + "tracing-opentelemetry" +] [dependencies] #nuclei = { version = "0.2", default-features = false, features = ["epoll", "async-exec"] } @@ -95,6 +102,10 @@ deadpool-postgres = { version = "0.10.2", features = [ ], optional = true } deadpool = { version = "0.9.3", optional = true } async-global-executor = "2.0.4" +opentelemetry = { version = "0.17", features = ["rt-tokio"], optional = true } +opentelemetry-datadog = { version = "0.5", features = ["reqwest-client"], optional = true } +tracing-opentelemetry = { version = "0.17.2", optional = true } + [dev-dependencies] daemonize = "0.4.1" dirs = "4.0.0" diff --git a/src/config.rs b/src/config.rs index 9d47de5..9f482a2 100755 --- a/src/config.rs +++ b/src/config.rs @@ -2,6 +2,10 @@ use crate::kafka::enums::*; #[derive(Clone, Default)] pub struct Config { + /// + /// + pub datadog_config: Option, + /// /// Kafka Config for the service pub kafka_config: KafkaConfig, @@ -13,6 +17,26 @@ impl Config { } } +#[derive(Clone)] +pub struct DatadogConfig { + /// + /// Opentelemetry API version + pub api_version: u8, + + /// + /// Datadog agent endpoint + pub agent_endpoint: String, +} + +impl Default for DatadogConfig { + fn default() -> Self { + Self { + api_version: 5, + agent_endpoint: String::from("dd"), + } + } +} + #[derive(Clone)] pub struct KafkaConfig { /// diff --git a/src/sensors/datadog.rs b/src/sensors/datadog.rs new file mode 100644 index 0000000..c30d369 --- /dev/null +++ b/src/sensors/datadog.rs @@ -0,0 +1,51 @@ +use crate::config::DatadogConfig; +use crate::sensors::sensor::CSensor; +use opentelemetry::sdk::trace; +use opentelemetry::sdk::trace::{Sampler, Tracer}; +use opentelemetry_datadog::{new_pipeline, ApiVersion}; +use tracing::{warn, Subscriber}; +use tracing_subscriber::fmt::format::FmtSpan; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::registry::LookupSpan; +use tracing_subscriber::{EnvFilter, Layer, Registry}; + +pub struct CDataDogSensor { + app_name: String, + config: DatadogConfig, +} + +impl CDataDogSensor { + pub fn new(app_name: String, config: DatadogConfig) -> Self { + Self { app_name, config } + } + + pub fn build_layer(&self) -> Box> + where + S: Subscriber + for<'span> LookupSpan<'span>, + { + let api_version = match self.config.api_version { + 3 => ApiVersion::Version03, + 5 => ApiVersion::Version05, + _ => { + warn!("Defaulting to DataDog API Version 5."); + ApiVersion::Version05 + } + }; + let dpb = new_pipeline() + .with_service_name(self.app_name.as_str()) + .with_version(api_version) + .with_agent_endpoint(self.config.agent_endpoint.as_str()) + .with_trace_config(trace::config().with_sampler(Sampler::AlwaysOn)); + + #[cfg(feature = "asyncexec")] + let ins = dpb.install_batch(opentelemetry::runtime::AsyncStd); + #[cfg(feature = "tokio")] + let ins = dpb.install_batch(opentelemetry::runtime::Tokio); + + let tracer: Tracer = ins.unwrap(); + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + Box::new(telemetry) + } +} + +impl CSensor for CDataDogSensor {} diff --git a/src/sensors/mod.rs b/src/sensors/mod.rs index 8b13789..f5e5037 100644 --- a/src/sensors/mod.rs +++ b/src/sensors/mod.rs @@ -1 +1,7 @@ +#[cfg(feature = "sensor_datadog")] +#[cfg_attr(feature = "docs", doc(cfg(sensor_datadog)))] +/// DataDog sensor for Callysto +pub mod datadog; +/// Sensor interface for Callysto +pub mod sensor; diff --git a/src/sensors/sensor.rs b/src/sensors/sensor.rs new file mode 100644 index 0000000..a85fadf --- /dev/null +++ b/src/sensors/sensor.rs @@ -0,0 +1 @@ +pub trait CSensor {}