Skip to content

[#119] Add cortexbrain-common crate to eliminate code duplication#154

Merged
LorenzoTettamanti merged 3 commits intoCortexFlow:feature/ebpf-corefrom
AndreaBozzo:refactor/issue-119-common-crate
Nov 5, 2025
Merged

[#119] Add cortexbrain-common crate to eliminate code duplication#154
LorenzoTettamanti merged 3 commits intoCortexFlow:feature/ebpf-corefrom
AndreaBozzo:refactor/issue-119-common-crate

Conversation

@AndreaBozzo
Copy link
Contributor

@AndreaBozzo AndreaBozzo commented Nov 3, 2025

I'm sending this PR as lorenzo said he might already be working on few refactors, low-code but modifies few files. Works towards completing #119

Summary

Implements some code optimization for issue #119 by creating a shared cortexbrain-common crate to eliminate repeated code fragments across components.

Status: Solved my upstream problem ( xd )

Changes Made

New Shared Crate:

  • Created core/common/ with centralized utilities:
    • logger.rs: Shared logger initialization functions (init_default_logger(), init_logger_without_time())
    • constants.rs: Shared constants (BPF_PATH, PIN_MAP_PATH)

Refactored Components:

  • api (cortexflow_agent_api): Migrated to use shared logger initialization
  • identity (cortexflow_identity): Migrated to use shared logger and constants
  • metrics: Migrated to use shared logger and constants

Impact

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
  • Net change: +194 insertions / -137 deletions

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, shared data structures)
  • Seamlessly integrated with latest upstream changes (renamed packages, new features)

Files Changed

  • core/Cargo.toml - Added common to workspace
  • core/common/ - New shared crate (4 files)
  • core/api/ - Refactored to use common
  • core/src/components/identity/ - Refactored to use common
  • core/src/components/metrics/ - Refactored to use common

Test Plan

  • Build cortexbrain-common crate successfully
  • Build and verify cortexflow_agent_api component
  • Build and verify cortexflow_identity component
  • Build and verify metrics component
  • All components compile successfully after merge with upstream
  • Runtime testing (suggested for reviewers)

Integration Notes

  • Adapted to renamed packages (cortexflow_agent_api, cortexflow_identity)
  • Integrated with new features and dependencies from upstream
  • All conflicts resolved
  • All modified components build successfully

Areas worth checking out next (imo)

  • BPF loading utilities
  • Shared types crate (PacketLog, ConnArray, VethLog)
  • Error handling patterns
  • PerfEventArray helpers

…cation

This commit introduces a new shared crate `cortexbrain-common` to address
issue ( part of )CortexFlow#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 ✓
@AndreaBozzo AndreaBozzo marked this pull request as draft November 3, 2025 17:22
@AndreaBozzo AndreaBozzo closed this Nov 3, 2025
Resolved conflicts by integrating cortexbrain-common crate with upstream changes:
- Maintained cortexbrain-common dependency in all modified components
- Adapted to renamed packages (cortexflow_agent_api, cortexflow_identity)
- Integrated with new features and dependencies from upstream
- Fixed PIN_MAP_PATH reference in metrics component

All modified components compile successfully.
@AndreaBozzo AndreaBozzo reopened this Nov 3, 2025
@AndreaBozzo AndreaBozzo marked this pull request as ready for review November 3, 2025 17:41
@LorenzoTettamanti
Copy link
Member

Buonasera @AndreaBozzo , i'll check your PR in a couple of hours

@LorenzoTettamanti LorenzoTettamanti added enhancement New feature or request code refactoring Code refactoring rust Pull requests that update Rust code labels Nov 4, 2025
@LorenzoTettamanti LorenzoTettamanti moved this to In Progress in CortexFlow v0.1 Nov 4, 2025
@LorenzoTettamanti
Copy link
Member

@AndreaBozzo, checked the PR and it seems great. I'll go ahead with a runtime test today, as it's currently unchecked in the test plan. Sorry for being late, but I've had busy days

@AndreaBozzo
Copy link
Contributor Author

@AndreaBozzo, checked the PR and it seems great. I'll go ahead with a runtime test today, as it's currently unchecked in the test plan. Sorry for being late, but I've had busy days

No problem bro, as this pr implements a common usage crate that can be expanded further, also @siddh34 should take a look i think

@siddh34
Copy link
Collaborator

siddh34 commented Nov 5, 2025

Will try to test it in few hours @AndreaBozzo

Edit:

Made some exceptions for copying common into docker context

image image image

Things seems to be working so far!

Thanks mate @AndreaBozzo for contribution 🚀🚀

@AndreaBozzo
Copy link
Contributor Author

Thanks man, looking forward to cooperate further

@LorenzoTettamanti
Copy link
Member

@AndreaBozzo just tested by my side, and the code is working. I'm creating the new tags. Thanks for your PR!

@LorenzoTettamanti LorenzoTettamanti marked this pull request as draft November 5, 2025 20:08
@LorenzoTettamanti LorenzoTettamanti marked this pull request as ready for review November 5, 2025 21:46
@LorenzoTettamanti LorenzoTettamanti merged commit 93f50a4 into CortexFlow:feature/ebpf-core Nov 5, 2025
1 check passed
@LorenzoTettamanti
Copy link
Member

@AndreaBozzo @siddh34 , i've added a commit to update the dockerfiles and the building scripts and everything seems to work now.

New tags:

cortexflow-identity: identity:0.1.1-beta.3

cortexflow-agent: agent:0.1.1-beta.3

cortexflow-metrics: metrics:0.1.0

@AndreaBozzo
Copy link
Contributor Author

Awesome, if you guys have any optimization you want to suggest/try just go ahead and tell or i'll propose some work on bpf utilities next.

Also, @LorenzoTettamanti i can't straight up access the Docker links you provided becouse i lack right, but i still can see them on your public docker profiles so It should be fine.

Catch up soon

@LorenzoTettamanti
Copy link
Member

Awesome, if you guys have any optimization you want to suggest/try just go ahead and tell or i'll propose some work on bpf utilities next.

Also, @LorenzoTettamanti i can't straight up access the Docker links you provided becouse i lack right, but i still can see them on your public docker profiles so It should be fine.

Catch up soon

Sure @AndreaBozzo, I'll let you know, next week!

@LorenzoTettamanti LorenzoTettamanti moved this from In Progress to Done in CortexFlow v0.1 Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

code refactoring Code refactoring enhancement New feature or request rust Pull requests that update Rust code

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants