Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Periodic reporting instance properties. #50

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/en/setup/service-agent/php-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 16 additions & 2 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down
49 changes: 35 additions & 14 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,15 +35,15 @@ 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,
runtime::{self, Runtime},
select,
signal::unix::{signal, SignalKind},
sync::mpsc::{self, error::TrySendError},
time::sleep,
time::{self, sleep},
};
use tonic::{
async_trait,
Expand Down Expand Up @@ -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));
}
}