Working open telemetry

This commit is contained in:
Ondrej Vlach 2023-11-28 17:09:50 +01:00
parent f2b16ba472
commit b43aa68130
4 changed files with 302 additions and 26 deletions

259
Cargo.lock generated
View File

@ -124,6 +124,51 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "axum"
version = "0.6.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b829e4e32b91e643de6eafe82b1d90675f5874230191a4ffbc1b336dec4d6bf"
dependencies = [
"async-trait",
"axum-core",
"bitflags 1.3.2",
"bytes",
"futures-util",
"http",
"http-body",
"hyper",
"itoa",
"matchit",
"memchr",
"mime",
"percent-encoding",
"pin-project-lite",
"rustversion",
"serde",
"sync_wrapper",
"tower",
"tower-layer",
"tower-service",
]
[[package]]
name = "axum-core"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "759fa577a247914fd3f7f76d62972792636412fbfd634cd452f6a385a74d2d2c"
dependencies = [
"async-trait",
"bytes",
"futures-util",
"http",
"http-body",
"mime",
"rustversion",
"tower-layer",
"tower-service",
]
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.69" version = "0.3.69"
@ -656,13 +701,19 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-util", "futures-util",
"http", "http",
"indexmap", "indexmap 2.1.0",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
"tracing", "tracing",
] ]
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.3" version = "0.14.3"
@ -765,6 +816,18 @@ dependencies = [
"want", "want",
] ]
[[package]]
name = "hyper-timeout"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbb958482e8c7be4bc3cf272a766a2b0bf1a6755e7a6ae777f017a31d11b13b1"
dependencies = [
"hyper",
"pin-project-lite",
"tokio",
"tokio-io-timeout",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.58" version = "0.1.58"
@ -804,6 +867,16 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "indexmap"
version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [
"autocfg",
"hashbrown 0.12.3",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "2.1.0" version = "2.1.0"
@ -811,7 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
dependencies = [ dependencies = [
"equivalent", "equivalent",
"hashbrown", "hashbrown 0.14.3",
"serde", "serde",
] ]
@ -832,6 +905,15 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@ -905,6 +987,12 @@ dependencies = [
"regex-automata 0.1.10", "regex-automata 0.1.10",
] ]
[[package]]
name = "matchit"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e7465ac9959cc2b1404e8e2367b43684a6d13790fe23056cc8c6c5a6b7bcb94"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.6.4"
@ -1009,7 +1097,7 @@ checksum = "1e32339a5dc40459130b3bd269e9892439f55b33e772d2a9d402a789baaf4e8a"
dependencies = [ dependencies = [
"futures-core", "futures-core",
"futures-sink", "futures-sink",
"indexmap", "indexmap 2.1.0",
"js-sys", "js-sys",
"once_cell", "once_cell",
"pin-project-lite", "pin-project-lite",
@ -1017,6 +1105,46 @@ dependencies = [
"urlencoding", "urlencoding",
] ]
[[package]]
name = "opentelemetry-otlp"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f24cda83b20ed2433c68241f918d0f6fdec8b1d43b7a9590ab4420c5095ca930"
dependencies = [
"async-trait",
"futures-core",
"http",
"opentelemetry",
"opentelemetry-proto",
"opentelemetry-semantic-conventions",
"opentelemetry_sdk",
"prost",
"thiserror",
"tokio",
"tonic",
]
[[package]]
name = "opentelemetry-proto"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2e155ce5cc812ea3d1dffbd1539aed653de4bf4882d60e6e04dcf0901d674e1"
dependencies = [
"opentelemetry",
"opentelemetry_sdk",
"prost",
"tonic",
]
[[package]]
name = "opentelemetry-semantic-conventions"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f5774f1ef1f982ef2a447f6ee04ec383981a3ab99c8e77a1a7b30182e65bbc84"
dependencies = [
"opentelemetry",
]
[[package]] [[package]]
name = "opentelemetry-stdout" name = "opentelemetry-stdout"
version = "0.2.0" version = "0.2.0"
@ -1092,6 +1220,8 @@ dependencies = [
"headless_chrome", "headless_chrome",
"log", "log",
"opentelemetry", "opentelemetry",
"opentelemetry-otlp",
"opentelemetry-semantic-conventions",
"opentelemetry-stdout", "opentelemetry-stdout",
"opentelemetry_sdk", "opentelemetry_sdk",
"ovlach_data", "ovlach_data",
@ -1160,6 +1290,26 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e"
[[package]]
name = "pin-project"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422"
dependencies = [
"pin-project-internal",
]
[[package]]
name = "pin-project-internal"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.13" version = "0.2.13"
@ -1206,6 +1356,29 @@ dependencies = [
"yansi 1.0.0-rc.1", "yansi 1.0.0-rc.1",
] ]
[[package]]
name = "prost"
version = "0.11.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-derive"
version = "0.11.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.33" version = "1.0.33"
@ -1346,7 +1519,7 @@ dependencies = [
"either", "either",
"figment", "figment",
"futures", "futures",
"indexmap", "indexmap 2.1.0",
"log", "log",
"memchr", "memchr",
"multer", "multer",
@ -1378,7 +1551,7 @@ checksum = "a2238066abf75f21be6cd7dc1a09d5414a671f4246e384e49fe3f8a4936bd04c"
dependencies = [ dependencies = [
"devise", "devise",
"glob", "glob",
"indexmap", "indexmap 2.1.0",
"proc-macro2", "proc-macro2",
"quote", "quote",
"rocket_http", "rocket_http",
@ -1398,7 +1571,7 @@ dependencies = [
"futures", "futures",
"http", "http",
"hyper", "hyper",
"indexmap", "indexmap 2.1.0",
"log", "log",
"memchr", "memchr",
"pear", "pear",
@ -1535,7 +1708,7 @@ version = "0.9.27"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c" checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c"
dependencies = [ dependencies = [
"indexmap", "indexmap 2.1.0",
"itoa", "itoa",
"ryu", "ryu",
"serde", "serde",
@ -1669,6 +1842,12 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sync_wrapper"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160"
[[package]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.8.1" version = "3.8.1"
@ -1774,6 +1953,16 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "tokio-io-timeout"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b74022ada614a1b4834de765f9bb43877f910cc8ce4be40e89042c9223a8bf"
dependencies = [
"pin-project-lite",
"tokio",
]
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "2.2.0" version = "2.2.0"
@ -1837,13 +2026,67 @@ version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03" checksum = "d34d383cd00a163b4a5b85053df514d45bc330f6de7737edfe0a93311d1eaa03"
dependencies = [ dependencies = [
"indexmap", "indexmap 2.1.0",
"serde", "serde",
"serde_spanned", "serde_spanned",
"toml_datetime", "toml_datetime",
"winnow", "winnow",
] ]
[[package]]
name = "tonic"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a"
dependencies = [
"async-trait",
"axum",
"base64",
"bytes",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"hyper",
"hyper-timeout",
"percent-encoding",
"pin-project",
"prost",
"tokio",
"tokio-stream",
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c"
dependencies = [
"futures-core",
"futures-util",
"indexmap 1.9.3",
"pin-project",
"pin-project-lite",
"rand",
"slab",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c20c8dbed6283a09604c3e69b4b7eeb54e298b8a600d4d5ecb5ad39de609f1d0"
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.2" version = "0.3.2"

View File

@ -20,7 +20,7 @@ tokio = { version = "1.34.0", features = ["macros"] }
tracing = "0.1.37" tracing = { version = "0.1.40", features = ["attributes", "std"] }
tracing-appender = "0.2.2" 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"]}
@ -28,5 +28,7 @@ tracing-opentelemetry = "0.22.0"
opentelemetry = {version="0.21"} opentelemetry = {version="0.21"}
opentelemetry_sdk = {version="0.21", features = ["trace", "rt-tokio", "logs"]} opentelemetry_sdk = {version="0.21", features = ["trace", "rt-tokio", "logs"]}
opentelemetry-stdout = { version = "0.2.0", features = ["trace", "logs", "metrics"] } opentelemetry-stdout = { version = "0.2.0", features = ["trace", "logs", "metrics"] }
opentelemetry-otlp = { version="*", features = ["trace", "logs", "metrics"] }
opentelemetry-semantic-conventions = "0.13.0"
uuid = {version="1.4.1", features = ["v4"]} uuid = {version="1.4.1", features = ["v4"]}
yansi = "0.5.1" yansi = "0.5.1"

View File

@ -1,10 +1,13 @@
use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::Resource;
use ovlach_pdf::rocket_builder; use ovlach_pdf::rocket_builder;
use rocket::launch; use rocket::launch;
use opentelemetry::trace::TracerProvider as _; use opentelemetry::trace::{TracerProvider as _, TraceError};
use opentelemetry_sdk::trace::TracerProvider; use opentelemetry_sdk::trace::{TracerProvider, Config};
use opentelemetry_stdout as stdout; use opentelemetry_stdout as stdout;
use tokio::net::unix::pipe;
use tracing::{error, span}; use tracing::{error, span};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use tracing_subscriber::Registry; use tracing_subscriber::Registry;
@ -28,6 +31,23 @@ fn init_meterprovider() -> MeterProvider {
} }
fn init_tracer() -> Result<opentelemetry_sdk::trace::Tracer, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(
opentelemetry_otlp::new_exporter()
.tonic()
.with_endpoint("http://localhost:4317"),
)
.with_trace_config(
opentelemetry_sdk::trace::config().with_resource(Resource::new(vec![KeyValue::new(
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
"ovlach_pdf",
)])),
)
.install_batch(runtime::Tokio)
//.install_simple()
}
#[rocket::main] #[rocket::main]
async fn main() { async fn main() {
@ -36,8 +56,12 @@ async fn main() {
.with_simple_exporter(logger) .with_simple_exporter(logger)
.build(); .build();
let telemetry = tracing_opentelemetry::layer().with_tracer(provider.tracer("default")); let telemetry = tracing_opentelemetry::layer().with_tracer(
let meter_provider = init_meterprovider(); provider.tracer("default")
).with_tracer(
init_tracer().unwrap()
);
let meter_provider = init_meterprovider();
Registry::default().with(telemetry).init(); Registry::default().with(telemetry).init();
let _ = rocket_builder().launch().await; let _ = rocket_builder().launch().await;
} }

View File

@ -5,14 +5,17 @@ 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, error}; use tracing::{info_span, debug, info, error, Span, instrument};
use crate::chromium::rocket::BrowserHolder; use crate::chromium::rocket::BrowserHolder;
use crate::tools::rocket::TracingSpan; use crate::tools::rocket::TracingSpan;
fn generate_pdf(browser: Browser) { fn generate_pdf(browser: Browser) {
let tab = browser.new_tab().unwrap(); let tab = browser.new_tab().unwrap();
let tab = tab.navigate_to("file:///home/6a6996c0-1609-48b6-8ca6-affbef1b4d1d/Devel/Nanobyte/ovlach/ovlach_pdf/template.html").unwrap().wait_until_navigated().unwrap(); info_span!("open_pdf").in_scope(|| {
tab.navigate_to("file:///home/6a6996c0-1609-48b6-8ca6-affbef1b4d1d/Devel/Nanobyte/ovlach/ovlach_pdf/template.html").unwrap().wait_until_navigated().unwrap();
});
let options = PrintToPdfOptions{ let options = PrintToPdfOptions{
margin_bottom: Some(0.0), margin_bottom: Some(0.0),
margin_left: Some(0.0), margin_left: Some(0.0),
@ -26,25 +29,29 @@ fn generate_pdf(browser: Browser) {
//thread::sleep(Duration::from_secs(10)); //thread::sleep(Duration::from_secs(10));
let bytes = tab.print_to_pdf(Some(options)).unwrap(); let bytes = info_span!("print_pdf").in_scope(|| {
let mut file = fs::OpenOptions::new() tab.print_to_pdf(Some(options)).unwrap()
// .create(true) // To create a new file });
.write(true)
// either use the ? operator or unwrap since it returns a Result info_span!("write_temporary_file").in_scope(|| {
.open("/tmp/foo.pdf").unwrap(); let mut file = fs::OpenOptions::new()
file.write_all(&bytes).unwrap(); // .create(true) // To create a new file
.write(true)
// either use the ? operator or unwrap since it returns a Result
.open("/tmp/foo.pdf").unwrap();
file.write_all(&bytes).unwrap();
});
} }
#[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();
let span = info_span!("render_pdf", username = username); let span = info_span!("render_pdf", username = username);
let entered_inner_span = span.enter();
info!("generate PDF"); info!("generate PDF");
generate_pdf(browser.browser); span.in_scope(||{
generate_pdf(browser.browser);
});
info!("done generating"); info!("done generating");
drop(entered_inner_span);
drop(entered_span); drop(entered_span);
"foo!".to_string();
NamedFile::open("/tmp/foo.pdf").await.expect("failed to open foo.pdf") NamedFile::open("/tmp/foo.pdf").await.expect("failed to open foo.pdf")
} }