Skip to content

Commit

Permalink
Adds profiler logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkar598 committed Jul 19, 2023
1 parent 858e560 commit fca1a29
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions dmsrc/influxdb2.dm
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define rustg_influxdb2_publish(data, endpoint, token) RUSTG_CALL(RUST_G, "influxdb2_publish")(data, endpoint, token)
#define rustg_influxdb2_publish_profile(data, endpoint, token, round_id) RUSTG_CALL(RUST_G, "influxdb2_publish_profile")(data, endpoint, token, round_id)
52 changes: 52 additions & 0 deletions src/influxdb2.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::error::Error;
Expand Down Expand Up @@ -57,3 +58,54 @@ byond_fn!(
}))
}
);

#[derive(Serialize, Deserialize)]
struct ProfileProcEntry {
name: String,
#[serde(rename = "self")]
self_: f32,
total: f32,
real: f32,
over: f32,
calls: f32,
}
byond_fn!(
fn influxdb2_publish_profile(data, endpoint, token, round_id) {
let data = data.to_owned();
let endpoint = endpoint.to_owned();
let token = token.to_owned();
let round_id = round_id.to_owned();
Some(jobs::start(move || {
fn handle(data: &str, endpoint: &str, token: &str, round_id: &str) -> Result<RequestPrep, Error> {
let mut lines = vec!();

let data: Vec<ProfileProcEntry> = serde_json::from_str(data)?;
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
.to_string();
for entry in data {
lines.push(concat_string!("profile,proc=", entry.name, " self=", entry.self_.to_string(), ",total=", entry.total.to_string(), ",real=", entry.real.to_string(), ",over=", entry.over.to_string(), ",calls=", entry.calls.to_string(), ",round_id=", round_id.to_string(), " ", timestamp));
}

construct_request(
"post",
endpoint,
lines.join("\n").as_str(),
concat_string!("{\"Authorization\":\"Token ", token ,"\"}").as_str(),
""
)
}

let req = match handle(data.as_str(), endpoint.as_str(), token.as_str(), round_id.as_str()) {
Ok(r) => r,
Err(e) => return e.to_string()
};
match submit_request(req) {
Ok(r) => r,
Err(e) => e.to_string()
}
}))
}
);

0 comments on commit fca1a29

Please sign in to comment.