Skip to content

Commit

Permalink
Merge pull request #440 from guswynn/tracing
Browse files Browse the repository at this point in the history
add `tracing` feature
  • Loading branch information
benesch authored Feb 7, 2022
2 parents 6b4c8ca + 44bcaa9 commit 9594a34
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 19 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- os: macos-11.0
- os: windows-2019
features: cmake-build,libz-static
rdkafka-sys-features: cmake-build,libz-static
- os: ubuntu-20.04
features: tracing
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
Expand All @@ -38,7 +41,7 @@ jobs:
toolchain: ${{ env.rust_version }}
default: true
- run: cargo build --all-targets --verbose --features "${{ matrix.features }}"
- run: cd rdkafka-sys && cargo test --features "${{ matrix.features }}"
- run: cd rdkafka-sys && cargo test --features "${{ matrix.rdkafka-sys-features }}"

# Use the `minimal-versions` resolver to ensure we're not claiming to support
# an older version of a dependency than we actually do.
Expand Down
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.

3 changes: 2 additions & 1 deletion 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 @@ -65,5 +66,5 @@ external_lz4 = ["rdkafka-sys/external_lz4"]
[package.metadata.docs.rs]
# docs.rs doesn't allow writing to ~/.cargo/registry (reasonably), so we have to
# use the CMake build for a proper out-of-tree build.
features = ["cmake_build", "naive-runtime", "tokio"]
features = ["cmake-build", "naive-runtime", "tracing", "tokio"]
rustdoc-args = ["--cfg", "docsrs"]
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::{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 log_enabled!(target: "librdkafka", DEBUG) {
RDKafkaLogLevel::Debug
} else if log_enabled!(target: "librdkafka", Level::Info) {
} else if log_enabled!(target: "librdkafka", INFO) {
RDKafkaLogLevel::Info
} else if log_enabled!(target: "librdkafka", Level::Warn) {
} else if log_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 @@ -9,11 +9,11 @@ use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use std::time::Duration;

use crate::log::trace;
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 slab::Slab;

use rdkafka_sys as rdsys;
Expand Down
13 changes: 10 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 @@ -262,6 +267,8 @@
#![allow(clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_cfg))]

mod log;

pub use rdkafka_sys::types;

pub mod admin;
Expand Down
19 changes: 19 additions & 0 deletions src/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! 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 use log::Level::{Debug as DEBUG, Info as INFO, Warn as WARN};
#[cfg(not(feature = "tracing"))]
pub use log::{debug, error, info, log_enabled, trace, warn};

#[cfg(feature = "tracing")]
pub use tracing::{debug, enabled as log_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 9594a34

Please sign in to comment.