Skip to content

Commit

Permalink
signal hander for tracer enabled
Browse files Browse the repository at this point in the history
Signed-off-by: Ziy1-Tan <[email protected]>
  • Loading branch information
Ziy1-Tan committed Oct 19, 2024
1 parent b017ce8 commit 1014996
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
5 changes: 3 additions & 2 deletions vmm/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ protobuf = "3.2"
async-trait = "0.1"
regex = "1.5.6"

tracing-log = "0.2.0"
tracing = "0.1.40"
tracing-opentelemetry = "0.21.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "tracing-log"] }

opentelemetry = { version = "0.20.0", features = ["rt-tokio"] }
opentelemetry-otlp = "0.13.0"

signal-hook = "0.3.1"

[build-dependencies]
ttrpc-codegen = { git = "https://github.com/kuasar-io/ttrpc-rust.git", branch = "v0.7.1-kuasar" }
tonic-build = "0.7.2"
68 changes: 61 additions & 7 deletions vmm/common/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,37 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

use std::sync::atomic::{AtomicBool, Ordering};

use anyhow::anyhow;
use lazy_static::lazy_static;
use log::{debug, info, warn};
use nix::libc::{SIGINT, SIGTERM, SIGUSR1};
use opentelemetry::{
global,
sdk::{trace, trace::Tracer, Resource},
sdk::{
trace::{self, Tracer},
Resource,
},
trace::noop::NoopTracer,
};
use signal_hook::iterator::Signals;
use tracing_subscriber::{
layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, Registry,
};
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Layer, Registry};

lazy_static! {
static ref ENABLED: AtomicBool = AtomicBool::new(false);
}

pub fn enabled() -> bool {
ENABLED.load(Ordering::Relaxed)
}

// 设置 ENABLED 的值
pub fn set_enabled(enabled: bool) {
ENABLED.store(enabled, Ordering::Relaxed);
}

pub fn setup_tracing(
log_level: &str,
Expand All @@ -29,18 +54,21 @@ pub fn setup_tracing(
let env_filter = init_logger_filter(log_level)
.map_err(|e| anyhow!("failed to init logger filter: {}", e))?;

tracing_log::LogTracer::init().map_err(|e| anyhow!("failed to init LogTracer: {}", e))?;

let mut layers = vec![tracing_subscriber::fmt::layer().boxed()];

if enable_tracing {
let tracer = init_otlp_tracer(otlp_service_name)
.map_err(|e| anyhow!("failed to init otlp tracer: {}", e))?;
layers.push(tracing_opentelemetry::layer().with_tracer(tracer).boxed());
} else {
layers.push(
tracing_opentelemetry::layer()
.with_tracer(NoopTracer::new())
.boxed(),
);
}

let subscriber = Registry::default().with(env_filter).with(layers);
tracing::subscriber::set_global_default(subscriber)
.map_err(|e| anyhow!("failed to set global subscriber: {}", e))?;
Registry::default().with(env_filter).with(layers).init();
Ok(())
}

Expand All @@ -63,5 +91,31 @@ pub fn init_otlp_tracer(otlp_service_name: &str) -> anyhow::Result<Tracer> {
}

pub fn shutdown_tracing() {
set_enabled(false);
global::shutdown_tracer_provider();
}

pub async fn handle_signals(log_level: &str, otlp_service_name: &str) {
let mut signals = Signals::new([SIGTERM, SIGINT, SIGUSR1]).expect("new signal failed");
for sig in signals.forever() {
match sig {
SIGUSR1 => {
set_enabled(!enabled());
let _ = setup_tracing(log_level, enabled(), otlp_service_name);
}
SIGINT | SIGTERM => {
// 处理退出信号,执行清理操作并退出
info!("Received exit signal, stopping tracing and exiting...");
shutdown_tracing();
std::process::exit(0); // 优雅退出
}
_ => {
if let Ok(sig) = nix::sys::signal::Signal::try_from(sig) {
debug!("received {}", sig);
} else {
warn!("received invalid signal {}", sig);
}
}
}
}
}

0 comments on commit 1014996

Please sign in to comment.