Additional logging and tracing enhancements

This commit is contained in:
Cameron
2025-06-04 15:05:23 -04:00
parent 7c882fd31c
commit 2c553a8016
5 changed files with 231 additions and 68 deletions

View File

@@ -1,6 +1,6 @@
use actix_web::http::header::HeaderMap;
use actix_web::HttpRequest;
use opentelemetry::global::BoxedTracer;
use opentelemetry::global::{BoxedSpan, BoxedTracer};
use opentelemetry::propagation::TextMapPropagator;
use opentelemetry::trace::{Span, Status, Tracer};
use opentelemetry::{global, Context, KeyValue};
@@ -82,26 +82,31 @@ pub fn extract_context_from_request(req: &HttpRequest) -> Context {
propagator.extract(&HeaderExtractor(req.headers()))
}
pub fn trace_db_call<F, O>(context: &Context, query_type: &str, operation: &str, func: F) -> anyhow::Result<O>
where F: FnOnce() -> anyhow::Result<O> {
pub fn trace_db_call<F, O>(
context: &Context,
query_type: &str,
operation: &str,
func: F,
) -> anyhow::Result<O>
where
F: FnOnce(&mut BoxedSpan) -> anyhow::Result<O>,
{
let tracer = global::tracer("db");
let mut span = tracer
.span_builder(format!("db.{}.{}", query_type, operation))
.with_attributes(vec![
KeyValue::new("db.query_type", query_type.to_string().clone()),
KeyValue::new("db.operation", operation.to_string().clone()),
]).start_with_context(&tracer, context);
let result = func();
])
.start_with_context(&tracer, context);
let result = func(&mut span);
match &result {
Ok(_) => {
span.set_status(Status::Ok);
}
Err(e) => {
span.set_status(Status::error(e.to_string()))
}
Err(e) => span.set_status(Status::error(e.to_string())),
}
result
}
}