You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2023/01/29 08:13:27 UTC

[skywalking-php] branch master updated: Periodic reporting instance properties. (#50)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-php.git


The following commit(s) were added to refs/heads/master by this push:
     new 02086d8  Periodic reporting instance properties. (#50)
02086d8 is described below

commit 02086d8f209b287a469518b5e60881d21099432b
Author: jmjoy <jm...@apache.org>
AuthorDate: Sun Jan 29 16:13:21 2023 +0800

    Periodic reporting instance properties. (#50)
---
 docs/en/setup/service-agent/php-agent/README.md |  7 ++++
 src/lib.rs                                      | 14 +++++++
 src/module.rs                                   | 18 ++++++++-
 src/worker.rs                                   | 49 ++++++++++++++++++-------
 4 files changed, 72 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<String> = Lazy::new(|| {
         .unwrap_or_default()
 });
 
+pub static HEARTBEAT_PERIOD: Lazy<i64> =
+    Lazy::new(|| ini_get::<i64>(SKYWALKING_AGENT_HEARTBEAT_PERIOD));
+
+pub static PROPERTIES_REPORT_PERIOD_FACTOR: Lazy<i64> =
+    Lazy::new(|| ini_get::<i64>(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 75912af..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,
@@ -34,7 +35,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 +43,7 @@ use tokio::{
     select,
     signal::unix::{signal, SignalKind},
     sync::mpsc::{self, error::TrySendError},
-    time::sleep,
+    time::{self, sleep},
 };
 use tonic::{
     async_trait,
@@ -298,17 +299,37 @@ impl Drop for WorkerExitGuard {
 }
 
 fn report_properties_and_keep_alive(reporter: TxReporter) {
-    let manager = Manager::new(&*SERVICE_NAME, &*SERVICE_INSTANCE, reporter);
+    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 period = *HEARTBEAT_PERIOD * *PROPERTIES_REPORT_PERIOD_FACTOR;
+        if period <= 0 {
+            return;
+        }
 
-    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");
+        let mut ticker = time::interval(Duration::from_secs(period as u64));
+        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_.report_properties(props);
+        }
+    });
 
-    manager.keep_alive(Duration::from_secs(10));
+    if *HEARTBEAT_PERIOD <= 0 {
+        manager.keep_alive(Duration::from_secs(*HEARTBEAT_PERIOD as u64));
+    }
 }