From 5262f56040c4d672e5e439fb74117584d6b4bbeb Mon Sep 17 00:00:00 2001 From: AndreaBozzo Date: Mon, 3 Nov 2025 18:12:14 +0100 Subject: [PATCH 1/2] [#119] Add cortexbrain-common crate to eliminate code duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit introduces a new shared crate `cortexbrain-common` to address issue ( part of )#119 regarding code optimization and reduction of repeated code fragments. ## Changes Made ### New Shared Crate - Created `core/common/` crate with: - `logger.rs`: Centralized logger initialization functions - `constants.rs`: Shared constants (BPF_PATH, PIN_MAP_PATH) ### Refactored Components - **api**: Migrated to use shared logger initialization - **identity**: Migrated to use shared logger and constants - **metrics**: Migrated to use shared logger and constants ### Code Reduction - Eliminated ~46 lines of duplicated code - Logger initialization: 5 duplicates (10 lines each) → 1 shared function - Constants: 6 duplicate declarations → 1 shared module ## Benefits - Single source of truth for logger configuration - Consistent initialization across all components - Easier maintenance and future modifications - Foundation for further refactoring (BPF utilities, error types) ## Testing All modified components compile successfully: - cortexbrain-common ✓ - api ✓ - identity ✓ - metrics ✓ --- core/Cargo.lock | 12 +++++++ core/Cargo.toml | 1 + core/api/Cargo.toml | 1 + core/api/src/main.rs | 13 ++------ core/common/Cargo.toml | 9 +++++ core/common/src/constants.rs | 7 ++++ core/common/src/lib.rs | 2 ++ core/common/src/logger.rs | 37 +++++++++++++++++++++ core/src/components/identity/Cargo.toml | 1 + core/src/components/identity/src/helpers.rs | 6 +--- core/src/components/identity/src/main.rs | 20 +++-------- core/src/components/metrics/Cargo.toml | 1 + core/src/components/metrics/src/main.rs | 17 ++-------- 13 files changed, 81 insertions(+), 46 deletions(-) create mode 100644 core/common/Cargo.toml create mode 100644 core/common/src/constants.rs create mode 100644 core/common/src/lib.rs create mode 100644 core/common/src/logger.rs diff --git a/core/Cargo.lock b/core/Cargo.lock index e6ea0f4d..0897ee99 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -46,6 +46,7 @@ dependencies = [ "aya", "bytemuck", "bytemuck_derive", + "cortexbrain-common", "identity", "prost", "tokio", @@ -344,6 +345,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "cortexbrain-common" +version = "0.1.0" +dependencies = [ + "anyhow", + "tracing", + "tracing-subscriber", +] + [[package]] name = "crc32fast" version = "1.5.0" @@ -609,6 +619,7 @@ dependencies = [ "bytemuck", "bytemuck_derive", "bytes", + "cortexbrain-common", "libc", "nix", "tokio", @@ -725,6 +736,7 @@ dependencies = [ "aya-log", "bytemuck", "bytes", + "cortexbrain-common", "libc", "nix", "tokio", diff --git a/core/Cargo.toml b/core/Cargo.toml index 088c2e97..4ccc8471 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -2,6 +2,7 @@ resolver = "3" members = [ #"src/components/loadbalancer", + "common", "api", #"src/components/proxy", #"src/components/xdp", diff --git a/core/api/Cargo.toml b/core/api/Cargo.toml index 7c869078..c82c03e3 100644 --- a/core/api/Cargo.toml +++ b/core/api/Cargo.toml @@ -12,6 +12,7 @@ tonic-prost = "0.14.0" tracing = "0.1.41" aya = "0.13.1" identity = { path = "../src/components/identity" } +cortexbrain-common = { path = "../common" } tonic-reflection = "0.14.0" tonic-build = "0.14.0" tracing-subscriber = "0.3.19" diff --git a/core/api/src/main.rs b/core/api/src/main.rs index 2c76b431..4410458e 100644 --- a/core/api/src/main.rs +++ b/core/api/src/main.rs @@ -1,6 +1,6 @@ // module imports use tonic::transport::{Error, Server}; -use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; +use cortexbrain_common::logger; mod agent; mod api; @@ -20,16 +20,7 @@ use tracing::{error, info}; #[main] async fn main() -> Result<(), Error> { //init tracing subscriber - tracing_subscriber::fmt() - .with_max_level(tracing::Level::INFO) - .with_target(false) - .with_level(true) - .with_span_events(FmtSpan::NONE) - .with_file(false) - .pretty() - .with_env_filter(EnvFilter::new("info")) - .with_line_number(false) - .init(); + logger::init_default_logger(); info!("Starting agent server..."); info!("fetching data"); diff --git a/core/common/Cargo.toml b/core/common/Cargo.toml new file mode 100644 index 00000000..6c8e04a8 --- /dev/null +++ b/core/common/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "cortexbrain-common" +version = "0.1.0" +edition = "2021" + +[dependencies] +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt"] } +anyhow = "1.0" diff --git a/core/common/src/constants.rs b/core/common/src/constants.rs new file mode 100644 index 00000000..4cc38355 --- /dev/null +++ b/core/common/src/constants.rs @@ -0,0 +1,7 @@ +/// Environment variable name for the BPF program file path. +/// Used by all components to load their eBPF programs. +pub const BPF_PATH: &str = "BPF_PATH"; + +/// Environment variable name for the BPF map pinning path. +/// Used for sharing maps between eBPF programs. +pub const PIN_MAP_PATH: &str = "PIN_MAP_PATH"; diff --git a/core/common/src/lib.rs b/core/common/src/lib.rs new file mode 100644 index 00000000..c5d43735 --- /dev/null +++ b/core/common/src/lib.rs @@ -0,0 +1,2 @@ +pub mod constants; +pub mod logger; diff --git a/core/common/src/logger.rs b/core/common/src/logger.rs new file mode 100644 index 00000000..5a1b8906 --- /dev/null +++ b/core/common/src/logger.rs @@ -0,0 +1,37 @@ +use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter}; + +/// Initialize the default logger configuration used across CortexBrain components. +/// +/// This configures tracing with: +/// - INFO level logging +/// - Pretty formatting +/// - No target, file, or line number information +/// - Environment-based filtering +pub fn init_default_logger() { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .with_target(false) + .with_level(true) + .with_span_events(FmtSpan::NONE) + .with_file(false) + .pretty() + .with_env_filter(EnvFilter::new("info")) + .with_line_number(false) + .init(); +} + +/// Initialize logger without timestamp information. +/// Used by components that don't need timestamp logging. +pub fn init_logger_without_time() { + tracing_subscriber::fmt() + .with_max_level(tracing::Level::INFO) + .with_target(false) + .with_level(true) + .with_span_events(FmtSpan::NONE) + .with_file(false) + .without_time() + .pretty() + .with_env_filter(EnvFilter::new("info")) + .with_line_number(false) + .init(); +} diff --git a/core/src/components/identity/Cargo.toml b/core/src/components/identity/Cargo.toml index ea05ad9e..5a4e408e 100644 --- a/core/src/components/identity/Cargo.toml +++ b/core/src/components/identity/Cargo.toml @@ -14,5 +14,6 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } libc = "0.2.172" bytemuck = {version ="1.23.0",features = ["derive"]} bytemuck_derive = "1.10.1" +cortexbrain-common = { path = "../../../common" } nix = { version = "0.30.1", features = ["net"] } diff --git a/core/src/components/identity/src/helpers.rs b/core/src/components/identity/src/helpers.rs index 102df17d..d3c19b7a 100644 --- a/core/src/components/identity/src/helpers.rs +++ b/core/src/components/identity/src/helpers.rs @@ -21,11 +21,7 @@ use std::{ }, }; use tracing::{error, info, warn}; - -/* - * decleare bpf path env variable - */ -const BPF_PATH: &str = "BPF_PATH"; +use cortexbrain_common::constants; /* * TryFrom Trait implementation for IpProtocols enum diff --git a/core/src/components/identity/src/main.rs b/core/src/components/identity/src/main.rs index a4fa1c14..537fcda5 100644 --- a/core/src/components/identity/src/main.rs +++ b/core/src/components/identity/src/main.rs @@ -41,26 +41,14 @@ use std::{ use anyhow::{Context, Ok}; use tokio::{fs, signal}; use tracing::{error, info}; -use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan}; - -const BPF_PATH: &str = "BPF_PATH"; //BPF env path -const PIN_MAP_PATH: &str = "PIN_MAP_PATH"; +use cortexbrain_common::{constants, logger}; use std::collections::HashMap; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { //init tracing subscriber - tracing_subscriber::fmt() - .with_max_level(tracing::Level::INFO) - .with_target(false) - .with_level(true) - .with_span_events(FmtSpan::NONE) - .with_file(false) - .pretty() - .with_env_filter(EnvFilter::new("info")) - .with_line_number(false) - .init(); + logger::init_default_logger(); info!("Starting identity service..."); info!("fetching data"); @@ -69,7 +57,7 @@ async fn main() -> Result<(), anyhow::Error> { let link_ids = Arc::new(Mutex::new(HashMap::::new())); //init conntracker data path - let bpf_path = std::env::var(BPF_PATH).context("BPF_PATH environment variable required")?; + let bpf_path = std::env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?; let data = fs::read(Path::new(&bpf_path)) .await .context("failed to load file from path")?; @@ -77,7 +65,7 @@ async fn main() -> Result<(), anyhow::Error> { //init bpf data let bpf = Arc::new(Mutex::new(Bpf::load(&data)?)); let bpf_map_save_path = - std::env::var(PIN_MAP_PATH).context("PIN_MAP_PATH environment variable required")?; + std::env::var(constants::PIN_MAP_PATH).context("PIN_MAP_PATH environment variable required")?; match init_bpf_maps(bpf.clone()) { std::result::Result::Ok(bpf_maps) => { diff --git a/core/src/components/metrics/Cargo.toml b/core/src/components/metrics/Cargo.toml index 9116a11c..b0d8c029 100644 --- a/core/src/components/metrics/Cargo.toml +++ b/core/src/components/metrics/Cargo.toml @@ -13,4 +13,5 @@ tracing = "0.1.41" tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } libc = "0.2.172" bytemuck = "1.23.0" +cortexbrain-common = { path = "../../../common" } nix ={version="0.30.1",features=["net"]} diff --git a/core/src/components/metrics/src/main.rs b/core/src/components/metrics/src/main.rs index 77913bdd..fabeeadd 100644 --- a/core/src/components/metrics/src/main.rs +++ b/core/src/components/metrics/src/main.rs @@ -21,9 +21,7 @@ use std::{ use anyhow::{Context, Ok}; use tokio::{signal}; use tracing::{error, info}; -use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan}; - -const BPF_PATH: &str = "BPF_PATH"; //BPF env path +use cortexbrain_common::{constants, logger}; mod helpers; use crate::helpers::display_metrics_map; @@ -33,21 +31,12 @@ mod structs; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { //init tracing subscriber - tracing_subscriber::fmt() - .with_max_level(tracing::Level::INFO) - .with_target(false) - .with_level(true) - .with_span_events(FmtSpan::NONE) - .with_file(false) - .pretty() - .with_env_filter(EnvFilter::new("info")) - .with_line_number(false) - .init(); + logger::init_default_logger(); info!("Starting metrics service..."); info!("fetching data"); - let bpf_path = env::var(BPF_PATH).context("BPF_PATH environment variable required")?; + let bpf_path = env::var(constants::BPF_PATH).context("BPF_PATH environment variable required")?; let data = fs::read(Path::new(&bpf_path)).context("Failed to load file from path")?; let mut bpf = Ebpf::load(&data)?; //init bpf logger From 32160f049a79bd2de8a89eb8507147463d2d6050 Mon Sep 17 00:00:00 2001 From: LorenzoTettamanti Date: Wed, 5 Nov 2025 22:46:13 +0100 Subject: [PATCH 2/2] Fixed building scripts and Dockerfiles after introducing common crate --- core/api/Dockerfile | 1 + core/src/components/identity/Dockerfile | 1 + core/src/components/identity/build-identity.sh | 2 ++ core/src/components/metrics/Dockerfile | 1 + core/src/components/metrics/build-metrics.sh | 2 ++ 5 files changed, 7 insertions(+) diff --git a/core/api/Dockerfile b/core/api/Dockerfile index 468bf97f..3946f3f6 100644 --- a/core/api/Dockerfile +++ b/core/api/Dockerfile @@ -21,6 +21,7 @@ WORKDIR /usr/src/app/agent # Copy Cargo manifest and sources COPY . . +COPY common ../common # Fetch dependencies and build release RUN cargo fetch diff --git a/core/src/components/identity/Dockerfile b/core/src/components/identity/Dockerfile index 197b5cba..30feb4ef 100644 --- a/core/src/components/identity/Dockerfile +++ b/core/src/components/identity/Dockerfile @@ -7,6 +7,7 @@ WORKDIR /usr/src/app/identity-service # Copy Cargo manifest and sources COPY Cargo.toml . COPY src ./src +COPY common ../../../common # Fetch dependencies and build release RUN cargo fetch && cargo build --release diff --git a/core/src/components/identity/build-identity.sh b/core/src/components/identity/build-identity.sh index bcee85d8..133aa0ad 100755 --- a/core/src/components/identity/build-identity.sh +++ b/core/src/components/identity/build-identity.sh @@ -8,6 +8,7 @@ popd echo "Copying connection tracker binaries" cp -r ../../../target/bpfel-unknown-none/release/conntracker conntracker +cp -r ../../../common common # Run docker build docker build -t identity:0.0.1 . @@ -15,3 +16,4 @@ docker build -t identity:0.0.1 . # Cleanup echo "Cleaning building files" rm -rf conntracker +rm -rf common diff --git a/core/src/components/metrics/Dockerfile b/core/src/components/metrics/Dockerfile index b5674bf8..8b22aaeb 100644 --- a/core/src/components/metrics/Dockerfile +++ b/core/src/components/metrics/Dockerfile @@ -7,6 +7,7 @@ WORKDIR /usr/src/app/metrics # Copy Cargo manifest and sources COPY Cargo.toml . COPY src ./src +COPY common ../../../common # Fetch dependencies and build release RUN cargo fetch && cargo build --release diff --git a/core/src/components/metrics/build-metrics.sh b/core/src/components/metrics/build-metrics.sh index 874ae1ed..fe6a9f64 100755 --- a/core/src/components/metrics/build-metrics.sh +++ b/core/src/components/metrics/build-metrics.sh @@ -8,6 +8,7 @@ popd echo "Copying metrics_tracer binaries" cp -r ../../../target/bpfel-unknown-none/release/metrics_tracer metrics_tracer +cp -r ../../../common common # Run docker build docker build -t metrics:0.0.1 . @@ -15,3 +16,4 @@ docker build -t metrics:0.0.1 . # Cleanup echo "Cleaning building files" rm -rf metrics_tracer +rm -rf common