Skip to content

Commit

Permalink
remove shutdown from signal handler
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 29, 2024
1 parent 4f11fb0 commit ecdca7c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions vmm/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ ttrpc = { version = "0.7", features = ["async"] }
protobuf = "3.2"
async-trait = "0.1"
regex = "1.5.6"
futures = { version = "0.3.21" }
signal-hook-tokio = { version = "0.3.1", features = ["futures-v0_3"] }

tracing = "0.1.40"
tracing-opentelemetry = "0.21.0"
Expand All @@ -26,8 +28,6 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
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"
13 changes: 8 additions & 5 deletions vmm/common/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ 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 futures::StreamExt;
use nix::libc;
use signal_hook_tokio::Signals;

use crate::trace;

pub async fn handle_signals(log_level: &str, otlp_service_name: &str) {
let mut signals = Signals::new([SIGTERM, SIGINT, SIGUSR1]).expect("new signal failed");
let mut signals = Signals::new([libc::SIGUSR1])
.expect("new signal failed")
.fuse();

for sig in signals.forever() {
if sig == SIGUSR1 {
while let Some(sig) = signals.next().await {
if sig == libc::SIGUSR1 {
trace::set_enabled(!trace::is_enabled());
let _ = trace::setup_tracing(log_level, otlp_service_name);
}
Expand Down
11 changes: 6 additions & 5 deletions vmm/common/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,27 @@ use tracing_subscriber::{
};

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

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

pub fn set_enabled(enabled: bool) {
ENABLED.store(enabled, Ordering::Relaxed);
TRACE_ENABLED.store(enabled, Ordering::Relaxed);
}

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()];
// TODO: shutdown tracer provider when is_enabled is false
if is_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 {
shutdown_tracing();
}

Registry::default()
Expand Down Expand Up @@ -79,6 +78,8 @@ pub fn init_otlp_tracer(otlp_service_name: &str) -> anyhow::Result<Tracer> {
Ok(tracer)
}

// TODO: may hang indefinitely, use it again when https://github.com/open-telemetry/opentelemetry-rust/issues/868 is resolved
#[allow(dead_code)]
pub fn shutdown_tracing() {
global::shutdown_tracer_provider();
}
3 changes: 2 additions & 1 deletion vmm/sandbox/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vmm/task/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions vmm/task/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,11 @@ fn init_logger(log_level: &str) -> anyhow::Result<()> {
.add_directive(format!("vmm_task={}", log_level).parse()?);

let mut layers = vec![tracing_subscriber::fmt::layer().boxed()];
// TODO: shutdown tracer provider when is_enabled is false
if trace::is_enabled() {
let tracer = trace::init_otlp_tracer("kuasar-vmm-task-service")
.map_err(|e| anyhow!("failed to init otlp tracer: {}", e))?;
layers.push(tracing_opentelemetry::layer().with_tracer(tracer).boxed());
} else {
trace::shutdown_tracing();
}

Registry::default()
Expand Down Expand Up @@ -254,8 +253,6 @@ async fn handle_signals(signals: Signals, log_level: &str) {
while let Some(sig) = signals.next().await {
match sig {
libc::SIGTERM | libc::SIGINT => {
trace::set_enabled(false);
trace::shutdown_tracing();
debug!("received {}", sig);
}
libc::SIGCHLD => loop {
Expand Down

0 comments on commit ecdca7c

Please sign in to comment.