From 815e0928849c1455909520fad032b0aa4cf161ee Mon Sep 17 00:00:00 2001 From: Denis Gorbachev <829578+DenisGorbachev@users.noreply.github.com> Date: Fri, 5 Jul 2024 11:43:40 +0700 Subject: [PATCH] Add tracing (optional) --- Cargo.toml | 2 ++ examples/create_a_message.rs | 12 ++++++++++++ examples/streaming_messages.rs | 12 ++++++++++++ src/api_key.rs | 2 +- src/client.rs | 2 +- src/messages/api.rs | 2 ++ 6 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8b7e37e..cd2641c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ thiserror = "1.0.*" pin-project = "1.1.*" futures-core = "0.3.*" clust_macros = { version = "0.9.0", optional = true } +tracing = { version = "0.1.*", optional = true, features = ["attributes"] } [dev-dependencies] anyhow = "1.0.86" @@ -43,3 +44,4 @@ tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread", "fs"] } futures-util = "0.3.30" tokio-stream = "0.1.15" base64 = "0.22.1" +tracing-subscriber = { version = "0.3.18" , features = ["env-filter"] } diff --git a/examples/create_a_message.rs b/examples/create_a_message.rs index 9fa6bdd..64e7a80 100644 --- a/examples/create_a_message.rs +++ b/examples/create_a_message.rs @@ -28,6 +28,9 @@ struct Arguments { #[tokio::main] async fn main() -> anyhow::Result<()> { + #[cfg(feature = "tracing")] + init_tracing_subscriber(); + // 0. Parse the command-line arguments. let arguments = Arguments::parse(); @@ -68,3 +71,12 @@ async fn main() -> anyhow::Result<()> { Ok(()) } + +#[cfg(feature = "tracing")] +fn init_tracing_subscriber() { + use tracing_subscriber::util::SubscriberInitExt; + let subscriber = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .finish(); + subscriber.init(); +} diff --git a/examples/streaming_messages.rs b/examples/streaming_messages.rs index 70fe7ce..dc503cc 100644 --- a/examples/streaming_messages.rs +++ b/examples/streaming_messages.rs @@ -31,6 +31,9 @@ struct Arguments { #[tokio::main] async fn main() -> anyhow::Result<()> { + #[cfg(feature = "tracing")] + init_tracing_subscriber(); + // 0. Parse the command-line arguments. let arguments = Arguments::parse(); @@ -86,3 +89,12 @@ async fn main() -> anyhow::Result<()> { Ok(()) } + +#[cfg(feature = "tracing")] +fn init_tracing_subscriber() { + use tracing_subscriber::util::SubscriberInitExt; + let subscriber = tracing_subscriber::fmt() + .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) + .finish(); + subscriber.init(); +} diff --git a/src/api_key.rs b/src/api_key.rs index 1b8e012..85eac8e 100644 --- a/src/api_key.rs +++ b/src/api_key.rs @@ -1,7 +1,7 @@ use std::env::VarError; /// The API key of the Anthropic API. -#[derive(Clone, Eq, PartialEq, Hash)] +#[derive(Clone, Eq, PartialEq, Hash, Debug)] pub struct ApiKey { value: String, } diff --git a/src/client.rs b/src/client.rs index 6075912..00ab61b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -8,7 +8,7 @@ use crate::messages::{ use crate::{ApiKey, Beta, Version}; /// The API client. -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct Client { /// The API key. api_key: ApiKey, diff --git a/src/messages/api.rs b/src/messages/api.rs index 8cb91f0..e6842df 100644 --- a/src/messages/api.rs +++ b/src/messages/api.rs @@ -9,6 +9,7 @@ use crate::ClientError; use futures_core::Stream; +#[cfg_attr(feature = "tracing", tracing::instrument)] pub(crate) async fn create_a_message( client: &Client, request_body: MessagesRequestBody, @@ -65,6 +66,7 @@ pub(crate) async fn create_a_message( } } +#[cfg_attr(feature = "tracing", tracing::instrument)] pub(crate) async fn create_a_message_stream( client: &Client, request_body: MessagesRequestBody,