Skip to content

Commit

Permalink
Refactor module static variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy committed Aug 17, 2023
1 parent e0021e2 commit a664338
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/pecl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ jobs:

- name: Install Rust Stable Globally
run: |
curl https://sh.rustup.rs -sSf | sudo -E sh -s -- -y
curl https://sh.rustup.rs -sSf | sudo -E sh -s -- -y --default-toolchain none
- name: PECL install
run: |
source "/tmp/cargo/env"
sudo -E cargo version
sudo -E cargo run -p scripts --release -- create-package-xml --version 0.0.0 --notes "Just for TEST."
cat package.xml
Expand Down
93 changes: 46 additions & 47 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ use std::{
use tracing::{debug, error, info, metadata::LevelFilter};
use tracing_subscriber::FmtSubscriber;

static IS_ENABLE: Lazy<bool> = Lazy::new(|| {
if !ini_get::<bool>(SKYWALKING_AGENT_ENABLE) {
return false;
}

let sapi = get_sapi_module_name().to_bytes();

if sapi == b"fpm-fcgi" {
return true;
}

if sapi == b"cli" && get_module_registry().exists("swoole") {
return true;
}

false
});

pub static SERVER_ADDR: Lazy<String> =
Lazy::new(|| get_str_ini_with_default(SKYWALKING_AGENT_SERVER_ADDR));

Expand Down Expand Up @@ -123,53 +141,50 @@ pub fn init() {
return;
}

if let Err(err) = try_init_logger() {
eprintln!("skywalking_agent: initialize logger failed: {}", err);
}

// Initialize configuration properties.
Lazy::force(&SERVER_ADDR);
Lazy::force(&SERVICE_NAME);
Lazy::force(&SERVICE_INSTANCE);
Lazy::force(&SKYWALKING_VERSION);
Lazy::force(&RUNTIME_DIR);
Lazy::force(&SOCKET_FILE_PATH);
Lazy::force(&AUTHENTICATION);
Lazy::force(&ENABLE_TLS);
Lazy::force(&SSL_TRUSTED_CA_PATH);
Lazy::force(&SSL_KEY_PATH);
Lazy::force(&SSL_CERT_CHAIN_PATH);
Lazy::force(&HEARTBEAT_PERIOD);
Lazy::force(&PROPERTIES_REPORT_PERIOD_FACTOR);
Lazy::force(&ENABLE_ZEND_OBSERVER);
Lazy::force(&WORKER_THREADS);
Lazy::force(&REPORTER_TYPE);
Lazy::force(&KAFKA_BOOTSTRAP_SERVERS);
Lazy::force(&KAFKA_PRODUCER_CONFIG);

if let Err(err) = try_init_logger() {
eprintln!("skywalking_agent: initialize logger failed: {}", err);
}

// Skywalking agent info.
let service_name = Lazy::force(&SERVICE_NAME);
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,
heartbeat_period,
properties_report_period_factor,
service_name = &*SERVICE_NAME,
service_instance = &*SERVICE_INSTANCE,
skywalking_version = &*SKYWALKING_VERSION,
heartbeat_period = &*HEARTBEAT_PERIOD,
properties_report_period_factor = &*PROPERTIES_REPORT_PERIOD_FACTOR,
"Starting skywalking agent"
);

// Skywalking version check.
if *skywalking_version < 8 {
let skywalking_version = *SKYWALKING_VERSION;
if skywalking_version < 8 {
error!(
skywalking_version,
"The skywalking agent only supports versions after skywalking 8"
);
return;
}

// Initialize TLS if enabled.
let enable_tls = Lazy::force(&ENABLE_TLS);
let ssl_trusted_ca_path = Lazy::force(&SSL_TRUSTED_CA_PATH);
let ssl_key_path = Lazy::force(&SSL_KEY_PATH);
let ssl_cert_chain_path = Lazy::force(&SSL_CERT_CHAIN_PATH);
debug!(
enable_tls,
ssl_trusted_ca_path, ssl_key_path, ssl_cert_chain_path, "Skywalking TLS info"
);

// Initialize runtime directory.
if RUNTIME_DIR.as_os_str().is_empty() {
error!("The skywalking agent runtime directory must not be empty");
Expand All @@ -181,12 +196,11 @@ pub fn init() {
}

// Initialize Agent worker.
Lazy::force(&SOCKET_FILE_PATH);
init_worker();

tracer::set_global_tracer(Tracer::new(
service_name,
service_instance,
&*SERVICE_NAME,
&*SERVICE_INSTANCE,
Reporter::new(&*SOCKET_FILE_PATH),
));

Expand Down Expand Up @@ -244,27 +258,12 @@ fn try_init_logger() -> anyhow::Result<()> {
Ok(())
}

#[inline]
fn get_module_registry() -> &'static ZArr {
unsafe { ZArr::from_ptr(&sys::module_registry) }
}

#[inline]
pub fn is_enable() -> bool {
static IS_ENABLE: Lazy<bool> = Lazy::new(|| {
if !ini_get::<bool>(SKYWALKING_AGENT_ENABLE) {
return false;
}

let sapi = get_sapi_module_name().to_bytes();

if sapi == b"fpm-fcgi" {
return true;
}

if sapi == b"cli" && get_module_registry().exists("swoole") {
return true;
}

false
});
*IS_ENABLE
}
8 changes: 8 additions & 0 deletions src/reporter/reporter_grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ async fn create_endpoint(server_addr: &str) -> anyhow::Result<Endpoint> {
debug!(url, "Create Endpoint");
let mut endpoint = Endpoint::from_shared(url)?;

debug!(
enable_tls = *ENABLE_TLS,
ssl_trusted_ca_path = &*SSL_TRUSTED_CA_PATH,
ssl_key_path = &*SSL_KEY_PATH,
ssl_cert_chain_path = &*SSL_CERT_CHAIN_PATH,
"Skywalking TLS info"
);

if *ENABLE_TLS {
let domain_name = server_addr.split(':').next().unwrap_or_default();
debug!(domain_name, "Configure TLS domain");
Expand Down
12 changes: 5 additions & 7 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use crate::{
channel::{self, TxReporter},
module::{
HEARTBEAT_PERIOD, PROPERTIES_REPORT_PERIOD_FACTOR,
SERVICE_INSTANCE, SERVICE_NAME, SOCKET_FILE_PATH, WORKER_THREADS,
HEARTBEAT_PERIOD, PROPERTIES_REPORT_PERIOD_FACTOR, SERVICE_INSTANCE, SERVICE_NAME,
SOCKET_FILE_PATH, WORKER_THREADS,
},
reporter::run_reporter,
util::change_permission,
Expand All @@ -30,8 +30,8 @@ use skywalking::{
reporter::{CollectItem, CollectItemConsume},
};
use std::{
cmp::Ordering, error::Error, fs, io, marker::PhantomData, num::NonZeroUsize,
process::exit, thread::available_parallelism, time::Duration,
cmp::Ordering, error::Error, fs, io, marker::PhantomData, num::NonZeroUsize, process::exit,
thread::available_parallelism, time::Duration,
};
use tokio::{
net::UnixListener,
Expand All @@ -40,9 +40,7 @@ use tokio::{
signal::unix::{signal, SignalKind},
sync::mpsc::{self, error::TrySendError},
};
use tonic::{
async_trait,
};
use tonic::async_trait;
use tracing::{debug, error, info, warn};

pub fn init_worker() {
Expand Down

0 comments on commit a664338

Please sign in to comment.