Added in Telemetry
Next Build will have opt-in/opt-out capabilities
This commit is contained in:
Generated
+1056
-191
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "airlock_libs"
|
name = "airlock_libs"
|
||||||
version = "2.0.0"
|
version = "3.0.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
@@ -10,12 +10,20 @@ crate-type = ["cdylib"]
|
|||||||
chrono = "0.4.42"
|
chrono = "0.4.42"
|
||||||
indicatif = "0.18.2"
|
indicatif = "0.18.2"
|
||||||
mongodb = "3.3.0"
|
mongodb = "3.3.0"
|
||||||
|
opentelemetry = { version = "0.18.0", features = ["rt-tokio", "metrics", "trace"] }
|
||||||
|
opentelemetry-otlp = { version = "0.11.0", features = ["trace", "metrics"] }
|
||||||
|
opentelemetry-semantic-conventions = { version = "0.10.0" }
|
||||||
|
opentelemetry-proto = { version = "0.1.0"}
|
||||||
pyo3 = { version = "0.27.0", features = ["extension-module", "generate-import-lib"] }
|
pyo3 = { version = "0.27.0", features = ["extension-module", "generate-import-lib"] }
|
||||||
reqwest = { version = "0.12.24", features = ["json", "native-tls"] }
|
reqwest = { version = "0.12.24", features = ["json", "native-tls"] }
|
||||||
serde = "1.0.228"
|
serde = "1.0.228"
|
||||||
serde-pyobject = "0.8.0"
|
serde-pyobject = "0.8.0"
|
||||||
serde_json = "1.0.145"
|
serde_json = "1.0.145"
|
||||||
tokio = { version = "1.48.0", features = ["full"] }
|
tokio = { version = "1.48.0", features = ["full"] }
|
||||||
|
tonic = { version = "0.8.2", features = ["tls-roots"] }
|
||||||
|
tracing = "0.1.41"
|
||||||
|
tracing-subscriber = "0.3.20"
|
||||||
|
tracing-opentelemetry = "0.32.0"
|
||||||
|
|
||||||
[package.metadata.maturin]
|
[package.metadata.maturin]
|
||||||
generate-abi-stubs = true
|
generate-abi-stubs = true
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ build-backend = "maturin"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "airlock_libs"
|
name = "airlock_libs"
|
||||||
version = "2.0.0"
|
version = "3.0.0"
|
||||||
description = "Airlock Digital API Wrapper"
|
description = "Airlock Digital API Wrapper"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = { text = "AGPL-3.0-only" }
|
license = { text = "AGPL-3.0-only" }
|
||||||
|
|||||||
+153
-84
@@ -1,6 +1,12 @@
|
|||||||
use chrono::{Duration, Local, NaiveDate};
|
use chrono::{Duration, Local, NaiveDate};
|
||||||
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
|
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
|
||||||
use mongodb::bson::oid::ObjectId;
|
use mongodb::bson::oid::ObjectId;
|
||||||
|
use opentelemetry::global::shutdown_tracer_provider;
|
||||||
|
use opentelemetry::sdk::Resource;
|
||||||
|
use opentelemetry::trace::{Status, TraceContextExt, TraceError};
|
||||||
|
use opentelemetry::{Context, KeyValue, sdk::trace as sdktrace, trace::Tracer};
|
||||||
|
use opentelemetry::{Key, global};
|
||||||
|
use opentelemetry_otlp::WithExportConfig;
|
||||||
use pyo3::{prelude::*, types::PyString};
|
use pyo3::{prelude::*, types::PyString};
|
||||||
use reqwest::{
|
use reqwest::{
|
||||||
Client,
|
Client,
|
||||||
@@ -17,6 +23,7 @@ use std::{
|
|||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
use tracing_subscriber::prelude::*;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
struct ApiResponse {
|
struct ApiResponse {
|
||||||
@@ -61,6 +68,12 @@ pub fn pull_policy_exec_histories(
|
|||||||
exec_types: String,
|
exec_types: String,
|
||||||
days: i64,
|
days: i64,
|
||||||
) -> Py<PyString> {
|
) -> Py<PyString> {
|
||||||
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
|
rt.block_on(async {
|
||||||
|
let _ = init_tracer();
|
||||||
|
});
|
||||||
|
let tracer = global::tracer("global_tracer");
|
||||||
|
let _cx = Context::new();
|
||||||
let file_path: PathBuf = format!(
|
let file_path: PathBuf = format!(
|
||||||
"{}\\cache\\chunkinator.json",
|
"{}\\cache\\chunkinator.json",
|
||||||
get_base_directory().display()
|
get_base_directory().display()
|
||||||
@@ -93,102 +106,142 @@ pub fn pull_policy_exec_histories(
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
);
|
);
|
||||||
progress_bar.enable_steady_tick(std::time::Duration::from_millis(100));
|
progress_bar.enable_steady_tick(std::time::Duration::from_millis(100));
|
||||||
let client = build_client(py, &py_self);
|
let client = tracer.in_span("Building HTTP Client", |cx| {
|
||||||
|
let client_result = build_client(py, &py_self);
|
||||||
|
match client_result {
|
||||||
|
Ok(client_result) => {
|
||||||
|
cx.span().add_event(
|
||||||
|
"info",
|
||||||
|
vec![KeyValue::new(
|
||||||
|
"Client Built Successfully",
|
||||||
|
format!("{:?}", client_result),
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
client_result
|
||||||
|
}
|
||||||
|
Err(client_result) => {
|
||||||
|
cx.span().add_event(
|
||||||
|
"warn",
|
||||||
|
vec![KeyValue::new(
|
||||||
|
"Client Failed to Build",
|
||||||
|
format!("{:?}", &client_result),
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
cx.span()
|
||||||
|
.set_status(Status::error("Client Failed to Build"));
|
||||||
|
panic!("Failed to Build Client: {:?}", client_result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
let api: Py<PyAny> = py_self;
|
let api: Py<PyAny> = py_self;
|
||||||
let cutoff = Local::now().naive_local() - Duration::days(days);
|
let cutoff = Local::now().naive_local() - Duration::days(days);
|
||||||
let mut f = File::open(&writeable_filepath).unwrap();
|
let mut f = File::open(&writeable_filepath).unwrap();
|
||||||
loop {
|
tracer.in_span("Airlock Data Retreival", |cx| {
|
||||||
f.seek(SeekFrom::Start(0)).unwrap();
|
let span = cx.span();
|
||||||
let execution_histories = history_logging(
|
span.set_attribute(Key::new("Days").string(days.to_string().to_string()));
|
||||||
py,
|
loop {
|
||||||
&api,
|
tracing::info!("Starting Airlock Data Retrieval");
|
||||||
&exec_types,
|
f.seek(SeekFrom::Start(0)).unwrap();
|
||||||
&checkpoint_number,
|
let execution_histories = tracer.in_span(checkpoint_number.to_string(), |cx| {
|
||||||
&policy_names,
|
let results: ApiResponse = history_logging(
|
||||||
&client,
|
py,
|
||||||
);
|
&api,
|
||||||
let parsed_responses = execution_histories.response.exechistories;
|
&exec_types,
|
||||||
if parsed_responses.is_empty() {
|
&checkpoint_number,
|
||||||
break;
|
&policy_names,
|
||||||
}
|
&client,
|
||||||
let mut seen: HashMap<(String, String, String), Group> = if writeable_filepath.exists() {
|
);
|
||||||
let mut contents = String::new();
|
cx.span().set_attribute(KeyValue::new(
|
||||||
f.read_to_string(&mut contents).unwrap();
|
"Items in Response",
|
||||||
let existing_data: ApiResponse =
|
results.response.exechistories.len().to_string(),
|
||||||
serde_json::from_str(&contents).unwrap_or(ApiResponse {
|
));
|
||||||
error: "Success".to_string(),
|
results
|
||||||
response: ExecHistories {
|
});
|
||||||
exechistories: vec![],
|
let parsed_responses = execution_histories.response.exechistories;
|
||||||
},
|
if parsed_responses.is_empty() {
|
||||||
});
|
|
||||||
existing_data
|
|
||||||
.response
|
|
||||||
.exechistories
|
|
||||||
.into_iter()
|
|
||||||
.map(|entry| {
|
|
||||||
(
|
|
||||||
(
|
|
||||||
entry.sha256.clone(),
|
|
||||||
entry.filename.clone(),
|
|
||||||
entry.hostname.clone(),
|
|
||||||
),
|
|
||||||
entry,
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
} else {
|
|
||||||
HashMap::new()
|
|
||||||
};
|
|
||||||
for (index, executions) in parsed_responses.iter().enumerate() {
|
|
||||||
if executions.checkpoint.is_empty() || executions.datetime.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if index == parsed_responses.len() - 1 {
|
|
||||||
checkpoint_number = executions.checkpoint.clone();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let history_date = match NaiveDate::parse_from_str(
|
let mut seen: HashMap<(String, String, String), Group> = if writeable_filepath.exists()
|
||||||
&executions.datetime.replace(" +0000 UTC", ""),
|
{
|
||||||
"%Y-%m-%dT%H:%M:%SZ",
|
let mut contents = String::new();
|
||||||
) {
|
f.read_to_string(&mut contents).unwrap();
|
||||||
Ok(date) => date,
|
let existing_data: ApiResponse =
|
||||||
Err(_) => continue,
|
serde_json::from_str(&contents).unwrap_or(ApiResponse {
|
||||||
|
error: "Success".to_string(),
|
||||||
|
response: ExecHistories {
|
||||||
|
exechistories: vec![],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
existing_data
|
||||||
|
.response
|
||||||
|
.exechistories
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| {
|
||||||
|
(
|
||||||
|
(
|
||||||
|
entry.sha256.clone(),
|
||||||
|
entry.filename.clone(),
|
||||||
|
entry.hostname.clone(),
|
||||||
|
),
|
||||||
|
entry,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
} else {
|
||||||
|
HashMap::new()
|
||||||
};
|
};
|
||||||
if history_date >= cutoff.into() {
|
for (index, executions) in parsed_responses.iter().enumerate() {
|
||||||
let key = (
|
if executions.checkpoint.is_empty() || executions.datetime.is_empty() {
|
||||||
executions.sha256.clone(),
|
continue;
|
||||||
executions.filename.clone(),
|
}
|
||||||
executions.hostname.clone(),
|
if index == parsed_responses.len() - 1 {
|
||||||
);
|
checkpoint_number = executions.checkpoint.clone();
|
||||||
seen.entry(key).or_insert(executions.clone());
|
break;
|
||||||
|
}
|
||||||
|
let history_date = match NaiveDate::parse_from_str(
|
||||||
|
&executions.datetime.replace(" +0000 UTC", ""),
|
||||||
|
"%Y-%m-%dT%H:%M:%SZ",
|
||||||
|
) {
|
||||||
|
Ok(date) => date,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
if history_date >= cutoff.into() {
|
||||||
|
let key = (
|
||||||
|
executions.sha256.clone(),
|
||||||
|
executions.filename.clone(),
|
||||||
|
executions.hostname.clone(),
|
||||||
|
);
|
||||||
|
seen.entry(key).or_insert(executions.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let final_response = ApiResponse {
|
||||||
|
error: "Success".to_string(),
|
||||||
|
response: ExecHistories {
|
||||||
|
exechistories: seen.values().cloned().collect(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let data_write = serde_json::to_string_pretty(&final_response).unwrap();
|
||||||
|
fs::write(&writeable_filepath, data_write).unwrap();
|
||||||
|
if let Some(last_item) = &final_response.response.exechistories.last()
|
||||||
|
&& let Ok(last_date) = NaiveDate::parse_from_str(
|
||||||
|
&last_item.datetime.replace(" +0000 UTC", ""),
|
||||||
|
"%Y-%m-%dT%H:%M:%SZ",
|
||||||
|
)
|
||||||
|
{
|
||||||
|
let date_diff = Local::now().naive_local().date() - last_date;
|
||||||
|
let percentage_diff = (days - date_diff.num_days()) as f64 / days as f64 * 100.0;
|
||||||
|
progress_bar.set_position(percentage_diff.round() as u64);
|
||||||
|
progress_bar.set_message("Total Percent Complete");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let final_response = ApiResponse {
|
});
|
||||||
error: "Success".to_string(),
|
|
||||||
response: ExecHistories {
|
|
||||||
exechistories: seen.values().cloned().collect(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
let data_write = serde_json::to_string_pretty(&final_response).unwrap();
|
|
||||||
fs::write(&writeable_filepath, data_write).unwrap();
|
|
||||||
if let Some(last_item) = &final_response.response.exechistories.last()
|
|
||||||
&& let Ok(last_date) = NaiveDate::parse_from_str(
|
|
||||||
&last_item.datetime.replace(" +0000 UTC", ""),
|
|
||||||
"%Y-%m-%dT%H:%M:%SZ",
|
|
||||||
)
|
|
||||||
{
|
|
||||||
let date_diff = Local::now().naive_local().date() - last_date;
|
|
||||||
let percentage_diff = (days - date_diff.num_days()) as f64 / days as f64 * 100.0;
|
|
||||||
progress_bar.set_position(percentage_diff.round() as u64);
|
|
||||||
progress_bar.set_message("Total Percent Complete");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
progress_bar.finish_with_message("All Checkpoints Complete");
|
progress_bar.finish_with_message("All Checkpoints Complete");
|
||||||
let return_data = fs::read_to_string(&writeable_filepath).unwrap();
|
let return_data = fs::read_to_string(&writeable_filepath).unwrap();
|
||||||
|
shutdown_tracer_provider();
|
||||||
PyString::new(py, &return_data).into()
|
PyString::new(py, &return_data).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_client(py: Python<'_>, py_self: &Py<PyAny>) -> Client {
|
fn build_client(py: Python<'_>, py_self: &Py<PyAny>) -> Result<reqwest::Client, reqwest::Error> {
|
||||||
let headers = py_self.getattr(py, "headers").unwrap().to_string();
|
let headers = py_self.getattr(py, "headers").unwrap().to_string();
|
||||||
let headers_replace = headers.replace('\'', "\"");
|
let headers_replace = headers.replace('\'', "\"");
|
||||||
let parsed: Value = serde_json::from_str(headers_replace.as_str()).unwrap();
|
let parsed: Value = serde_json::from_str(headers_replace.as_str()).unwrap();
|
||||||
@@ -207,7 +260,6 @@ fn build_client(py: Python<'_>, py_self: &Py<PyAny>) -> Client {
|
|||||||
.default_headers(header_map)
|
.default_headers(header_map)
|
||||||
.timeout(std::time::Duration::from_secs(300))
|
.timeout(std::time::Duration::from_secs(300))
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -276,3 +328,20 @@ fn skipback(days: i64) -> ObjectId {
|
|||||||
let objectid_hex = format!("{}0000000000000000", hex_timestamp);
|
let objectid_hex = format!("{}0000000000000000", hex_timestamp);
|
||||||
ObjectId::parse_str(&objectid_hex).expect("Invalid ObjectId hex")
|
ObjectId::parse_str(&objectid_hex).expect("Invalid ObjectId hex")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
|
||||||
|
opentelemetry_otlp::new_pipeline()
|
||||||
|
.tracing()
|
||||||
|
.with_exporter(
|
||||||
|
opentelemetry_otlp::new_exporter()
|
||||||
|
.tonic()
|
||||||
|
.with_endpoint("https://signoz.racooncity.org"),
|
||||||
|
)
|
||||||
|
.with_trace_config(
|
||||||
|
sdktrace::config().with_resource(Resource::new(vec![KeyValue::new(
|
||||||
|
"service.name",
|
||||||
|
"LoxideLibs",
|
||||||
|
)])),
|
||||||
|
)
|
||||||
|
.install_simple()
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user