Add Opentelemetry tracing

This commit is contained in:
Daan Boerlage 2023-04-06 13:51:16 +02:00
parent 7dd317512d
commit 95dc469e4b
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 993 additions and 24 deletions

955
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,4 +9,11 @@ axum = "0.6.12"
reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls-webpki-roots"] } reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls-webpki-roots"] }
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
eyre = "0.6" eyre = "0.6"
# Insights
tower-http = { version = "0.4", features = ["trace"] }
tracing = "0.1"
axum-tracing-opentelemetry = { version = "0.10", features = ["otlp", "tracing_subscriber_ext"] }
opentelemetry = { version = "0.19", features = ["rt-tokio"] }
tracing-opentelemetry = "0.18"

1
src/lifecycle/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod shutdown;

26
src/lifecycle/shutdown.rs Normal file
View file

@ -0,0 +1,26 @@
pub async fn shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c()
.await
.expect("failed to install Ctrl+C handler");
};
#[cfg(unix)]
let terminate = async {
tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("failed to install signal handler")
.recv()
.await;
};
#[cfg(not(unix))]
let terminate = std::future::pending::<()>();
tokio::select! {
_ = ctrl_c => {},
_ = terminate => {},
}
tracing::warn!("signal received, starting graceful shutdown");
opentelemetry::global::shutdown_tracer_provider();
}

View file

@ -1,10 +1,13 @@
mod collectors; mod collectors;
mod lifecycle;
use axum::routing::get; use axum::{Router, BoxError};
use axum::Router;
use axum::extract::Path; use axum::extract::Path;
use axum::http::StatusCode; use axum::http::StatusCode;
use axum::response::IntoResponse; use axum::response::{IntoResponse};
use axum::routing::get;
use axum_tracing_opentelemetry::{response_with_trace_layer, opentelemetry_tracing_layer};
use std::net::SocketAddr;
use collectors::PromMetric; use collectors::PromMetric;
fn convert_into_metrics(data: Vec<impl PromMetric>) -> String { fn convert_into_metrics(data: Vec<impl PromMetric>) -> String {
@ -36,15 +39,22 @@ async fn stats(Path(rsn): Path<String>) -> impl IntoResponse {
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<(), BoxError> {
axum_tracing_opentelemetry::tracing_subscriber_ext::init_subscribers()?;
let app = Router::new() let app = Router::new()
.route("/worlds", get(worlds)) .route("/worlds", get(worlds))
.route("/stats/:rsn", get(stats)); .route("/stats/:rsn", get(stats))
.layer(response_with_trace_layer())
.layer(opentelemetry_tracing_layer());
println!("Starting..."); let addr = SocketAddr::from(([0, 0, 0, 0], 3030));
tracing::warn!("listening on {}", addr);
axum::Server::bind(&"0.0.0.0:3030".parse().unwrap()) axum::Server::bind(&"0.0.0.0:3030".parse().unwrap())
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .with_graceful_shutdown(lifecycle::shutdown::shutdown_signal())
.unwrap(); .await?;
Ok(())
} }