From 35c13fa48619fa2a7193e0928f28926fb0d8d8de Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Mon, 29 Mar 2021 14:58:36 +0200 Subject: [PATCH] First step for telemetry --- Cargo.lock | 170 ++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 4 ++ src/lib.rs | 2 + src/main.rs | 4 +- src/telemetry.rs | 9 +++ 5 files changed, 176 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 70fff92..fbb0c73 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,7 @@ dependencies = [ "futures-sink", "log", "pin-project 0.4.27", - "tokio", + "tokio 0.2.25", "tokio-util", ] @@ -123,7 +123,7 @@ dependencies = [ "futures-channel", "futures-util", "smallvec", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -267,6 +267,20 @@ dependencies = [ "syn", ] +[[package]] +name = "actix-web-opentelemetry" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac71801f541f5225e71f522d56901b0be7fd6ee2181724a7e551b74249d0129" +dependencies = [ + "actix-http", + "actix-web", + "futures", + "opentelemetry 0.12.0", + "opentelemetry-semantic-conventions", + "serde 1.0.125", +] + [[package]] name = "addr2line" version = "0.14.1" @@ -574,6 +588,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "dashmap" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e77a43b28d0668df09411cb0bc9a8c2adc40f9a048afe863e05fd43251e8e39c" +dependencies = [ + "cfg-if 1.0.0", + "num_cpus", +] + [[package]] name = "derive_more" version = "0.99.11" @@ -902,7 +926,7 @@ dependencies = [ "http", "indexmap", "slab", - "tokio", + "tokio 0.2.25", "tokio-util", "tracing", "tracing-futures", @@ -938,6 +962,7 @@ version = "0.17.1" dependencies = [ "actix-rt", "actix-web", + "actix-web-opentelemetry", "badge", "bytes 1.0.1", "config", @@ -947,18 +972,21 @@ dependencies = [ "lazy_static", "number_prefix", "openssl-probe", + "opentelemetry 0.13.0", + "opentelemetry-jaeger", "reqwest", "ructe", "serde 1.0.125", "serde_derive", "serde_json", "tempfile", - "tokio", + "tokio 0.2.25", "tracing", "tracing-actix-web", "tracing-bunyan-formatter", "tracing-futures", "tracing-log", + "tracing-opentelemetry", "tracing-subscriber", "vergen", ] @@ -1025,7 +1053,7 @@ dependencies = [ "itoa", "pin-project 1.0.5", "socket2", - "tokio", + "tokio 0.2.25", "tower-service", "tracing", "want", @@ -1040,7 +1068,7 @@ dependencies = [ "bytes 0.5.6", "hyper", "native-tls", - "tokio", + "tokio 0.2.25", "tokio-tls", ] @@ -1074,6 +1102,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 = "iovec" version = "0.1.4" @@ -1484,6 +1518,73 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "opentelemetry" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "514d24875c140ed269eecc2d1b56d7b71b573716922a763c317fb1b1b4b58f15" +dependencies = [ + "async-trait", + "dashmap", + "fnv", + "futures", + "js-sys", + "lazy_static", + "percent-encoding", + "pin-project 1.0.5", + "rand 0.8.3", + "thiserror", +] + +[[package]] +name = "opentelemetry" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91cea1dfd50064e52db033179952d18c770cbc5dfefc8eba45d619357ba3914" +dependencies = [ + "async-trait", + "futures", + "js-sys", + "lazy_static", + "percent-encoding", + "pin-project 1.0.5", + "rand 0.8.3", + "thiserror", + "tokio 1.4.0", + "tokio-stream", +] + +[[package]] +name = "opentelemetry-jaeger" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a619edf77ab8bc75d99220811e1cf1a14f1760750c80c4752a20a9aef7dce214" +dependencies = [ + "async-trait", + "lazy_static", + "opentelemetry 0.13.0", + "thiserror", + "thrift", +] + +[[package]] +name = "opentelemetry-semantic-conventions" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ecb679b997c41108a498e3ce8b933fe2f643a9a722ad8ca33f95edab84df106" +dependencies = [ + "opentelemetry 0.12.0", +] + +[[package]] +name = "ordered-float" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3305af35278dd29f46fcdd139e0b1fbfae2153f0e5928b39b035542dd31e37b7" +dependencies = [ + "num-traits 0.2.14", +] + [[package]] name = "owned_ttf_parser" version = "0.6.0" @@ -1807,7 +1908,7 @@ dependencies = [ "pin-project-lite 0.2.4", "serde 1.0.125", "serde_urlencoded", - "tokio", + "tokio 0.2.25", "tokio-tls", "url", "wasm-bindgen", @@ -2184,6 +2285,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 = "time" version = "0.1.43" @@ -2268,6 +2382,27 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "tokio" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134af885d758d645f0f0505c9a8b3f9bf8a348fd822e112ab5248138348f1722" +dependencies = [ + "autocfg", + "pin-project-lite 0.2.4", +] + +[[package]] +name = "tokio-stream" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e177a5d8c3bf36de9ebe6d58537d8879e964332f93fb3339e43f618c81361af0" +dependencies = [ + "futures-core", + "pin-project-lite 0.2.4", + "tokio 1.4.0", +] + [[package]] name = "tokio-tls" version = "0.3.1" @@ -2275,7 +2410,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a70f4fcd7b3b24fb194f837560168208f669ca8cb70d0c4b862944452396343" dependencies = [ "native-tls", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -2289,7 +2424,7 @@ dependencies = [ "futures-sink", "log", "pin-project-lite 0.1.11", - "tokio", + "tokio 0.2.25", ] [[package]] @@ -2391,6 +2526,19 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-opentelemetry" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccdf13c28f1654fe806838f28c5b9cb23ca4c0eae71450daa489f50e523ceb1" +dependencies = [ + "opentelemetry 0.12.0", + "tracing", + "tracing-core", + "tracing-log", + "tracing-subscriber", +] + [[package]] name = "tracing-serde" version = "0.1.2" @@ -2439,7 +2587,7 @@ dependencies = [ "rand 0.7.3", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "url", ] @@ -2459,7 +2607,7 @@ dependencies = [ "resolv-conf", "smallvec", "thiserror", - "tokio", + "tokio 0.2.25", "trust-dns-proto", ] diff --git a/Cargo.toml b/Cargo.toml index c47f888..ed49367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ name = "hoc" [dependencies] actix-rt = "1.1.1" actix-web = "3.3.2" +actix-web-opentelemetry = "0.10.0" badge = "0.3.0" bytes = "1.0.1" config = { version = "0.11.0", features = ["toml"] } @@ -24,6 +25,8 @@ git2 = "0.13.17" lazy_static = "1.4.0" number_prefix = "0.4.0" openssl-probe = "0.1.2" +opentelemetry = { version = "0.13.0", default-features = false, features = ["trace", "rt-tokio-current-thread"] } +opentelemetry-jaeger = "0.12.0" reqwest = "0.10.10" serde = "1.0.125" serde_derive = "1.0.123" @@ -33,6 +36,7 @@ tracing-actix-web = "0.2.1" tracing-bunyan-formatter = "0.1.7" tracing-futures = "0.2.5" tracing-log = "0.1.2" +tracing-opentelemetry = "0.11.0" tracing-subscriber = { version = "0.2.17", features = ["registry", "env-filter"] } [build-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 7806845..adea118 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,7 @@ use actix_web::{ middleware::{self, normalize::TrailingSlash}, web, App, HttpResponse, HttpServer, Responder, }; +use actix_web_opentelemetry::RequestTracing; use badge::{Badge, BadgeOptions}; use git2::{BranchType, Repository}; use number_prefix::NumberPrefix; @@ -480,6 +481,7 @@ async fn start_server(listener: TcpListener, settings: Settings) -> std::io::Res App::new() .app_data(state.clone()) .app_data(repo_count.clone()) + .wrap(RequestTracing::new()) .wrap(tracing_actix_web::TracingLogger) .wrap(middleware::NormalizePath::new(TrailingSlash::Trim)) .service(index) diff --git a/src/main.rs b/src/main.rs index 945b6ef..256a6ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ use hoc::{config::Settings, telemetry}; - use std::net::TcpListener; fn init() { @@ -18,7 +17,8 @@ async fn main() -> std::io::Result<()> { let address = format!("{}:{}", settings.host, settings.port); // TODO: error handling - let listener = TcpListener::bind(address)?; + let listener = TcpListener::bind(address).expect("cannot bind addres"); + hoc::run(listener, settings) .await .expect("Server error") diff --git a/src/telemetry.rs b/src/telemetry.rs index e19fd1a..2a441da 100644 --- a/src/telemetry.rs +++ b/src/telemetry.rs @@ -1,3 +1,4 @@ +use opentelemetry::{global, runtime::TokioCurrentThread}; use tracing::{subscriber::set_global_default, Subscriber}; use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer}; use tracing_log::LogTracer; @@ -9,7 +10,15 @@ pub fn get_subscriber(name: &str, env_filter: &str) -> impl Subscriber + Send + let formatting_layer = BunyanFormattingLayer::new(name.to_string(), std::io::stdout); + global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new()); + let tracer = opentelemetry_jaeger::new_pipeline() + .with_service_name(name) + .install_batch(TokioCurrentThread) + .expect("cannot install jaeger pipeline"); + let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); + Registry::default() + .with(telemetry) .with(env_filter) .with(JsonStorageLayer) .with(formatting_layer)