Crates.io | opentelemetry-auto-span |
lib.rs | opentelemetry-auto-span |
version | 0.3.2 |
source | src |
created_at | 2022-08-09 11:36:48.269078 |
updated_at | 2023-11-21 06:57:19.309502 |
description | A helper library for opentelemetry tracing |
homepage | |
repository | https://github.com/hinohi/rust-opentelemetry-auto-span |
max_upload_size | |
id | 641734 |
size | 298,361 |
add dependencies
[dependencies]
opentelemetry-auto-span = "0.3"
annotate function
use opentelemetry_auto_span::auto_span;
#[get("/user/{id}")]
#[auto_span]
async fn get_user(
id: web::Path<(i64,)>,
db: web::Data<sqlx::MySqlPool>,
) -> actix_web::Result<HttpResponse, Error> {
let user: User = sqlx::query_as("SELECT * FROM users WHERE id = ?")
.bind(id.into_inner().0)
.fetch_one(&**db)
.await?;
Ok(HttpResponse::Ok().json(&user))
}
then, capture bellow information
get_user
start to end).await
span.await?
) and handle by ?
, logging the errorsqlx::query*
#[auto_span]
async fn get_user(
id: web::Path<(i64,)>,
db: web::Data<sqlx::MySqlPool>,
) -> actix_web::Result<HttpResponse, Error> {
let user: User = sqlx::query_as("SELECT * FROM users WHERE id = ?")
.bind(id.into_inner().0)
.fetch_one(&**db)
.await?;
Ok(HttpResponse::Ok().json(&user))
}
↓
async fn get_user(
id: web::Path<(i64,)>,
db: web::Data<sqlx::MySqlPool>,
) -> actix_web::Result<HttpResponse, Error> {
#[allow(unused_imports)]
use opentelemetry::trace::{Span as _, TraceContextExt as _, Tracer as _};
let __otel_auto_tracer = opentelemetry::global::tracer("");
let __otel_auto_ctx =
opentelemetry::Context::current_with_span(__otel_auto_tracer.start("fn:get_user"));
let __otel_auto_guard = __otel_auto_ctx.clone().attach();
let __otel_auto_span = __otel_auto_ctx.span();
let user: User = {
let __otel_auto_ctx =
opentelemetry::Context::current_with_span(__otel_auto_tracer.start("db"));
let __otel_auto_guard = __otel_auto_ctx.clone().attach();
let __otel_auto_span = __otel_auto_ctx.span();
{
__otel_auto_span.set_attribute(opentelemetry::KeyValue::new(
"db.statement",
"SELECT * FROM users WHERE id = ?",
));
sqlx::query_as("SELECT * FROM users WHERE id = ?")
}
.bind(id.into_inner().0)
.fetch_one(&**db)
.await
}
.map_err(|e| {
__otel_auto_span.set_status(::opentelemetry::trace::Status::error(format!("{}", e)));
e
})?;
Ok(HttpResponse::Ok().json(&user))
}
usage:
#[auto_span(debug)]
fn my_func() {}
name | action |
---|---|
debug | Dump the migrated code to ./target/auto_span or /tmp/auto_span |
all_await | Generate span for all await |