Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions examples/actix-web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,22 @@ async fn root() -> HttpResponse {
async fn get_user(path: web::Path<u32>) -> ActixResult<HttpResponse> {
let user_id = path.into_inner();
async {
logfire::info!("Fetching user with ID: {user_id}", user_id = user_id as i64);
logfire::info!("Fetching user with ID: {user_id}");

// Simulate database lookup
tokio::time::sleep(std::time::Duration::from_millis(10))
.instrument(logfire::span!("Database query for user"))
.await;

logfire::debug!(
"Database query completed for user {user_id}",
user_id = user_id as i64
);
logfire::debug!("Database query completed for user {user_id}");

if user_id == 0 {
logfire::warn!(
"Invalid user ID requested: {user_id}",
user_id = user_id as i64
);
logfire::warn!("Invalid user ID requested: {user_id}");
return Ok(HttpResponse::BadRequest().finish());
}

if user_id > 1000 {
logfire::error!("User {user_id} not found", user_id = user_id as i64);
logfire::error!("User {user_id} not found");
return Ok(HttpResponse::NotFound().finish());
}

Expand All @@ -120,10 +114,7 @@ async fn get_user(path: web::Path<u32>) -> ActixResult<HttpResponse> {
email: format!("user{user_id}@example.com"),
};

logfire::info!(
"Successfully retrieved user {user_id}",
user_id = user_id as i64
);
logfire::info!("Successfully retrieved user {user_id}");

Ok(HttpResponse::Ok().json(user))
}
Expand Down Expand Up @@ -158,7 +149,7 @@ async fn create_user(payload: web::Json<CreateUserRequest>) -> ActixResult<HttpR

logfire::info!(
"Successfully created user {id} with name {name}",
id = user.id as i64,
id = user.id,
name = &user.name
);

Expand Down
23 changes: 7 additions & 16 deletions examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,28 +102,22 @@ async fn root() -> &'static str {

async fn get_user(Path(user_id): Path<u32>) -> Result<Json<User>, StatusCode> {
async {
logfire::info!("Fetching user with ID: {user_id}", user_id = user_id as i64);
logfire::info!("Fetching user with ID: {user_id}");

// Simulate database lookup
tokio::time::sleep(std::time::Duration::from_millis(10))
.instrument(logfire::span!("Database query for user"))
.await;

logfire::debug!(
"Database query completed for user {user_id}",
user_id = user_id as i64
);
logfire::debug!("Database query completed for user {user_id}",);

if user_id == 0 {
logfire::warn!(
"Invalid user ID requested: {user_id}",
user_id = user_id as i64
);
logfire::warn!("Invalid user ID requested: {user_id}",);
return Err(StatusCode::BAD_REQUEST);
}

if user_id > 1000 {
logfire::error!("User {user_id} not found", user_id = user_id as i64);
logfire::error!("User {user_id} not found");
return Err(StatusCode::NOT_FOUND);
}

Expand All @@ -133,14 +127,11 @@ async fn get_user(Path(user_id): Path<u32>) -> Result<Json<User>, StatusCode> {
email: format!("user{user_id}@example.com"),
};

logfire::info!(
"Successfully retrieved user {user_id}",
user_id = user_id as i64
);
logfire::info!("Successfully retrieved user {user_id}",);

Ok(Json(user))
}
.instrument(logfire::span!("Fetching user {user_id}", user_id = user_id))
.instrument(logfire::span!("Fetching user {user_id}"))
.await
}

Expand Down Expand Up @@ -173,7 +164,7 @@ async fn create_user(

logfire::info!(
"Successfully created user {id} with name {name}",
id = user.id as i64,
id = user.id,
name = &user.name
);

Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() -> Result<()> {
logfire::info!(
"total size of {cwd} is {size} bytes",
cwd = cwd.display().to_string(),
size = total_size as i64
size = total_size
);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/internal/exporters/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ mod tests {
let _ = crate::span!(level: Level::DEBUG, "debug span");
let _ =
crate::span!(parent: &root, level: Level::DEBUG, "debug span with explicit parent");
crate::info!("log with values", foo = 42, bar = 33);
crate::info!("log with values", foo = 42i64, bar = 33i64);
crate::info!("hello world log");
panic!("oh no!");
}))
Expand Down
70 changes: 57 additions & 13 deletions src/macros/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,71 @@ pub fn converter<T>(_: &T) -> LogfireConverter<T> {
LogfireConverter(TryConvertOption(FallbackToConvertValue(PhantomData)))
}

/// `usize` might exceed OTLP range of i64. The Python SDK handles this by converting
/// to a string for oversize values, we do the same.
impl LogfireConverter<usize> {
/// Convenience to take ownership of borrow on String
impl LogfireConverter<&'_ String> {
#[inline]
#[must_use]
pub fn convert_value(&self, value: usize) -> Option<Value> {
if let Ok(value) = i64::try_from(value) {
Some(value.into())
} else {
// TODO emit a warning?
Some(value.to_string().into())
pub fn convert_value(&self, value: &String) -> Option<Value> {
Some(String::to_owned(value).into())
}
}

macro_rules! impl_into_try_into_i64_value {
($type:ty) => {
impl LogfireConverter<$type> {
#[inline]
#[must_use]
pub fn convert_value(&self, value: $type) -> Option<Value> {
// Attempt to convert the value to an i64.
if let Ok(value) = i64::try_from(value) {
Some(value.into())
} else {
// If it fails (e.g., overflow), fall back to a string.
// TODO emit a warning?
Some(value.to_string().into())
}
}
}
};
}

macro_rules! impl_into_from_i64_value {
($type:ty) => {
impl LogfireConverter<$type> {
#[inline]
#[must_use]
pub fn convert_value(&self, value: $type) -> Option<Value> {
Some(i64::from(value).into())
}
}
};
}

impl_into_from_i64_value!(u8);
impl_into_from_i64_value!(u16);
impl_into_from_i64_value!(u32);
impl_into_try_into_i64_value!(u64);
impl_into_try_into_i64_value!(u128);
impl_into_try_into_i64_value!(usize);
impl_into_from_i64_value!(i8);
impl_into_from_i64_value!(i16);
impl_into_from_i64_value!(i32);
impl_into_try_into_i64_value!(i128);
impl_into_try_into_i64_value!(isize);

impl LogfireConverter<f32> {
#[inline]
#[must_use]
pub fn convert_value(&self, value: f32) -> Option<Value> {
Some(f64::from(value).into())
}
}

/// Convenience to take ownership of borrow on String
impl LogfireConverter<&'_ String> {
impl LogfireConverter<char> {
#[inline]
#[must_use]
pub fn convert_value(&self, value: &String) -> Option<Value> {
Some(String::to_owned(value).into())
pub fn convert_value(&self, value: char) -> Option<Value> {
Some(value.to_string().into())
}
}

Expand Down
Loading
Loading