functional tracing

This commit is contained in:
Ondrej Vlach 2023-11-28 14:29:21 +01:00
parent 0e45cbcb6a
commit f2b16ba472
7 changed files with 105 additions and 24 deletions

5
Cargo.lock generated
View File

@ -1023,6 +1023,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c13b2df4cd59c176099ac82806725ba340c8fa7b1a7004c0912daad30470f63e" checksum = "c13b2df4cd59c176099ac82806725ba340c8fa7b1a7004c0912daad30470f63e"
dependencies = [ dependencies = [
"async-trait",
"chrono", "chrono",
"futures-util", "futures-util",
"opentelemetry", "opentelemetry",
@ -1030,6 +1031,7 @@ dependencies = [
"ordered-float", "ordered-float",
"serde", "serde",
"serde_json", "serde_json",
"thiserror",
] ]
[[package]] [[package]]
@ -1049,7 +1051,10 @@ dependencies = [
"ordered-float", "ordered-float",
"percent-encoding", "percent-encoding",
"rand", "rand",
"serde_json",
"thiserror", "thiserror",
"tokio",
"tokio-stream",
] ]
[[package]] [[package]]

View File

@ -25,8 +25,8 @@ tracing-appender = "0.2.2"
tracing-log = "0.2.0" tracing-log = "0.2.0"
tracing-subscriber = {version="0.3.17", features=["env-filter", "json", "registry", "smallvec"]} tracing-subscriber = {version="0.3.17", features=["env-filter", "json", "registry", "smallvec"]}
tracing-opentelemetry = "0.22.0" tracing-opentelemetry = "0.22.0"
opentelemetry = "0.21" opentelemetry = {version="0.21"}
opentelemetry_sdk = "0.21" opentelemetry_sdk = {version="0.21", features = ["trace", "rt-tokio", "logs"]}
opentelemetry-stdout = { version = "0.2.0", features = ["trace"] } opentelemetry-stdout = { version = "0.2.0", features = ["trace", "logs", "metrics"] }
uuid = {version="1.4.1", features = ["v4"]} uuid = {version="1.4.1", features = ["v4"]}
yansi = "0.5.1" yansi = "0.5.1"

View File

