Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Basic setup for datadog #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"] }
Expand Down Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use crate::kafka::enums::*;

#[derive(Clone, Default)]
pub struct Config {
///
///
pub datadog_config: Option<DatadogConfig>,

///
/// Kafka Config for the service
pub kafka_config: KafkaConfig,
Expand All @@ -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 {
///
Expand Down
51 changes: 51 additions & 0 deletions src/sensors/datadog.rs
Original file line number Diff line number Diff line change
@@ -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<S>(&self) -> Box<dyn Layer<S>>
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 {}
6 changes: 6 additions & 0 deletions src/sensors/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions src/sensors/sensor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub trait CSensor {}