Added in Telemetry

Next Build will have opt-in/opt-out capabilities
This commit is contained in:
brotoskyj
2025-11-14 13:41:04 -05:00
parent 0a0f39542d
commit bf5c7d156b
4 changed files with 1219 additions and 277 deletions
+1056 -191
View File
File diff suppressed because it is too large Load Diff
+9 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "airlock_libs"
version = "2.0.0"
version = "3.0.0"
edition = "2024"
[lib]
@@ -10,12 +10,20 @@ crate-type = ["cdylib"]
chrono = "0.4.42"
indicatif = "0.18.2"
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"] }
reqwest = { version = "0.12.24", features = ["json", "native-tls"] }
serde = "1.0.228"
serde-pyobject = "0.8.0"
serde_json = "1.0.145"
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]
generate-abi-stubs = true
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "maturin"
[project]
name = "airlock_libs"
version = "2.0.0"
version = "3.0.0"
description = "Airlock Digital API Wrapper"
readme = "README.md"
license = { text = "AGPL-3.0-only" }
+74 -5
View File
@@ -1,6 +1,12 @@
use chrono::{Duration, Local, NaiveDate};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
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 reqwest::{
Client,
@@ -17,6 +23,7 @@ use std::{
path::PathBuf,
str::FromStr,
};
use tracing_subscriber::prelude::*;
#[derive(Debug, Deserialize, Serialize)]
struct ApiResponse {
@@ -61,6 +68,12 @@ pub fn pull_policy_exec_histories(
exec_types: String,
days: i64,
) -> 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!(
"{}\\cache\\chunkinator.json",
get_base_directory().display()
@@ -93,13 +106,44 @@ pub fn pull_policy_exec_histories(
.unwrap(),
);
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 cutoff = Local::now().naive_local() - Duration::days(days);
let mut f = File::open(&writeable_filepath).unwrap();
tracer.in_span("Airlock Data Retreival", |cx| {
let span = cx.span();
span.set_attribute(Key::new("Days").string(days.to_string().to_string()));
loop {
tracing::info!("Starting Airlock Data Retrieval");
f.seek(SeekFrom::Start(0)).unwrap();
let execution_histories = history_logging(
let execution_histories = tracer.in_span(checkpoint_number.to_string(), |cx| {
let results: ApiResponse = history_logging(
py,
&api,
&exec_types,
@@ -107,11 +151,18 @@ pub fn pull_policy_exec_histories(
&policy_names,
&client,
);
cx.span().set_attribute(KeyValue::new(
"Items in Response",
results.response.exechistories.len().to_string(),
));
results
});
let parsed_responses = execution_histories.response.exechistories;
if parsed_responses.is_empty() {
break;
}
let mut seen: HashMap<(String, String, String), Group> = if writeable_filepath.exists() {
let mut seen: HashMap<(String, String, String), Group> = if writeable_filepath.exists()
{
let mut contents = String::new();
f.read_to_string(&mut contents).unwrap();
let existing_data: ApiResponse =
@@ -183,12 +234,14 @@ pub fn pull_policy_exec_histories(
progress_bar.set_message("Total Percent Complete");
}
}
});
progress_bar.finish_with_message("All Checkpoints Complete");
let return_data = fs::read_to_string(&writeable_filepath).unwrap();
shutdown_tracer_provider();
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_replace = headers.replace('\'', "\"");
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)
.timeout(std::time::Duration::from_secs(300))
.build()
.unwrap()
}
#[tokio::main]
@@ -276,3 +328,20 @@ fn skipback(days: i64) -> ObjectId {
let objectid_hex = format!("{}0000000000000000", hex_timestamp);
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()
}