From 8b85bf1d96fd5a7469af7a5fcc3cc8727d298bd5 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Sat, 28 Jan 2023 16:02:23 +0800 Subject: [PATCH 1/2] Periodic reporting instance properties. --- src/worker.rs | 49 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/worker.rs b/src/worker.rs index 75912af..262dffe 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -34,7 +34,7 @@ use skywalking::{ }; use std::{ cmp::Ordering, error::Error, ffi::CStr, fs, io, marker::PhantomData, num::NonZeroUsize, - process::exit, thread::available_parallelism, time::Duration, + process::exit, sync::Arc, thread::available_parallelism, time::Duration, }; use tokio::{ net::UnixListener, @@ -42,7 +42,7 @@ use tokio::{ select, signal::unix::{signal, SignalKind}, sync::mpsc::{self, error::TrySendError}, - time::sleep, + time::{self, sleep}, }; use tonic::{ async_trait, @@ -297,18 +297,41 @@ impl Drop for WorkerExitGuard { } } -fn report_properties_and_keep_alive(reporter: TxReporter) { - let manager = Manager::new(&*SERVICE_NAME, &*SERVICE_INSTANCE, reporter); +/// The period in which the agent report a heartbeat to the backend. +const HEARTBEAT_PERIOD: u64 = 30; - let mut props = Properties::new(); - props.insert_os_info(); - props.update(Properties::KEY_LANGUAGE, "php"); - props.update(Properties::KEY_PROCESS_NO, unsafe { - libc::getppid().to_string() - }); - debug!(?props, "Report instance properties"); +/// The agent sends the instance properties to the backend every +/// `collector.heartbeat_period * collector.properties_report_period_factor` +/// seconds +const PROPERTIES_REPORT_PERIOD_FACTOR: u64 = 10; - manager.report_properties(props); +fn report_properties_and_keep_alive(reporter: TxReporter) { + let manager = Arc::new(Manager::new(&*SERVICE_NAME, &*SERVICE_INSTANCE, reporter)); + let manager_ = manager.clone(); + + // TODO Refactor report instance properties and keep alive like skywalking-java. + // + // In skywalking-java, when properties_report_period reached, report the + // instance properties without keep alive. However, this needs to change the + // api of skywalking-rust, so wait for the next version of skywalking-rust. + tokio::spawn(async move { + let mut ticker = time::interval(Duration::from_secs( + HEARTBEAT_PERIOD * PROPERTIES_REPORT_PERIOD_FACTOR, + )); + loop { + ticker.tick().await; + + let mut props = Properties::new(); + props.insert_os_info(); + props.update(Properties::KEY_LANGUAGE, "php"); + props.update(Properties::KEY_PROCESS_NO, unsafe { + libc::getppid().to_string() + }); + debug!(?props, "Report instance properties"); + + manager_.report_properties(props); + } + }); - manager.keep_alive(Duration::from_secs(10)); + manager.keep_alive(Duration::from_secs(HEARTBEAT_PERIOD)); } From 168501e02ec4233c97619bffdd1356298d2d4d79 Mon Sep 17 00:00:00 2001 From: jmjoy Date: Sun, 29 Jan 2023 14:49:32 +0800 Subject: [PATCH 2/2] Add config options. --- .../setup/service-agent/php-agent/README.md | 7 +++++ src/lib.rs | 14 ++++++++++ src/module.rs | 18 +++++++++++-- src/worker.rs | 26 +++++++++---------- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/docs/en/setup/service-agent/php-agent/README.md b/docs/en/setup/service-agent/php-agent/README.md index 896d4c1..adc0a7e 100644 --- a/docs/en/setup/service-agent/php-agent/README.md +++ b/docs/en/setup/service-agent/php-agent/README.md @@ -96,4 +96,11 @@ skywalking_agent.skywalking_version = 8 ; The certificate file. Enable mTLS when ssl_key_path and ssl_cert_chain_path exist. ; skywalking_agent.ssl_cert_chain_path = + +; Agent heartbeat report period. Unit, second. Default is 30. +; skywalking_agent.heartbeat_period = 30 + +; The agent sends the instance properties to the backend every +; heartbeat_period * properties_report_period_factor seconds. Default is 10. +; skywalking_agent.properties_report_period_factor = 10 ``` diff --git a/src/lib.rs b/src/lib.rs index 5237d62..4987d2a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,14 @@ const SKYWALKING_AGENT_SSL_KEY_PATH: &str = "skywalking_agent.ssl_key_path"; /// exist. const SKYWALKING_AGENT_SSL_CERT_CHAIN_PATH: &str = "skywalking_agent.ssl_cert_chain_path"; +/// Agent heartbeat report period. Unit, second. +const SKYWALKING_AGENT_HEARTBEAT_PERIOD: &str = "skywalking_agent.heartbeat_period"; + +/// The agent sends the instance properties to the backend every +/// heartbeat_period * properties_report_period_factor seconds. +const SKYWALKING_AGENT_PROPERTIES_REPORT_PERIOD_FACTOR: &str = + "skywalking_agent.properties_report_period_factor"; + #[php_get_module] pub fn get_module() -> Module { let mut module = Module::new( @@ -132,6 +140,12 @@ pub fn get_module() -> Module { "".to_string(), Policy::System, ); + module.add_ini(SKYWALKING_AGENT_HEARTBEAT_PERIOD, 30i64, Policy::System); + module.add_ini( + SKYWALKING_AGENT_PROPERTIES_REPORT_PERIOD_FACTOR, + 10i64, + Policy::System, + ); // Hooks. module.on_module_init(module::init); diff --git a/src/module.rs b/src/module.rs index 9cbb52c..dc99ced 100644 --- a/src/module.rs +++ b/src/module.rs @@ -19,7 +19,8 @@ use crate::{ util::{get_sapi_module_name, IPS}, worker::init_worker, SKYWALKING_AGENT_AUTHENTICATION, SKYWALKING_AGENT_ENABLE, SKYWALKING_AGENT_ENABLE_TLS, - SKYWALKING_AGENT_LOG_FILE, SKYWALKING_AGENT_LOG_LEVEL, SKYWALKING_AGENT_RUNTIME_DIR, + SKYWALKING_AGENT_HEARTBEAT_PERIOD, SKYWALKING_AGENT_LOG_FILE, SKYWALKING_AGENT_LOG_LEVEL, + SKYWALKING_AGENT_PROPERTIES_REPORT_PERIOD_FACTOR, SKYWALKING_AGENT_RUNTIME_DIR, SKYWALKING_AGENT_SERVICE_NAME, SKYWALKING_AGENT_SKYWALKING_VERSION, SKYWALKING_AGENT_SSL_CERT_CHAIN_PATH, SKYWALKING_AGENT_SSL_KEY_PATH, SKYWALKING_AGENT_SSL_TRUSTED_CA_PATH, @@ -109,6 +110,12 @@ pub static SSL_CERT_CHAIN_PATH: Lazy = Lazy::new(|| { .unwrap_or_default() }); +pub static HEARTBEAT_PERIOD: Lazy = + Lazy::new(|| ini_get::(SKYWALKING_AGENT_HEARTBEAT_PERIOD)); + +pub static PROPERTIES_REPORT_PERIOD_FACTOR: Lazy = + Lazy::new(|| ini_get::(SKYWALKING_AGENT_PROPERTIES_REPORT_PERIOD_FACTOR)); + pub fn init() { if !is_enable() { return; @@ -121,9 +128,16 @@ pub fn init() { let service_instance = Lazy::force(&SERVICE_INSTANCE); let skywalking_version = Lazy::force(&SKYWALKING_VERSION); let authentication = Lazy::force(&AUTHENTICATION); + let heartbeat_period = Lazy::force(&HEARTBEAT_PERIOD); + let properties_report_period_factor = Lazy::force(&PROPERTIES_REPORT_PERIOD_FACTOR); info!( service_name, - service_instance, skywalking_version, authentication, "Starting skywalking agent" + service_instance, + skywalking_version, + authentication, + heartbeat_period, + properties_report_period_factor, + "Starting skywalking agent" ); // Skywalking version check. diff --git a/src/worker.rs b/src/worker.rs index 262dffe..8b98104 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -16,8 +16,9 @@ use crate::{ channel::{self, TxReporter}, module::{ - AUTHENTICATION, ENABLE_TLS, SERVICE_INSTANCE, SERVICE_NAME, SOCKET_FILE_PATH, - SSL_CERT_CHAIN_PATH, SSL_KEY_PATH, SSL_TRUSTED_CA_PATH, + AUTHENTICATION, ENABLE_TLS, HEARTBEAT_PERIOD, PROPERTIES_REPORT_PERIOD_FACTOR, + SERVICE_INSTANCE, SERVICE_NAME, SOCKET_FILE_PATH, SSL_CERT_CHAIN_PATH, SSL_KEY_PATH, + SSL_TRUSTED_CA_PATH, }, util::change_permission, SKYWALKING_AGENT_SERVER_ADDR, SKYWALKING_AGENT_WORKER_THREADS, @@ -297,14 +298,6 @@ impl Drop for WorkerExitGuard { } } -/// The period in which the agent report a heartbeat to the backend. -const HEARTBEAT_PERIOD: u64 = 30; - -/// The agent sends the instance properties to the backend every -/// `collector.heartbeat_period * collector.properties_report_period_factor` -/// seconds -const PROPERTIES_REPORT_PERIOD_FACTOR: u64 = 10; - fn report_properties_and_keep_alive(reporter: TxReporter) { let manager = Arc::new(Manager::new(&*SERVICE_NAME, &*SERVICE_INSTANCE, reporter)); let manager_ = manager.clone(); @@ -315,9 +308,12 @@ fn report_properties_and_keep_alive(reporter: TxReporter) { // instance properties without keep alive. However, this needs to change the // api of skywalking-rust, so wait for the next version of skywalking-rust. tokio::spawn(async move { - let mut ticker = time::interval(Duration::from_secs( - HEARTBEAT_PERIOD * PROPERTIES_REPORT_PERIOD_FACTOR, - )); + let period = *HEARTBEAT_PERIOD * *PROPERTIES_REPORT_PERIOD_FACTOR; + if period <= 0 { + return; + } + + let mut ticker = time::interval(Duration::from_secs(period as u64)); loop { ticker.tick().await; @@ -333,5 +329,7 @@ fn report_properties_and_keep_alive(reporter: TxReporter) { } }); - manager.keep_alive(Duration::from_secs(HEARTBEAT_PERIOD)); + if *HEARTBEAT_PERIOD <= 0 { + manager.keep_alive(Duration::from_secs(*HEARTBEAT_PERIOD as u64)); + } }