Skip to content
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
19 changes: 19 additions & 0 deletions src/bin/dolos/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use dolos_snapshot::registry::Auth;
use miette::{Context as _, IntoDiagnostic};
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_otlp::WithExportConfig as _;
use opentelemetry_sdk::trace::SdkTracerProvider;
use std::sync::Arc;
use std::sync::OnceLock;
use std::{fs, path::PathBuf, time::Duration};
use tokio_util::sync::CancellationToken;
use tracing::{debug, info};
Expand All @@ -20,6 +22,8 @@ use dolos::storage;

pub type Stores = storage::Stores<dolos_cardano::CardanoDelta>;

static TRACER_PROVIDER: OnceLock<SdkTracerProvider> = OnceLock::new();

/// Ensure the storage root directory exists.
pub fn ensure_storage_path(config: &RootConfig) -> Result<PathBuf, Error> {
storage::ensure_storage_path(config)
Expand Down Expand Up @@ -285,6 +289,7 @@ pub fn setup_tracing(config: &LoggingConfig, telemetry: &TelemetryConfig) -> mie
.build();

opentelemetry::global::set_tracer_provider(tracer.clone());
let _ = TRACER_PROVIDER.set(tracer.clone());

let layer = tracing_opentelemetry::layer().with_tracer(tracer.tracer("dolos"));
Some(layer)
Expand Down Expand Up @@ -319,6 +324,20 @@ pub fn setup_tracing(config: &LoggingConfig, telemetry: &TelemetryConfig) -> mie
Ok(())
}

/// Flush and stop the batch span exporter before the process exits.
///
/// The global provider owns a clone, so merely dropping the local provider from
/// `setup_tracing` does not stop its worker or export its final batch. Keeping a
/// handle here lets the CLI perform an explicit shutdown after every command,
/// including commands that do not run the long-lived daemon pipeline.
pub fn shutdown_tracing() {
if let Some(provider) = TRACER_PROVIDER.get() {
if let Err(error) = provider.shutdown() {
eprintln!("failed to shut down OpenTelemetry tracer provider: {error}");
}
}
}

pub fn open_genesis_files(config: &GenesisConfig) -> miette::Result<Genesis> {
Genesis::from_file_paths(
&config.byron_path,
Expand Down
7 changes: 5 additions & 2 deletions src/bin/dolos/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn main() -> Result<()> {

let feedback = crate::feedback::Feedback::default();

match (config, args.command) {
let result = match (config, args.command) {
(Ok(config), Command::Daemon(args)) => daemon::run(config, &args),
(Ok(config), Command::Sync(args)) => sync::run(&config, &args),
(Ok(config), Command::Serve(args)) => serve::run(config, &args),
Expand All @@ -119,5 +119,8 @@ fn main() -> Result<()> {
(Ok(config), Command::Minikupo(x)) => minikupo::run(&config, &x),

(Err(x), _) => Err(x),
}
};

crate::common::shutdown_tracing();
result
}