This commit is contained in:
2023-08-29 21:34:01 +02:00
parent b80cae1278
commit 25a92d858b
3 changed files with 94 additions and 8 deletions

View File

@@ -1,13 +1,18 @@
use axum::{
error_handling::HandleErrorLayer,
extract::State,
http::header::HeaderMap,
http::StatusCode,
middleware,
routing::{get, post},
BoxError, Router,
};
use sqlx::{
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
ConnectOptions,
};
use std::time::Duration;
use std::{str::FromStr, time::Duration};
use tower::{timeout::TimeoutLayer, ServiceBuilder};
use crate::{AppState, Error, RequestError, TopLevelPage};
@@ -31,11 +36,47 @@ fn get_referer(headers: &HeaderMap) -> Result<&str, Error> {
})
}
#[tracing::instrument]
async fn simple_handler(State(state): State<AppState>) -> &'static str {
use tracing::Instrument;
let pool = async {
SqlitePoolOptions::new()
.max_connections(5)
.connect_with(
SqliteConnectOptions::from_str("/tmp/tmp.SmE1WKBVMf")
.unwrap()
.log_statements(log::LevelFilter::Warn)
.log_slow_statements(
log::LevelFilter::Warn,
std::time::Duration::from_millis(100),
)
.pragma("foreign_keys", "1"),
)
.await
.unwrap()
}
.instrument(tracing::warn_span!("init_pool"))
.await;
tracing::warn!("test event");
async {
sqlx::query("SELECT * FROM users")
.execute(&pool)
.await
.unwrap()
}
.instrument(tracing::warn_span!("test_span"))
.await;
"ok"
}
#[tracing::instrument]
pub fn router(state: AppState) -> Router {
Router::new()
.route("/favicon.svg", get(icon))
.route("/assets/luggage.svg", get(icon))
.route("/q", get(simple_handler))
.route(
"/notfound",
get(|| async {

View File

@@ -7,19 +7,23 @@ use std::time::Duration;
use axum::Router;
use http::Request;
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
use tracing::Span;
use tracing::{Span, Subscriber};
use tracing_log::LogTracer;
use tracing_subscriber::{
filter::{LevelFilter, Targets},
fmt::{format::Format, Layer},
fmt::{
format::{Format, Writer},
FmtContext, FormatEvent, FormatFields, Layer,
},
layer::SubscriberExt,
prelude::*,
registry::Registry,
registry::{LookupSpan, Registry},
};
use uuid::Uuid;
use opentelemetry::{global, runtime::Tokio};
use tracing::Event;
pub enum OpenTelemetryConfig {
Enabled,
@@ -31,6 +35,42 @@ pub enum TokioConsoleConfig {
Disabled,
}
struct DebugFormat;
impl<S, N> FormatEvent<S, N> for DebugFormat
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let metadata = event.metadata();
write!(
&mut writer,
"{}\t{}:\t",
metadata.level(),
metadata.target()
)?;
if let Some(scope) = ctx.event_scope() {
for span in scope.from_root() {
write!(writer, "span: {}\t", span.metadata().name())?;
}
} else {
write!(writer, "NO SPAN\t")?;
};
ctx.field_format().format_fields(writer.by_ref(), event)?;
writeln!(writer)
}
}
pub async fn init_tracing<Func, T>(
opentelemetry_config: OpenTelemetryConfig,
tokio_console_config: TokioConsoleConfig,
@@ -53,6 +93,8 @@ where
.with_level(true)
.with_file(false);
let stdout_format = DebugFormat;
let stdout_layer = Layer::default()
.event_format(stdout_format)
.with_writer(io::stdout);
@@ -83,7 +125,8 @@ where
.with_service_name(env!("CARGO_PKG_NAME"))
.with_max_packet_size(20_000)
.with_auto_split_batch(true)
.install_batch(Tokio)
// .install_batch(Tokio)
.install_simple()
.unwrap();
let opentelemetry_filter = {
@@ -97,8 +140,9 @@ where
])
};
let opentelemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer);
// .with_filter(opentelemetry_filter);
let opentelemetry_layer = tracing_opentelemetry::layer()
.with_tracer(tracer)
.with_filter(opentelemetry_filter);
shutdown_functions.push(Box::new(|| {
println!("shutting down otel");
@@ -144,6 +188,7 @@ impl fmt::Display for Latency {
}
pub fn init_request_tracing(router: Router) -> Router {
return router;
router.layer(
TraceLayer::new_for_http()
.make_span_with(|_request: &Request<_>| {