Skip to content

Commit

Permalink
add tracing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed Feb 4, 2022
1 parent 6b4c8ca commit 6235557
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 17 deletions.
33 changes: 33 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ serde_derive = "1.0.0"
serde_json = "1.0.0"
slab = "0.4"
tokio = { version = "1.0", features = ["rt", "time"], optional = true }
tracing = { version = "0.1.30", optional = true }

[dev-dependencies]
async-std = { version = "1.9.0", features = ["attributes"] }
Expand Down Expand Up @@ -61,6 +62,7 @@ zstd = ["rdkafka-sys/zstd"]
zstd-pkg-config = ["rdkafka-sys/zstd-pkg-config"]
external-lz4 = ["rdkafka-sys/external-lz4"]
external_lz4 = ["rdkafka-sys/external_lz4"]
# tracing, optional dependency

[package.metadata.docs.rs]
# docs.rs doesn't allow writing to ~/.cargo/registry (reasonably), so we have to
Expand Down
2 changes: 1 addition & 1 deletion src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ use std::time::Duration;
use futures_channel::oneshot;
use futures_util::future::{self, Either, FutureExt};
use futures_util::ready;
use log::{trace, warn};

use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;

use crate::client::{Client, ClientContext, DefaultClientContext, NativeQueue};
use crate::config::{ClientConfig, FromClientConfig, FromClientConfigAndContext};
use crate::error::{IsError, KafkaError, KafkaResult};
use crate::log::{trace, warn};
use crate::util::{cstr_to_owned, AsCArray, ErrBuf, IntoOpaque, KafkaDrop, NativePtr, Timeout};

//
Expand Down
3 changes: 1 addition & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ use std::slice;
use std::string::ToString;
use std::sync::Arc;

use log::{debug, error, info, trace, warn};

use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;

use crate::config::{ClientConfig, NativeClientConfig, RDKafkaLogLevel};
use crate::consumer::RebalanceProtocol;
use crate::error::{IsError, KafkaError, KafkaResult};
use crate::groups::GroupList;
use crate::log::{debug, error, info, trace, warn};
use crate::metadata::Metadata;
use crate::statistics::Statistics;
use crate::util::{ErrBuf, KafkaDrop, NativePtr, Timeout};
Expand Down
9 changes: 4 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ use std::iter::FromIterator;
use std::os::raw::c_char;
use std::ptr;

use log::{log_enabled, Level};

use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;

use crate::client::ClientContext;
use crate::error::{IsError, KafkaError, KafkaResult};
use crate::log::{enabled, DEBUG, INFO, WARN};
use crate::util::{ErrBuf, KafkaDrop, NativePtr};

