Add opt-in performance tracing telemetry

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/20/head
trivernis 3 years ago
parent aa772ea173
commit a11a2f3dc5
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1129,6 +1129,12 @@ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
] ]
[[package]]
name = "integer-encoding"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e85a1509a128c855368e135cffcde7eac17d8e1083f41e2b98c58bc1a5074be"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.3" version = "0.10.3"
@ -1326,6 +1332,8 @@ dependencies = [
"mediarepo-logic", "mediarepo-logic",
"mediarepo-socket", "mediarepo-socket",
"num-integer", "num-integer",
"opentelemetry",
"opentelemetry-jaeger",
"rolling-file", "rolling-file",
"structopt", "structopt",
"tokio", "tokio",
@ -1334,6 +1342,7 @@ dependencies = [
"tracing-appender", "tracing-appender",
"tracing-flame", "tracing-flame",
"tracing-log", "tracing-log",
"tracing-opentelemetry",
"tracing-subscriber", "tracing-subscriber",
] ]
@ -1698,6 +1707,60 @@ dependencies = [
"vcpkg", "vcpkg",
] ]
[[package]]
name = "opentelemetry"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6105e89802af13fdf48c49d7646d3b533a70e536d818aae7e78ba0433d01acb8"
dependencies = [
"async-trait",
"crossbeam-channel",
"futures-channel",
"futures-executor",
"futures-util",
"js-sys",
"lazy_static",
"percent-encoding",
"pin-project",
"rand",
"thiserror",
"tokio",
"tokio-stream",
]
[[package]]
name = "opentelemetry-jaeger"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8c0b12cd9e3f9b35b52f6e0dac66866c519b26f424f4bbf96e3fe8bfbdc5229"
dependencies = [
"async-trait",
"lazy_static",
"opentelemetry",
"opentelemetry-semantic-conventions",
"thiserror",
"thrift",
"tokio",
]
[[package]]
name = "opentelemetry-semantic-conventions"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985cc35d832d412224b2cffe2f9194b1b89b6aa5d0bef76d080dce09d90e62bd"
dependencies = [
"opentelemetry",
]
[[package]]
name = "ordered-float"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3305af35278dd29f46fcdd139e0b1fbfae2153f0e5928b39b035542dd31e37b7"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "ordered-multimap" name = "ordered-multimap"
version = "0.3.1" version = "0.3.1"
@ -2685,6 +2748,19 @@ dependencies = [
"num_cpus", "num_cpus",
] ]
[[package]]
name = "thrift"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b82ca8f46f95b3ce96081fe3dd89160fdea970c254bb72925255d1b62aae692e"
dependencies = [
"byteorder",
"integer-encoding",
"log",
"ordered-float",
"threadpool",
]
[[package]] [[package]]
name = "thumbnailer" name = "thumbnailer"
version = "0.4.0" version = "0.4.0"
@ -3023,6 +3099,19 @@ dependencies = [
"tracing-core", "tracing-core",
] ]
[[package]]
name = "tracing-opentelemetry"
version = "0.17.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f9378e96a9361190ae297e7f3a8ff644aacd2897f244b1ff81f381669196fa6"
dependencies = [
"opentelemetry",
"tracing",
"tracing-core",
"tracing-log",
"tracing-subscriber",
]
[[package]] [[package]]
name = "tracing-serde" name = "tracing-serde"
version = "0.1.3" version = "0.1.3"

@ -27,6 +27,9 @@ rolling-file = "0.1.0"
num-integer = "0.1.44" num-integer = "0.1.44"
console-subscriber = "0.1.3" console-subscriber = "0.1.3"
log = "0.4.14" log = "0.4.14"
opentelemetry = { version = "0.17.0", features = ["rt-tokio"] }
opentelemetry-jaeger = { version = "0.16.0", features = ["rt-tokio"] }
tracing-opentelemetry = "0.17.2"
[dependencies.mediarepo-core] [dependencies.mediarepo-core]
path = "./mediarepo-core" path = "./mediarepo-core"

@ -1,11 +1,15 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::Level; use tracing::Level;
const DEFAULT_TELEMETRY_ENDPOINT: &str = "telemetry.trivernis.net:6831";
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LoggingSettings { pub struct LoggingSettings {
pub level: LogLevel, pub level: LogLevel,
pub trace_sql: bool, pub trace_sql: bool,
pub trace_api_calls: bool, pub trace_api_calls: bool,
pub telemetry: bool,
pub telemetry_endpoint: String,
} }
impl Default for LoggingSettings { impl Default for LoggingSettings {
@ -14,6 +18,8 @@ impl Default for LoggingSettings {
level: LogLevel::Info, level: LogLevel::Info,
trace_sql: false, trace_sql: false,
trace_api_calls: false, trace_api_calls: false,
telemetry: false,
telemetry_endpoint: String::from(DEFAULT_TELEMETRY_ENDPOINT),
} }
} }
} }

