Skip to content

Commit

Permalink
Refactor worker to standalone crate (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy authored Aug 15, 2024
1 parent 2a923f6 commit f433815
Show file tree
Hide file tree
Showing 16 changed files with 461 additions and 273 deletions.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ header:
- 'config.m4'
- 'vendor'
- 'dist-material'
- 'target'

comment: on-failure

Expand Down
47 changes: 34 additions & 13 deletions Cargo.lock

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

19 changes: 14 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,26 @@
members = [
".",
"scripts",
"worker",
]

[package]
name = "skywalking-php"
[workspace.package]
version = "0.8.0-dev"
authors = ["Apache Software Foundation", "jmjoy <jmjoy@apache.org>", "Yanlong He <heyanlong@apache.org>"]
description = "Apache SkyWalking PHP Agent."
edition = "2021"
rust-version = "1.65"
repository = "https://github.com/apache/skywalking-php"
license = "Apache-2.0"

[package]
name = "skywalking-php"
version = { workspace = true }
authors = { workspace = true }
description = "Apache SkyWalking PHP Agent."
edition = { workspace = true }
rust-version = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
readme = "README.md"
publish = false

Expand All @@ -36,7 +45,7 @@ name = "skywalking_agent"
crate-type = ["lib", "cdylib"]

[features]
kafka-reporter = ["skywalking/kafka-reporter", "rdkafka/sasl"]
kafka-reporter = ["skywalking-php-worker/kafka-reporter"]

[dependencies]
anyhow = { version = "1.0.72", features = ["backtrace"] }
Expand All @@ -49,8 +58,8 @@ once_cell = "1.18.0"
phper = "0.12.0"
prost = "0.11.9"
rdkafka = { version = "0.32.2", optional = true }
serde_json = { version = "1.0.104", features = ["preserve_order"] }
skywalking = { version = "0.8.0", features = ["management"] }
skywalking-php-worker = { path = "worker" }
systemstat = "0.2.3"
thiserror = "1.0.44"
tokio = { version = "1.29.1", features = ["full"] }
Expand Down
1 change: 1 addition & 0 deletions config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ EOF
scripts:scripts \
src:src \
tests:tests \
worker:worker \
])
fi
11 changes: 6 additions & 5 deletions dist-material/LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ The text of each license is the standard Apache 2.0 license.
https://crates.io/crates/sasl2-sys/0.1.22+2.1.28 0.1.22+2.1.28 Apache-2.0
https://crates.io/crates/scripts/0.0.0 0.0.0 Apache-2.0
https://crates.io/crates/skywalking/0.8.0 0.8.0 Apache-2.0
https://crates.io/crates/skywalking-php-worker/0.8.0-dev 0.8.0-dev Apache-2.0
https://crates.io/crates/sync_wrapper/0.1.2 0.1.2 Apache-2.0

