From 6fd36936a3cf4ca66dbd9286cbbd3ebbf3fbe86b Mon Sep 17 00:00:00 2001 From: Dietrich Date: Mon, 12 Apr 2021 16:32:59 +0200 Subject: [PATCH] Enable jaeger + opentracing logging --- Cargo.lock | 74 ++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 6 ++-- src/bin/pslink/main.rs | 25 +++++++++----- src/bin/pslink/views.rs | 28 +++++++++++++++- 4 files changed, 121 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e18936c..1825e85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1713,6 +1713,12 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "integer-encoding" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48dc51180a9b377fd75814d0cc02199c20f8e99433d6762f650d39cdbbd3b56f" + [[package]] name = "intl-memoizer" version = "0.5.1" @@ -2119,6 +2125,44 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "opentelemetry" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91cea1dfd50064e52db033179952d18c770cbc5dfefc8eba45d619357ba3914" +dependencies = [ + "async-trait", + "futures 0.3.14", + "js-sys", + "lazy_static", + "percent-encoding", + "pin-project 1.0.6", + "rand 0.8.3", + "thiserror", +] + +[[package]] +name = "opentelemetry-jaeger" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd4984441954f9ebbe3eebdfc6fd4fa95be6400d403171228779b949f3cd918" +dependencies = [ + "async-trait", + "lazy_static", + "opentelemetry", + "thiserror", + "thrift", +] + +[[package]] +name = "ordered-float" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3305af35278dd29f46fcdd139e0b1fbfae2153f0e5928b39b035542dd31e37b7" +dependencies = [ + "num-traits", +] + [[package]] name = "ouroboros" version = "0.8.3" @@ -2400,6 +2444,8 @@ dependencies = [ "fluent-langneg", "fluent-templates", "image", + "opentelemetry", + "opentelemetry-jaeger", "qrcode", "rand 0.8.3", "rpassword", @@ -2410,7 +2456,7 @@ dependencies = [ "tracing", "tracing-actix-web", "tracing-bunyan-formatter", - "tracing-log", + "tracing-opentelemetry", "tracing-subscriber", ] @@ -3372,6 +3418,19 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "thrift" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c6d965454947cc7266d22716ebfd07b18d84ebaf35eec558586bbb2a8cb6b5b" +dependencies = [ + "byteorder", + "integer-encoding", + "log", + "ordered-float", + "threadpool", +] + [[package]] name = "tiff" version = "0.6.1" @@ -3595,6 +3654,19 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99003208b647dae59dcefc49c98aecaa3512fbc29351685d4b9ef23a9218458e" +dependencies = [ + "opentelemetry", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", +] + [[package]] name = "tracing-serde" version = "0.1.2" diff --git a/Cargo.toml b/Cargo.toml index f93dbee..fe55603 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,9 +27,11 @@ chrono = { version = "0.4", features = ["serde"] } argonautica = "0.2" tracing = { version = "0.1", features = ["log"] } tracing-bunyan-formatter = "0.2.0" -tracing-subscriber = { version = "0.2.12", features = ["registry", "env-filter"] } -tracing-log = "0.1" +tracing-subscriber = { version = "0.2.17", features = ["registry", "env-filter"] } tracing-actix-web = "0.2.1" +tracing-opentelemetry = "0.12" +opentelemetry = "0.13" +opentelemetry-jaeger="0.12" qrcode = "0.12" image = "0.23" rand="0.8" diff --git a/src/bin/pslink/main.rs b/src/bin/pslink/main.rs index ff771f6..f47d891 100644 --- a/src/bin/pslink/main.rs +++ b/src/bin/pslink/main.rs @@ -15,18 +15,29 @@ use tracing::instrument; use tracing::{error, info, trace}; use tracing::{subscriber::set_global_default, Subscriber}; use tracing_actix_web::TracingLogger; -use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; -use tracing_log::LogTracer; +use tracing_opentelemetry::OpenTelemetryLayer; use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry}; /// Compose multiple layers into a `tracing`'s subscriber. -pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Send + Sync { +#[must_use] +pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + Sync { let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(env_filter)); - let formatting_layer = BunyanFormattingLayer::new(name, std::io::stdout); + // Create a jaeger exporter pipeline for a `trace_demo` service. + let tracer = opentelemetry_jaeger::new_pipeline() + .with_service_name(name) + .install_simple() + .expect("Error initializing Jaeger exporter"); + let formatting_layer = tracing_subscriber::fmt::layer().with_target(false); + + // Create a layer with the configured tracer + let otel_layer = OpenTelemetryLayer::new(tracer); + + // Use the tracing subscriber `Registry`, or any other subscriber + // that impls `LookupSpan` Registry::default() + .with(otel_layer) .with(env_filter) - .with(JsonStorageLayer) .with(formatting_layer) } @@ -34,7 +45,6 @@ pub fn get_subscriber(name: String, env_filter: String) -> impl Subscriber + Sen /// /// It should only be called once! pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) { - LogTracer::init().expect("Failed to set logger"); set_global_default(subscriber).expect("Failed to set subscriber"); } @@ -50,7 +60,6 @@ static_loader! { #[instrument] fn build_tera() -> Result { let mut tera = Tera::default(); - tracing::info!("Tracing activated!"); // Add translation support tera.register_function("fluent", FluentLoader::new(&*LOCALES)); @@ -209,7 +218,7 @@ async fn webservice(server_config: ServerConfig) -> Result<()> { #[instrument] #[actix_web::main] async fn main() -> std::result::Result<(), ServerError> { - let subscriber = get_subscriber("app".into(), "info".into()); + let subscriber = get_subscriber("fhs.li", "info"); init_subscriber(subscriber); match cli::setup().await { diff --git a/src/bin/pslink/views.rs b/src/bin/pslink/views.rs index 10a8969..5695010 100644 --- a/src/bin/pslink/views.rs +++ b/src/bin/pslink/views.rs @@ -15,13 +15,14 @@ use image::{DynamicImage, ImageOutputFormat, Luma}; use qrcode::{render::svg, QrCode}; use queries::{authenticate, Role}; use tera::{Context, Tera}; -use tracing::{info, trace, warn}; +use tracing::{info, instrument, trace, warn}; use pslink::forms::LinkForm; use pslink::models::{LoginUser, NewUser}; use pslink::queries; use pslink::ServerError; +#[instrument] fn redirect_builder(target: &str) -> HttpResponse { HttpResponse::SeeOther() .set(CacheControl(vec![ @@ -34,6 +35,7 @@ fn redirect_builder(target: &str) -> HttpResponse { .body(format!("Redirect to {}", target)) } +#[instrument] fn detect_language(request: &HttpRequest) -> Result { let requested = parse_accepted_languages( request @@ -63,6 +65,8 @@ fn detect_language(request: &HttpRequest) -> Result { } /// Show the list of all available links if a user is authenticated + +#[instrument(skip(id, tera))] pub async fn index( tera: web::Data, config: web::Data, @@ -81,6 +85,7 @@ pub async fn index( } /// Show the list of all available links if a user is authenticated +#[instrument(skip(id, tera))] pub async fn index_users( tera: web::Data, config: web::Data, @@ -98,6 +103,8 @@ pub async fn index_users( Ok(redirect_builder("/admin/login")) } } + +#[instrument(skip(id, tera))] pub async fn view_link_empty( tera: web::Data, config: web::Data, @@ -106,6 +113,7 @@ pub async fn view_link_empty( view_link(tera, config, id, web::Path::from("".to_owned())).await } +#[instrument(skip(id, tera))] pub async fn view_link( tera: web::Data, config: web::Data, @@ -145,6 +153,7 @@ pub async fn view_link( } } +#[instrument(skip(id, tera))] pub async fn view_profile( tera: web::Data, config: web::Data, @@ -172,6 +181,7 @@ pub async fn view_profile( } } +#[instrument(skip(id, tera))] pub async fn edit_profile( tera: web::Data, config: web::Data, @@ -198,6 +208,7 @@ pub async fn edit_profile( } } +#[instrument(skip(id))] pub async fn process_edit_profile( data: web::Form, config: web::Data, @@ -211,6 +222,7 @@ pub async fn process_edit_profile( ))) } +#[instrument(skip(id))] pub async fn download_png( id: Identity, config: web::Data, @@ -235,6 +247,7 @@ pub async fn download_png( } } +#[instrument(skip(id, tera))] pub async fn signup( tera: web::Data, config: web::Data, @@ -255,6 +268,7 @@ pub async fn signup( } } +#[instrument(skip(id))] pub async fn process_signup( data: web::Form, config: web::Data, @@ -269,6 +283,7 @@ pub async fn process_signup( } } +#[instrument(skip(id))] pub async fn toggle_admin( data: web::Path, config: web::Data, @@ -281,6 +296,7 @@ pub async fn toggle_admin( ))) } +#[instrument(skip(id))] pub async fn set_language( data: web::Path, config: web::Data, @@ -290,6 +306,7 @@ pub async fn set_language( Ok(redirect_builder("/admin/index/")) } +#[instrument(skip(id))] pub async fn login( tera: web::Data, id: Identity, @@ -323,6 +340,7 @@ pub async fn login( Ok(HttpResponse::Ok().body(rendered)) } +#[instrument(skip(id))] pub async fn process_login( data: web::Form, config: web::Data, @@ -355,11 +373,14 @@ pub async fn process_login( } } +#[instrument(skip(id))] pub async fn logout(id: Identity) -> Result { + info!("Logging out the user"); id.forget(); Ok(redirect_builder("/admin/login/")) } +#[instrument] pub async fn redirect( tera: web::Data, config: web::Data, @@ -390,12 +411,14 @@ pub async fn redirect( } } +#[instrument] pub async fn redirect_empty( config: web::Data, ) -> Result { Ok(redirect_builder(&config.empty_forward_url)) } +#[instrument(skip(id))] pub async fn create_link( tera: web::Data, config: web::Data, @@ -416,6 +439,7 @@ pub async fn create_link( } } +#[instrument(skip(id))] pub async fn process_link_creation( data: web::Form, config: web::Data, @@ -428,6 +452,7 @@ pub async fn process_link_creation( ))) } +#[instrument(skip(id))] pub async fn edit_link( tera: web::Data, config: web::Data, @@ -460,6 +485,7 @@ pub async fn process_link_edit( } } +#[instrument(skip(id))] pub async fn process_link_delete( id: Identity, config: web::Data,