@ -41,7 +41,7 @@ pub struct ChromiumCoordinator {
} }
impl ChromiumCoordinator { impl ChromiumCoordinator {
const NUMBER_OF_INSTANCES: usize = 10; const NUMBER_OF_INSTANCES: usize = 1; // TODO: make this configurable
pub async fn new() -> Self { pub async fn new() -> Self {
let instances: Arc<Mutex<Vec<BrowserHolder>>> = Arc::new(Mutex::new(Vec::with_capacity(Self::NUMBER_OF_INSTANCES))); let instances: Arc<Mutex<Vec<BrowserHolder>>> = Arc::new(Mutex::new(Vec::with_capacity(Self::NUMBER_OF_INSTANCES)));

View File

@ -1,6 +1,8 @@
use chromium::rocket::Chromium; use chromium::rocket::Chromium;
use log::debug;
use rocket::{Rocket, Build, routes}; use rocket::{Rocket, Build, routes};
use tools::rocket::TracingFairing; use tools::rocket::TracingFairing;
use tracing::Level;
use tracing_subscriber::EnvFilter; use tracing_subscriber::EnvFilter;
use tracing_subscriber::prelude::*; use tracing_subscriber::prelude::*;
pub mod routes; pub mod routes;
@ -25,6 +27,24 @@ use tracing_subscriber::registry::LookupSpan;
use uuid::Uuid; use uuid::Uuid;
use yansi::Paint; use yansi::Paint;
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::trace::TracerProvider;
use opentelemetry_stdout as stdout;
use tracing::{error, span};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
use opentelemetry::{
metrics::MeterProvider as _,
trace::{Tracer, TracerProvider as _},
KeyValue,
};
use opentelemetry_sdk::{
metrics::{PeriodicReader, MeterProvider},
runtime,
};
#[derive(PartialEq, Eq, Debug, Clone, Copy)] #[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum LogLevel { pub enum LogLevel {
@ -95,12 +115,8 @@ where
pub fn rocket_builder() -> Rocket<Build> { pub fn rocket_builder() -> Rocket<Build> {
tracing::subscriber::set_global_default( //let tracer_provider = init_trace();
tracing_subscriber::registry()
.with(default_logging_layer())
.with(filter_layer(LogLevel::Debug)),
)
.unwrap();
rocket::build() rocket::build()
.attach(TracingFairing {}) .attach(TracingFairing {})
.attach(Chromium::ignite()) .attach(Chromium::ignite())

View File

@ -2,7 +2,42 @@ use ovlach_pdf::rocket_builder;
use rocket::launch; use rocket::launch;
#[launch] use opentelemetry::trace::TracerProvider as _;
fn rocket() -> _ { use opentelemetry_sdk::trace::TracerProvider;
rocket_builder() use opentelemetry_stdout as stdout;
use tracing::{error, span};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use tracing_subscriber::Registry;
use opentelemetry::{
metrics::MeterProvider as _,
trace::{Tracer, TracerProvider as _},
KeyValue,
};
use opentelemetry_sdk::{
metrics::{PeriodicReader, MeterProvider},
runtime,
};
fn init_meterprovider() -> MeterProvider {
let exporter = opentelemetry_stdout::MetricsExporter::default();
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();
MeterProvider::builder().with_reader(reader).build()
}
#[rocket::main]
async fn main() {
let logger = opentelemetry_stdout::SpanExporter::default();
let provider = TracerProvider::builder()
.with_simple_exporter(logger)
.build();
let telemetry = tracing_opentelemetry::layer().with_tracer(provider.tracer("default"));
let meter_provider = init_meterprovider();
Registry::default().with(telemetry).init();
let _ = rocket_builder().launch().await;
} }

View File

@ -5,7 +5,7 @@ use std::io::prelude::*;
use headless_chrome::Browser; use headless_chrome::Browser;
use headless_chrome::{types::PrintToPdfOptions, LaunchOptions}; use headless_chrome::{types::PrintToPdfOptions, LaunchOptions};
use rocket::{get, Response, futures::Stream, tokio::net::UnixStream, fs::NamedFile}; use rocket::{get, Response, futures::Stream, tokio::net::UnixStream, fs::NamedFile};
use tracing::{info_span, debug, info}; use tracing::{info_span, debug, info, error};
use crate::chromium::rocket::BrowserHolder; use crate::chromium::rocket::BrowserHolder;
use crate::tools::rocket::TracingSpan; use crate::tools::rocket::TracingSpan;
@ -38,10 +38,6 @@ fn generate_pdf(browser: Browser) {
#[get("/cv/<username>/pdf")] #[get("/cv/<username>/pdf")]
pub async fn render_pdf_cv(username: &str, browser: BrowserHolder, tracing: TracingSpan) -> NamedFile { pub async fn render_pdf_cv(username: &str, browser: BrowserHolder, tracing: TracingSpan) -> NamedFile {
let entered_span = tracing.0.enter(); let entered_span = tracing.0.enter();
tracing.0.record(
"username",
username
);
let span = info_span!("render_pdf", username = username); let span = info_span!("render_pdf", username = username);
let entered_inner_span = span.enter(); let entered_inner_span = span.enter();
info!("generate PDF"); info!("generate PDF");

View File

@ -3,8 +3,10 @@
use std::time::SystemTime; use std::time::SystemTime;
use log::debug;
use log::info; use log::info;
use rocket::Build; use rocket::Build;
use rocket::Rocket; use rocket::Rocket;
use rocket::fairing; use rocket::fairing;
@ -19,15 +21,37 @@ use rocket::{
}; };
use uuid::Uuid;
use opentelemetry_sdk::trace::BatchSpanProcessor;
use tracing::Level;
use tracing_subscriber::EnvFilter;
use tracing_subscriber::prelude::*;
use tracing::{info_span, Span}; use tracing::{info_span, Span};
use tracing_log::LogTracer; use tracing_log::LogTracer;
use tracing_subscriber::Layer; use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::Registry;
use tracing_subscriber::{registry::LookupSpan, EnvFilter};
use uuid::Uuid;
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_sdk::trace::TracerProvider;
use opentelemetry_stdout as stdout;
use tracing::{error, span};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::Registry;
use opentelemetry::{
metrics::MeterProvider as _,
trace::{Tracer, TracerProvider as _},
KeyValue,
};
use opentelemetry_sdk::{
metrics::{PeriodicReader, MeterProvider},
runtime,
};
// Test // Test
@ -54,15 +78,20 @@ pub struct TracingSpan<T = Span>(
pub struct TracingFairing; pub struct TracingFairing;
#[rocket::async_trait] #[rocket::async_trait]
impl Fairing for TracingFairing { impl Fairing for TracingFairing {
fn info(&self) -> Info { fn info(&self) -> Info {
Info { Info {
name: "Tracing Fairing", name: "Tracing Fairing",
kind: Kind::Request | Kind::Response, kind: Kind::Request | Kind::Response | Kind::Ignite,
} }
} }
async fn on_ignite(&self, rocket: Rocket<Build>) -> fairing::Result {
Ok(rocket)
}
async fn on_request(&self, req: &mut Request<'_>, _data: &mut Data<'_>) { async fn on_request(&self, req: &mut Request<'_>, _data: &mut Data<'_>) {
let user_agent = req.headers().get_one("User-Agent").unwrap_or(""); let user_agent = req.headers().get_one("User-Agent").unwrap_or("");
let request_id = req let request_id = req
@ -80,7 +109,7 @@ impl Fairing for TracingFairing {
http.uri = %req.uri().path(), http.uri = %req.uri().path(),
http.user_agent=%user_agent, http.user_agent=%user_agent,
http.status_code = tracing::field::Empty, http.status_code = tracing::field::Empty,
http.request_id=%request_id http.request_id=%request_id,
); );
req.local_cache(|| TracingSpan::<Option<Span>>(Some(span))); req.local_cache(|| TracingSpan::<Option<Span>>(Some(span)));