more tracing

This commit is contained in:
Robin Appelman 2024-01-19 22:49:15 +01:00
commit b0348af90f
6 changed files with 115 additions and 29 deletions

2
.gitignore vendored
View file

@ -4,3 +4,5 @@ result
config.toml config.toml
.env .env
node_modules node_modules
*.key
*.crt

61
Cargo.lock generated
View file

@ -146,6 +146,28 @@ dependencies = [
"sha2 0.9.9", "sha2 0.9.9",
] ]
[[package]]
name = "async-stream"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd56dd203fef61ac097dd65721a419ddccb106b2d2b70ba60a6b529f03961a51"
dependencies = [
"async-stream-impl",
"futures-core",
"pin-project-lite",
]
[[package]]
name = "async-stream-impl"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.68" version = "0.1.68"
@ -913,6 +935,7 @@ dependencies = [
"time 0.3.20", "time 0.3.20",
"tokio", "tokio",
"toml 0.8.8", "toml 0.8.8",
"tonic",
"tower-http", "tower-http",
"tracing", "tracing",
"tracing-opentelemetry", "tracing-opentelemetry",
@ -3188,8 +3211,9 @@ version = "0.21.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba" checksum = "f9d5a6813c0759e4609cd494e8e725babae6a2ca7b62a5536a13daaec6fcb7ba"
dependencies = [ dependencies = [
"log",
"ring 0.17.3", "ring 0.17.3",
"rustls-webpki", "rustls-webpki 0.101.7",
"sct", "sct",
] ]
@ -3202,6 +3226,16 @@ dependencies = [
"base64 0.21.0", "base64 0.21.0",
] ]
[[package]]
name = "rustls-webpki"
version = "0.100.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f6a5fc258f1c1276dfe3016516945546e2d5383911efc0fc4f1cdc5df3a4ae3"
dependencies = [
"ring 0.16.20",
"untrusted 0.7.1",
]
[[package]] [[package]]
name = "rustls-webpki" name = "rustls-webpki"
version = "0.101.7" version = "0.101.7"
@ -3692,7 +3726,7 @@ dependencies = [
"tokio-stream", "tokio-stream",
"tracing", "tracing",
"url", "url",
"webpki-roots", "webpki-roots 0.25.3",
] ]
[[package]] [[package]]
@ -5087,6 +5121,16 @@ dependencies = [
"tokio", "tokio",
] ]
[[package]]
name = "tokio-rustls"
version = "0.24.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081"
dependencies = [
"rustls",
"tokio",
]
[[package]] [[package]]
name = "tokio-stream" name = "tokio-stream"
version = "0.1.12" version = "0.1.12"
@ -5161,6 +5205,7 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a" checksum = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a"
dependencies = [ dependencies = [
"async-stream",
"async-trait", "async-trait",
"axum", "axum",
"base64 0.21.0", "base64 0.21.0",
@ -5175,12 +5220,15 @@ dependencies = [
"percent-encoding", "percent-encoding",
"pin-project", "pin-project",
"prost", "prost",
"rustls-pemfile",
"tokio", "tokio",
"tokio-rustls",
"tokio-stream", "tokio-stream",
"tower", "tower",
"tower-layer", "tower-layer",
"tower-service", "tower-service",
"tracing", "tracing",
"webpki-roots 0.23.1",
] ]
[[package]] [[package]]
@ -5614,6 +5662,15 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "webpki-roots"
version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b03058f88386e5ff5310d9111d53f48b17d732b401aeb83a8d5190f2ac459338"
dependencies = [
"rustls-webpki 0.100.3",
]
[[package]] [[package]]
name = "webpki-roots" name = "webpki-roots"
version = "0.25.3" version = "0.25.3"

View file

@ -9,7 +9,7 @@ tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tracing-opentelemetry = "0.22.0" tracing-opentelemetry = "0.22.0"
opentelemetry = { version = "0.21.0" } opentelemetry = { version = "0.21.0" }
opentelemetry_sdk = { version = "0.21.2", features = ["rt-tokio"] } opentelemetry_sdk = { version = "0.21.2", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.14.0" } opentelemetry-otlp = { version = "0.14.0", features = ["tls"] }
serde = { version = "1.0.159", features = ["derive"] } serde = { version = "1.0.159", features = ["derive"] }
toml = "0.8.8" toml = "0.8.8"
sqlx = { version = "0.7.3", features = ["postgres", "time", "runtime-tokio-rustls"] } sqlx = { version = "0.7.3", features = ["postgres", "time", "runtime-tokio-rustls"] }
@ -37,3 +37,4 @@ rand = "0.8.5"
demostf-build = { path = "./build", version = "*" } demostf-build = { path = "./build", version = "*" }
include_dir = "0.7.3" include_dir = "0.7.3"
serde-env = "0.1.1" serde-env = "0.1.1"
tonic = { version = "0.9.2", features = ["tls", "tls-webpki-roots"] }

View file

@ -13,6 +13,8 @@ pub struct Config {
pub listen: Listen, pub listen: Listen,
pub database: DbConfig, pub database: DbConfig,
pub site: SiteConfig, pub site: SiteConfig,
#[serde(default)]
pub tracing: Option<TracingConfig>,
} }
impl Config { impl Config {
@ -89,3 +91,16 @@ fn default_api() -> String {
fn default_maps() -> String { fn default_maps() -> String {
"https://maps.demos.tf/".into() "https://maps.demos.tf/".into()
} }
#[derive(Debug, Deserialize)]
pub struct TracingConfig {
pub endpoint: String,
#[serde(default)]
pub tls: Option<TracingTlsConfig>,
}
#[derive(Debug, Deserialize)]
pub struct TracingTlsConfig {
pub cert_file: String,
pub key_file: String,
}

View file

@ -36,6 +36,8 @@ pub enum SetupError {
TracingSubscriber(#[from] TryInitError), TracingSubscriber(#[from] TryInitError),
#[error(transparent)] #[error(transparent)]
Config(#[from] ConfigError), Config(#[from] ConfigError),
#[error("{0}")]
Other(String),
} }
impl IntoResponse for Error { impl IntoResponse for Error {

View file

@ -44,17 +44,16 @@ use opentelemetry_otlp::WithExportConfig;
use opentelemetry_sdk::{runtime, trace, Resource}; use opentelemetry_sdk::{runtime, trace, Resource};
use sqlx::PgPool; use sqlx::PgPool;
use std::env::{args, var}; use std::env::{args, var};
use std::fs::{remove_file, set_permissions, Permissions}; use std::fs::{read, remove_file, set_permissions, Permissions};
use std::net::SocketAddr; use std::net::SocketAddr;
use std::os::unix::fs::PermissionsExt; use std::os::unix::fs::PermissionsExt;
use std::sync::Arc; use std::sync::Arc;
use steam_openid::SteamOpenId; use steam_openid::SteamOpenId;
use tokio::signal::ctrl_c; use tokio::signal::ctrl_c;
use tonic::transport::{ClientTlsConfig, Identity};
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::{error, info, info_span}; use tracing::{error, info, info_span};
use tracing_subscriber::{ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
fmt::layer, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer,
};
pub type Result<T, E = Error> = std::result::Result<T, E>; pub type Result<T, E = Error> = std::result::Result<T, E>;
@ -77,19 +76,42 @@ struct LogoSvg;
static KILL_ICONS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/images/kill_icons"); static KILL_ICONS: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/images/kill_icons");
fn setup() -> Result<Config, SetupError> { fn setup() -> Result<Config, SetupError> {
let open_telemetry = if let Some(tracing_endpoint) = var("TRACING_ENDPOINT") let config = args()
.ok() .nth(1)
.filter(|endpoint| !endpoint.is_empty()) .as_deref()
.map(Config::load)
.transpose()?
.or_else(Config::env)
.ok_or(SetupError::NoConfigProvided)?;
let open_telemetry = if let Some(tracing_cfg) = config
.tracing
.as_ref()
.filter(|tracing_cfg| !tracing_cfg.endpoint.is_empty())
{ {
let otlp_exporter = opentelemetry_otlp::new_exporter() let mut otlp_exporter = opentelemetry_otlp::new_exporter()
.tonic() .tonic()
.with_endpoint(tracing_endpoint); .with_endpoint(&tracing_cfg.endpoint);
if let Some(tracing_ident) = tracing_cfg.tls.as_ref().map(|tracing_tls_cfg| {
let key = read(&tracing_tls_cfg.key_file).map_err(|_| {
SetupError::Other(format!("failed to open {}", tracing_tls_cfg.key_file))
})?;
let cert = read(&tracing_tls_cfg.cert_file).map_err(|_| {
SetupError::Other(format!("failed to open {}", tracing_tls_cfg.cert_file))
})?;
Result::<_, SetupError>::Ok(Identity::from_pem(cert, key))
}) {
let tls_config = ClientTlsConfig::new().identity(tracing_ident?);
otlp_exporter = otlp_exporter.with_tls_config(tls_config);
}
let tracer = let tracer =
opentelemetry_otlp::new_pipeline() opentelemetry_otlp::new_pipeline()
.tracing() .tracing()
.with_exporter(otlp_exporter) .with_exporter(otlp_exporter)
.with_trace_config(trace::config().with_resource(Resource::new(vec![ .with_trace_config(trace::config().with_resource(Resource::new(vec![
KeyValue::new("service.name", "drops.tf"), KeyValue::new("service.name", "demos.tf"),
]))) ])))
.install_batch(runtime::Tokio)?; .install_batch(runtime::Tokio)?;
Some(tracing_opentelemetry::layer().with_tracer(tracer)) Some(tracing_opentelemetry::layer().with_tracer(tracer))
@ -107,24 +129,11 @@ fn setup() -> Result<Config, SetupError> {
))) )))
.try_init()?; .try_init()?;
args() Ok(config)
.nth(1)
.as_deref()
.map(Config::load)
.transpose()?
.or_else(Config::env)
.ok_or(SetupError::NoConfigProvided)
} }
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(layer().with_filter(EnvFilter::new(
var("RUST_LOG").unwrap_or_else(|_| "warn,frontend=info".into()),
)))
.try_init()
.expect("Failed to init tracing");
let config = setup()?; let config = setup()?;
let connection = config.database.connect().await?; let connection = config.database.connect().await?;