Skip to content

Commit

Permalink
make opentelemetry config optional
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Jul 17, 2024
1 parent 9db3a1c commit fc14557
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 38 deletions.
12 changes: 1 addition & 11 deletions src/agent/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ pub struct Config {
pub metrics_server: metrics::Config,
#[serde(default)]
pub remote_keypair_loader: services::keypairs::Config,
#[serde(default)]
pub opentelemetry: OpenTelemetryConfig,
pub opentelemetry: Option<OpenTelemetryConfig>,
}

impl Config {
Expand Down Expand Up @@ -92,12 +91,3 @@ pub struct OpenTelemetryConfig {
pub exporter_timeout_secs: u64,
pub exporter_endpoint: String,
}

impl Default for OpenTelemetryConfig {
fn default() -> Self {
Self {
exporter_timeout_secs: 3,
exporter_endpoint: "http://127.0.0.1:4317".to_string(),
}
}
}
58 changes: 31 additions & 27 deletions src/bin/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,42 @@ async fn main() -> Result<()> {
.with_thread_ids(true)
.with_ansi(std::io::stderr().is_terminal());

// Set up the OpenTelemetry exporter, defaults to 127.0.0.1:4317
let otlp_exporter = opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(&config.opentelemetry.exporter_endpoint)
.with_timeout(Duration::from_secs(
config.opentelemetry.exporter_timeout_secs,
));

// Set up the OpenTelemetry tracer
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(otlp_exporter)
.with_trace_config(opentelemetry_sdk::trace::config().with_resource(
opentelemetry_sdk::Resource::new(vec![KeyValue::new("service.name", "pyth-agent")]),
))
.install_batch(opentelemetry_sdk::runtime::Tokio)
.map_err(|e| anyhow::anyhow!("Error initializing open telemetry: {}", e))?;

// Set up the telemetry layer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);

let registry = tracing_subscriber::registry()
.with(telemetry)
.with(env_filter);

let mut layers = Vec::new();
layers.push(env_filter.boxed());

// Set up OpenTelemetry only if it's configured
if let Some(opentelemetry_config) = &config.opentelemetry {
// Set up the OpenTelemetry exporter
let otlp_exporter = opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint(&opentelemetry_config.exporter_endpoint)
.with_timeout(Duration::from_secs(
opentelemetry_config.exporter_timeout_secs,
));

// Set up the OpenTelemetry tracer
let tracer = opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(otlp_exporter)
.with_trace_config(opentelemetry_sdk::trace::config().with_resource(
opentelemetry_sdk::Resource::new(vec![KeyValue::new("service.name", "pyth-agent")]),
))
.install_batch(opentelemetry_sdk::runtime::Tokio)
.map_err(|e| anyhow::anyhow!("Error initializing open telemetry: {}", e))?;

// Set up the telemetry layer
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
layers.push(telemetry.boxed());
}
// Use the compact formatter if we're in a terminal, otherwise use the JSON formatter.
if std::io::stderr().is_terminal() {
registry.with(fmt_layer.compact()).init();
layers.push(fmt_layer.compact().boxed());
} else {
registry.with(fmt_layer.json()).init();
layers.push(fmt_layer.json().boxed());
}

tracing_subscriber::registry().with(layers).init();

// Launch the application. If it fails, print the full backtrace and exit. RUST_BACKTRACE
// should be set to 1 for this otherwise it will only print the top-level error.
if let Err(err) = start(config).await {
Expand Down

0 comments on commit fc14557

Please sign in to comment.