@ -19,5 +19,5 @@ features = ["migrate"]
[dependencies.sea-orm] [dependencies.sea-orm]
version = "0.6.0" version = "0.6.0"
features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "debug-print"] features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros"]
default-features = false default-features = false

@ -13,7 +13,8 @@ pub async fn get_database<S: AsRef<str>>(uri: S) -> RepoDatabaseResult<DatabaseC
migrate(uri.as_ref()).await?; migrate(uri.as_ref()).await?;
let mut opt = ConnectOptions::new(uri.as_ref().to_string()); let mut opt = ConnectOptions::new(uri.as_ref().to_string());
opt.connect_timeout(Duration::from_secs(10)) opt.connect_timeout(Duration::from_secs(10))
.idle_timeout(Duration::from_secs(10)); .idle_timeout(Duration::from_secs(10))
.sqlx_logging(false);
let conn = Database::connect(opt).await?; let conn = Database::connect(opt).await?;

@ -2,6 +2,8 @@ use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use console_subscriber::ConsoleLayer; use console_subscriber::ConsoleLayer;
use opentelemetry::sdk::Resource;
use opentelemetry::{sdk, KeyValue};
use rolling_file::RollingConditionBasic; use rolling_file::RollingConditionBasic;
use tracing::Level; use tracing::Level;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard}; use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
@ -37,6 +39,10 @@ pub fn init_tracing(repo_path: &PathBuf, log_cfg: &LoggingSettings) -> Vec<DropG
add_bromine_layer(log_cfg, &log_path, &mut guards, &mut layer_list); add_bromine_layer(log_cfg, &log_path, &mut guards, &mut layer_list);
add_app_log_layer(log_cfg, &log_path, &mut guards, &mut layer_list); add_app_log_layer(log_cfg, &log_path, &mut guards, &mut layer_list);
if log_cfg.telemetry {
add_telemetry_layer(log_cfg, &mut layer_list);
}
let tokio_console_enabled = std::env::var("TOKIO_CONSOLE") let tokio_console_enabled = std::env::var("TOKIO_CONSOLE")
.map(|v| v.eq_ignore_ascii_case("true")) .map(|v| v.eq_ignore_ascii_case("true"))
.unwrap_or(false); .unwrap_or(false);
@ -56,6 +62,39 @@ fn add_tokio_console_layer(layer_list: &mut DynLayerList<Registry>) {
layer_list.add(console_layer); layer_list.add(console_layer);
} }
fn add_telemetry_layer(log_cfg: &LoggingSettings, layer_list: &mut DynLayerList<Registry>) {
match opentelemetry_jaeger::new_pipeline()
.with_agent_endpoint(&log_cfg.telemetry_endpoint)
.with_trace_config(
sdk::trace::Config::default()
.with_resource(Resource::new(vec![KeyValue::new(
"service.name",
"mediarepo-daemon",
)]))
.with_max_attributes_per_span(1),
)
.with_instrumentation_library_tags(false)
.with_service_name("mediarepo-daemon")
.install_batch(opentelemetry::runtime::Tokio)
{
Ok(tracer) => {
let telemetry_layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
.with_filter(
filter::Targets::new()
.with_target("tokio", Level::INFO)
.with_target("h2", Level::INFO)
.with_target("sqlx", Level::ERROR)
.with_target("sea_orm", Level::INFO),
);
layer_list.add(telemetry_layer);
}
Err(e) => {
eprintln!("Failed to initialize telemetry tracing: {}", e);
}
}
}
fn add_app_log_layer( fn add_app_log_layer(
log_cfg: &LoggingSettings, log_cfg: &LoggingSettings,
log_path: &PathBuf, log_path: &PathBuf,

@ -79,6 +79,7 @@ async fn main() -> RepoResult<()> {
SubCommand::Start => start_server(opt, settings).await, SubCommand::Start => start_server(opt, settings).await,
}; };
opentelemetry::global::shutdown_tracer_provider();
match result { match result {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => { Err(e) => {

Loading…
Cancel
Save