/// The log levels supported by librdkafka.
Expand Down Expand Up @@ -286,11 +285,11 @@ impl Extend<(String, String)> for ClientConfig {

/// Return the log level
fn log_level_from_global_config() -> RDKafkaLogLevel {
if log_enabled!(target: "librdkafka", Level::Debug) {
if enabled!(target: "librdkafka", DEBUG) {
RDKafkaLogLevel::Debug
} else if log_enabled!(target: "librdkafka", Level::Info) {
} else if enabled!(target: "librdkafka", INFO) {
RDKafkaLogLevel::Info
} else if log_enabled!(target: "librdkafka", Level::Warn) {
} else if enabled!(target: "librdkafka", WARN) {
RDKafkaLogLevel::Warning
} else {
RDKafkaLogLevel::Error
Expand Down
3 changes: 1 addition & 2 deletions src/consumer/base_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use std::os::raw::c_void;
use std::ptr;
use std::sync::Arc;

use log::trace;

use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;

Expand All @@ -22,6 +20,7 @@ use crate::consumer::{
};
use crate::error::{IsError, KafkaError, KafkaResult};
use crate::groups::GroupList;
use crate::log::trace;
use crate::message::{BorrowedMessage, Message};
use crate::metadata::Metadata;
use crate::topic_partition_list::{Offset, TopicPartitionList};
Expand Down
3 changes: 1 addition & 2 deletions src/consumer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ use std::ptr;
use std::sync::Arc;
use std::time::Duration;

use log::{error, trace};

use rdkafka_sys as rdsys;
use rdkafka_sys::types::*;

use crate::client::{Client, ClientContext, NativeClient};
use crate::error::KafkaResult;
use crate::groups::GroupList;
use crate::log::{error, trace};
use crate::message::BorrowedMessage;
use crate::metadata::Metadata;
use crate::topic_partition_list::{Offset, TopicPartitionList};
Expand Down
2 changes: 1 addition & 1 deletion src/consumer/stream_consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use futures_channel::oneshot;
use futures_util::future::{self, Either, FutureExt};
use futures_util::pin_mut;
use futures_util::stream::{Stream, StreamExt};
use log::trace;
use crate::log::trace;
use slab::Slab;

use rdkafka_sys as rdsys;
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@
//!
//! ## Debugging
//!
//! rust-rdkafka uses the [`log`] and [`env_logger`] crates to handle logging.
//! Logging can be enabled using the `RUST_LOG` environment variable, for
//! example:
//! rust-rdkafka uses the [`log`] crate to handle logging.
//! Optionally, enable the `tracing` feature to emit [`tracing`]
//! events as opposed to [`log`] records.
//!
//! In test and examples, rust-rdkafka uses the [`env_logger`] crate
//! to format logs. In those contexts, logging can be enabled
//! using the `RUST_LOG` environment variable, for example:
//!
//! ```bash
//! RUST_LOG="librdkafka=trace,rdkafka::client=debug" cargo test
Expand All @@ -234,6 +238,7 @@
//! [`StreamConsumer`]: https://docs.rs/rdkafka/*/rdkafka/consumer/stream_consumer/struct.StreamConsumer.html
//! [`ThreadedProducer`]: https://docs.rs/rdkafka/*/rdkafka/producer/base_producer/struct.ThreadedProducer.html
//! [`log`]: https://docs.rs/log
//! [`tracing`]: https://docs.rs/tracing
//! [`env_logger`]: https://docs.rs/env_logger
//! [Apache Kafka]: https://kafka.apache.org
//! [asynchronous processing example]: https://github.com/fede1024/rust-rdkafka/blob/master/examples/asynchronous_processing.rs
Expand Down Expand Up @@ -270,6 +275,7 @@ pub mod config;
pub mod consumer;
pub mod error;
pub mod groups;
pub(crate) mod log;
pub mod message;
pub mod metadata;
pub mod producer;
Expand Down
21 changes: 21 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! A wrapper module to export logging functionality from
//! [`log`] or [`tracing`] depending on the `tracing` feature.
//!
//! [`log`]: https://docs.rs/log
//! [`tracing`]: https://docs.rs/tracing

#[cfg(not(feature = "tracing"))]
pub(crate) use log::{
debug, error, info, log_enabled as enabled, trace, warn, Level::Debug as DEBUG,
Level::Info as INFO, Level::Warn as WARN,
};

#[cfg(feature = "tracing")]
pub(crate) use tracing::{debug, enabled, error, info, trace, warn};

#[cfg(feature = "tracing")]
pub const DEBUG: tracing::Level = tracing::Level::DEBUG;
#[cfg(feature = "tracing")]
pub const INFO: tracing::Level = tracing::Level::INFO;
#[cfg(feature = "tracing")]
pub const WARN: tracing::Level = tracing::Level::WARN;
3 changes: 2 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
use futures_channel::oneshot;
#[cfg(feature = "naive-runtime")]
use futures_util::future::{FutureExt, Map};
use log::trace;

use crate::log::trace;

use rdkafka_sys as rdsys;

Expand Down

0 comments on commit 6235557

Please sign in to comment.