Skip to content

Commit

Permalink
Expand telemetry config (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop authored May 16, 2024
1 parent f275dd5 commit fae0ba9
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 58 deletions.
2 changes: 0 additions & 2 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
[service]
escalation_interval = "1m"
datadog_enabled = false
statsd_enabled = false

[service.predefined.network]
chain_id = 31337
Expand Down
108 changes: 77 additions & 31 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,23 @@ pub struct TxSitterConfig {
#[serde(with = "humantime_serde")]
pub escalation_interval: Duration,

#[serde(with = "humantime_serde", default = "default_soft_reorg_interval")]
#[serde(
with = "humantime_serde",
default = "default::soft_reorg_interval"
)]
pub soft_reorg_interval: Duration,

#[serde(with = "humantime_serde", default = "default_hard_reorg_interval")]
#[serde(
with = "humantime_serde",
default = "default::hard_reorg_interval"
)]
pub hard_reorg_interval: Duration,

#[serde(default)]
pub datadog_enabled: bool,

#[serde(default)]
pub statsd_enabled: bool,

#[serde(default, skip_serializing_if = "Option::is_none")]
pub predefined: Option<Predefined>,
}

const fn default_soft_reorg_interval() -> Duration {
Duration::from_secs(60)
}

const fn default_hard_reorg_interval() -> Duration {
Duration::from_secs(60 * 60)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub telemetry: Option<TelemetryConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -186,6 +181,59 @@ impl KeysConfig {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TelemetryConfig {
// Service name - used for logging, metrics and tracing
pub service_name: String,
// Traces
pub traces_endpoint: Option<String>,
// Metrics
pub metrics: Option<MetricsConfig>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
#[serde(default = "default::metrics::host")]
pub host: String,
#[serde(default = "default::metrics::port")]
pub port: u16,
#[serde(default = "default::metrics::queue_size")]
pub queue_size: usize,
#[serde(default = "default::metrics::buffer_size")]
pub buffer_size: usize,
pub prefix: String,
}

mod default {
use super::*;

pub fn soft_reorg_interval() -> Duration {
Duration::from_secs(60)
}

pub fn hard_reorg_interval() -> Duration {
Duration::from_secs(60 * 60)
}

pub mod metrics {
pub fn host() -> String {
"127.0.0.1".to_string()
}

pub fn port() -> u16 {
8125
}

pub fn queue_size() -> usize {
5000
}

pub fn buffer_size() -> usize {
256
}
}
}

#[cfg(test)]
mod tests {
use indoc::indoc;
Expand All @@ -197,8 +245,6 @@ mod tests {
escalation_interval = "1h"
soft_reorg_interval = "1m"
hard_reorg_interval = "1h"
datadog_enabled = false
statsd_enabled = false
[server]
host = "127.0.0.1:3000"
Expand All @@ -216,8 +262,6 @@ mod tests {
escalation_interval = "1h"
soft_reorg_interval = "1m"
hard_reorg_interval = "1h"
datadog_enabled = false
statsd_enabled = false
[server]
host = "127.0.0.1:3000"
Expand All @@ -239,11 +283,10 @@ mod tests {
let config = Config {
service: TxSitterConfig {
escalation_interval: Duration::from_secs(60 * 60),
soft_reorg_interval: default_soft_reorg_interval(),
hard_reorg_interval: default_hard_reorg_interval(),
datadog_enabled: false,
statsd_enabled: false,
soft_reorg_interval: default::soft_reorg_interval(),
hard_reorg_interval: default::hard_reorg_interval(),
predefined: None,
telemetry: None,
},
server: ServerConfig {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
Expand All @@ -267,11 +310,10 @@ mod tests {
let config = Config {
service: TxSitterConfig {
escalation_interval: Duration::from_secs(60 * 60),
soft_reorg_interval: default_soft_reorg_interval(),
hard_reorg_interval: default_hard_reorg_interval(),
datadog_enabled: false,
statsd_enabled: false,
soft_reorg_interval: default::soft_reorg_interval(),
hard_reorg_interval: default::hard_reorg_interval(),
predefined: None,
telemetry: None,
},
server: ServerConfig {
host: SocketAddr::from(([127, 0, 0, 1], 3000)),
Expand Down Expand Up @@ -302,17 +344,21 @@ mod tests {
std::env::set_var("TX_SITTER__DATABASE__USERNAME", "dbUsername");
std::env::set_var("TX_SITTER__DATABASE__PASSWORD", "dbPassword");
std::env::set_var("TX_SITTER__SERVICE__ESCALATION_INTERVAL", "1m");
std::env::set_var("TX_SITTER__SERVICE__DATADOG_ENABLED", "true");
std::env::set_var("TX_SITTER__SERVICE__STATSD_ENABLED", "true");
std::env::set_var(
"TX_SITTER__SERVICE__TELEMETRY__SERVICE_NAME",
"tx-sitter",
);
std::env::set_var("TX_SITTER__SERVER__HOST", "0.0.0.0:8080");
std::env::set_var("TX_SITTER__SERVER__USERNAME", "authUsername");
std::env::set_var("TX_SITTER__SERVER__PASSWORD", "authPassword");
std::env::set_var("TX_SITTER__KEYS__KIND", "kms");

let config = load_config(std::iter::empty()).unwrap();

assert!(config.service.statsd_enabled);
assert!(config.service.datadog_enabled);
assert_eq!(
config.service.telemetry.as_ref().unwrap().service_name,
"tx-sitter"
);
assert_eq!(config.service.escalation_interval, Duration::from_secs(60));
assert_eq!(
config.database.to_connection_string(),
Expand Down
47 changes: 26 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::path::PathBuf;
use clap::Parser;
use telemetry_batteries::metrics::statsd::StatsdBattery;
use telemetry_batteries::tracing::datadog::DatadogBattery;
use telemetry_batteries::tracing::TracingShutdownHandle;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::EnvFilter;
use tx_sitter::config::load_config;
use tx_sitter::service::Service;
use tx_sitter::shutdown::spawn_await_shutdown_task;
Expand Down Expand Up @@ -37,29 +37,34 @@ async fn main() -> eyre::Result<()> {

let config = load_config(args.config.iter().map(PathBuf::as_ref))?;

let _shutdown_handle = if config.service.datadog_enabled {
let shutdown_handle =
DatadogBattery::init(None, "tx-sitter-monolith", None, true);
let _tracing_shutdown_handle =
if let Some(telemetry) = &config.service.telemetry {
let tracing_shutdown_handle = DatadogBattery::init(
telemetry.traces_endpoint.as_deref(),
&telemetry.service_name,
None,
true,
);

Some(shutdown_handle)
} else {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().pretty().compact())
.with(EnvFilter::from_default_env())
.init();
if let Some(metrics_config) = &telemetry.metrics {
StatsdBattery::init(
&metrics_config.host,
metrics_config.port,
metrics_config.queue_size,
metrics_config.buffer_size,
Some(&metrics_config.prefix),
)?;
}

None
};
tracing_shutdown_handle
} else {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().pretty().compact())
.with(tracing_subscriber::EnvFilter::from_default_env())
.init();

if config.service.statsd_enabled {
StatsdBattery::init(
"localhost",
8125,
5000,
1024,
Some("tx_sitter_monolith"),
)?;
}
TracingShutdownHandle
};

spawn_await_shutdown_task();

Expand Down
6 changes: 4 additions & 2 deletions src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ impl Service {
task_runner.add_task("Handle soft reorgs", tasks::handle_soft_reorgs);
task_runner.add_task("Handle hard reorgs", tasks::handle_hard_reorgs);

if app.config.service.statsd_enabled {
task_runner.add_task("Emit metrics", tasks::emit_metrics);
if let Some(telemetry_config) = app.config.service.telemetry.as_ref() {
if telemetry_config.metrics.is_some() {
task_runner.add_task("Emit metrics", tasks::emit_metrics);
}
}

for chain_id in chain_ids {
Expand Down
3 changes: 1 addition & 2 deletions tests/common/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ impl ServiceBuilder {
escalation_interval: self.escalation_interval,
soft_reorg_interval: self.soft_reorg_interval,
hard_reorg_interval: self.hard_reorg_interval,
datadog_enabled: false,
statsd_enabled: false,
telemetry: None,
predefined: Some(Predefined {
network: PredefinedNetwork {
chain_id: DEFAULT_ANVIL_CHAIN_ID,
Expand Down

0 comments on commit fae0ba9

Please sign in to comment.