rust version implementation of procdockerstatsd#227
rust version implementation of procdockerstatsd#227FengPan-Frank wants to merge 1 commit intosonic-net:masterfrom
Conversation
|
/azp run |
|
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
scripts/rs/Cargo.toml
Outdated
There was a problem hiding this comment.
this is the rusttoml edition, and yea, it is 2024 for now.
scripts/rs/main.rs
Outdated
scripts/rs/main.rs
Outdated
scripts/rs/main.rs
Outdated
scripts/rs/main.rs
Outdated
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
| @@ -0,0 +1,18 @@ | |||
| [package] | |||
| name = "procdockerstatsd_rs" | |||
| authors = ["Feng Pan"] | ||
|
|
||
| [dependencies] | ||
| redis = "0.23" |
There was a problem hiding this comment.
we have wrapped the swss-common rust APIs here: https://github.com/sonic-net/sonic-dash-ha/tree/master/crates/swss-common, so I wonder if we can use it instead of using redis package directly.
| @@ -0,0 +1,18 @@ | |||
| [package] | |||
There was a problem hiding this comment.
should we update the folder to procdockerstatsd to be more explicit?
if we like a single crate to have multiple bin, then updating the main.rs into procdockerstatsd.rs will be better for future.
| } | ||
|
|
||
| fn convert_to_bytes(value: &str) -> u64 { | ||
| let re = Regex::new(r"(\d+\.?\d*)([a-zA-Z]+)").unwrap(); |
There was a problem hiding this comment.
This recompiles the regex on every call. This is bad practice, though admittedly if it's just once every 2 minutes it won't matter. This code will cache it for you.
fn convert_to_bytes(value: &str) -> u64 {
static RE: Regex = LazyLock::new(|| Regex::new(r"(\d+\.?\d*)([a-zA-Z]+)").unwrap());
if let Some(caps) = RE.captures(value) {
...| let mut stats = HashMap::new(); | ||
| stats.insert("CONTAINER ID".to_string(), values[0].to_string()); | ||
| stats.insert("NAME".to_string(), values[1].to_string()); | ||
| stats.insert("CPU%".to_string(), values[2].trim_end_matches('%').to_string()); | ||
| stats.insert("MEM_BYTES".to_string(), convert_to_bytes(values[3]).to_string()); | ||
| stats.insert("MEM_LIMIT_BYTES".to_string(), convert_to_bytes(values[5]).to_string()); | ||
| stats.insert("MEM%".to_string(), values[6].trim_end_matches('%').to_string()); | ||
| stats.insert("NET_IN_BYTES".to_string(), convert_to_bytes(values[7]).to_string()); | ||
| stats.insert("NET_OUT_BYTES".to_string(), convert_to_bytes(values[9]).to_string()); | ||
| stats.insert("BLOCK_IN_BYTES".to_string(), convert_to_bytes(values[10]).to_string()); | ||
| stats.insert("BLOCK_OUT_BYTES".to_string(), convert_to_bytes(values[12]).to_string()); | ||
| stats.insert("PIDS".to_string(), values[13].to_string()); |
There was a problem hiding this comment.
this might be cleaner if the key was &'static str, no calls to .to_string() for keys. I also like to construct maps like HashMap::from_iter([("CONTAINER_ID", values[0].to_string()), ("NAME", values[1].to_string()), ...]); but both suggestions are just a matter of taste.
rust version implementation of procdockerstatsd
Why I did it
rust version implementation of procdockerstatsd to save memory utilitization.
How I did it
How to verify it
memory comparison between python and rs implementation
