Skip to content

Commit

Permalink
Add tracing (optional)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisGorbachev committed Jul 5, 2024
1 parent 42a70b0 commit 815e092
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"] }
12 changes: 12 additions & 0 deletions examples/create_a_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}
12 changes: 12 additions & 0 deletions examples/streaming_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();
}
2 changes: 1 addition & 1 deletion src/api_key.rs
Original file line number Diff line number Diff line change
@@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/messages/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 815e092

Please sign in to comment.