Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing (optional) #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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