Skip to content

Commit

Permalink
code clean
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 24, 2024
1 parent 9cfb95d commit df917ac
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 220 deletions.
1 change: 1 addition & 0 deletions vmm/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use containerd_sandbox::data::Io;

pub mod api;
pub mod mount;
pub mod signal;
pub mod storage;
pub mod tracer;

Expand Down
31 changes: 31 additions & 0 deletions vmm/common/src/signal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2024 The Kuasar Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use nix::libc::{SIGINT, SIGTERM, SIGUSR1};
use signal_hook::iterator::Signals;

use crate::tracer::{enabled, set_enabled, setup_tracing};

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() {
if sig == SIGUSR1 {
set_enabled(!enabled());
let _ = setup_tracing(log_level, otlp_service_name);
}
}
}
45 changes: 3 additions & 42 deletions vmm/common/src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ 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::{self, Tracer},
Resource,
},
trace::noop::NoopTracer,
};
use signal_hook::iterator::Signals;
use tracing_subscriber::{
layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer, Registry,
};
Expand All @@ -41,31 +37,21 @@ 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,
enable_tracing: bool,
otlp_service_name: &str,
) -> anyhow::Result<()> {
pub fn setup_tracing(log_level: &str, otlp_service_name: &str) -> anyhow::Result<()> {
let env_filter = init_logger_filter(log_level)
.map_err(|e| anyhow!("failed to init logger filter: {}", e))?;

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

if enable_tracing {
if enabled() {
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(),
);
shutdown_tracing();
}

Registry::default().with(env_filter).with(layers).init();
Expand All @@ -91,30 +77,5 @@ 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 signal {}, stopping tracing and exiting...", sig);
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);
}
}
}
}
}
Loading

0 comments on commit df917ac

Please sign in to comment.