========================================================================
Expand Down Expand Up @@ -277,7 +278,7 @@ The text of each license is also included in licenses/LICENSE-[project].txt.
https://crates.io/crates/anstyle/1.0.1 1.0.1 Apache-2.0 OR MIT
https://crates.io/crates/anstyle-parse/0.2.1 0.2.1 Apache-2.0 OR MIT
https://crates.io/crates/anstyle-query/1.0.0 1.0.0 Apache-2.0 OR MIT
https://crates.io/crates/anstyle-wincon/1.0.1 1.0.1 Apache-2.0 OR MIT
https://crates.io/crates/anstyle-wincon/1.0.2 1.0.2 Apache-2.0 OR MIT
https://crates.io/crates/anyhow/1.0.72 1.0.72 Apache-2.0 OR MIT
https://crates.io/crates/async-trait/0.1.72 0.1.72 Apache-2.0 OR MIT
https://crates.io/crates/autocfg/1.1.0 1.1.0 Apache-2.0 OR MIT
Expand All @@ -295,8 +296,8 @@ The text of each license is also included in licenses/LICENSE-[project].txt.
https://crates.io/crates/chrono/0.4.26 0.4.26 Apache-2.0 OR MIT
https://crates.io/crates/chrono-tz/0.6.1 0.6.1 Apache-2.0 OR MIT
https://crates.io/crates/chrono-tz-build/0.0.2 0.0.2 Apache-2.0 OR MIT
https://crates.io/crates/clap/4.3.19 4.3.19 Apache-2.0 OR MIT
https://crates.io/crates/clap_builder/4.3.19 4.3.19 Apache-2.0 OR MIT
https://crates.io/crates/clap/4.3.24 4.3.24 Apache-2.0 OR MIT
https://crates.io/crates/clap_builder/4.3.24 4.3.24 Apache-2.0 OR MIT
https://crates.io/crates/clap_derive/4.3.12 4.3.12 Apache-2.0 OR MIT
https://crates.io/crates/clap_lex/0.5.0 0.5.0 Apache-2.0 OR MIT
https://crates.io/crates/colorchoice/1.0.0 1.0.0 Apache-2.0 OR MIT
Expand Down Expand Up @@ -385,7 +386,7 @@ The text of each license is also included in licenses/LICENSE-[project].txt.
https://crates.io/crates/ppv-lite86/0.2.17 0.2.17 Apache-2.0 OR MIT
https://crates.io/crates/prettyplease/0.1.25 0.1.25 Apache-2.0 OR MIT
https://crates.io/crates/proc-macro-crate/1.3.1 1.3.1 Apache-2.0 OR MIT
https://crates.io/crates/proc-macro2/1.0.66 1.0.66 Apache-2.0 OR MIT
https://crates.io/crates/proc-macro2/1.0.86 1.0.86 Apache-2.0 OR MIT
https://crates.io/crates/quick-error/1.2.3 1.2.3 Apache-2.0 OR MIT
https://crates.io/crates/quote/1.0.32 1.0.32 Apache-2.0 OR MIT
https://crates.io/crates/rand/0.8.5 0.8.5 Apache-2.0 OR MIT
Expand Down Expand Up @@ -542,7 +543,7 @@ The text of each license is also included in licenses/LICENSE-[project].txt.
https://crates.io/crates/hostname/0.3.1 0.3.1 MIT
https://crates.io/crates/http-body/0.4.5 0.4.5 MIT
https://crates.io/crates/hyper/0.14.27 0.14.27 MIT
https://crates.io/crates/is-terminal/0.4.9 0.4.9 MIT
https://crates.io/crates/is-terminal/0.4.12 0.4.12 MIT
https://crates.io/crates/matchers/0.1.0 0.1.0 MIT
https://crates.io/crates/matches/0.1.10 0.1.10 MIT
https://crates.io/crates/mio/0.8.8 0.8.8 MIT
Expand Down
10 changes: 5 additions & 5 deletions scripts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
[package]
name = "scripts"
version = "0.0.0"
authors = ["Apache Software Foundation", "jmjoy <jmjoy@apache.org>", "Yanlong He <heyanlong@apache.org>"]
authors = { workspace = true }
description = "The Scripts of Apache SkyWalking PHP Agent."
edition = "2021"
rust-version = "1.58"
repository = "https://github.com/apache/skywalking-php"
license = "Apache-2.0"
edition = { workspace = true }
rust-version = { workspace = true }
repository = { workspace = true }
license = { workspace = true }
publish = false

[dependencies]
Expand Down
24 changes: 0 additions & 24 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ use once_cell::sync::OnceCell;
use skywalking::reporter::{CollectItem, Report};
use std::{
io::Write,
mem::size_of,
ops::DerefMut,
os::unix::net::UnixStream,
path::{Path, PathBuf},
sync::Mutex,
};
use tokio::{io::AsyncReadExt, sync::mpsc};
use tracing::error;

fn channel_send<T>(data: CollectItem, mut sender: T) -> anyhow::Result<()>
Expand All @@ -40,18 +38,6 @@ where
Ok(())
}

pub async fn channel_receive(receiver: &mut tokio::net::UnixStream) -> anyhow::Result<CollectItem> {
let mut size_buf = [0u8; size_of::<usize>()];
receiver.read_exact(&mut size_buf).await?;
let size = usize::from_le_bytes(size_buf);

let mut content = vec![0u8; size];
receiver.read_exact(&mut content).await?;

let item = bincode::deserialize(&content)?;
Ok(item)
}

pub struct Reporter {
worker_addr: PathBuf,
stream: OnceCell<Mutex<UnixStream>>,
Expand Down Expand Up @@ -83,13 +69,3 @@ impl Report for Reporter {
}
}
}

pub struct TxReporter(pub mpsc::Sender<CollectItem>);

impl Report for TxReporter {
fn report(&self, item: CollectItem) {
if let Err(err) = self.0.try_send(item) {
error!(?err, "Send collect item failed");
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ mod errors;
mod execute;
mod module;
mod plugin;
mod reporter;
mod request;
mod tag;
mod util;
Expand Down
12 changes: 0 additions & 12 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use once_cell::sync::Lazy;
use phper::{ini::ini_get, sys, values::ZVal};
use std::{
ffi::CStr,
os::unix::prelude::OsStrExt,
panic::{catch_unwind, UnwindSafe},
path::Path,
};
use systemstat::{IpAddr, Platform, System};

Expand Down Expand Up @@ -87,16 +85,6 @@ pub fn get_sapi_module_name() -> &'static CStr {
unsafe { CStr::from_ptr(sys::sapi_module.name) }
}

pub fn change_permission(f: impl AsRef<Path>, mode: libc::mode_t) {
let f = f.as_ref().as_os_str().as_bytes();
let mut path = Vec::with_capacity(f.len() + 1);
path.extend_from_slice(f);
path.push(b'\0');
unsafe {
libc::chmod(path.as_ptr().cast(), mode);
}
}

pub fn get_str_ini_with_default(name: &str) -> String {
ini_get::<Option<&CStr>>(name)
.and_then(|s| s.to_str().ok())
Expand Down
Loading

0 comments on commit f433815

Please sign in to comment.