Skip to content

Commit

Permalink
Support client crates that use the standard Rust log framework instea…
Browse files Browse the repository at this point in the history
…d of Tokio Tracing. (#465)

This PR does this by enabling Tokio Tracing to emit logs if no tracing
subscriber is initialized (via the tracing/log feature).

Also fixes guards around costly logging to actually emits log events if no
tracing subscriber is initialized by using the enabled check of the log crate
instead of the enabled check of the tracing crate.

See: https://docs.rs/tracing/0.1.41/tracing/#emitting-log-records
  • Loading branch information
ximon18 authored Dec 18, 2024
1 parent f02b559 commit b69b2cd
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ futures-util = { version = "0.3", optional = true }
hashbrown = { version = "0.14.2", optional = true, default-features = false, features = ["allocator-api2", "inline-more"] } # 0.14.2 introduces explicit hashing
heapless = { version = "0.8", optional = true }
libc = { version = "0.2.153", default-features = false, optional = true } # 0.2.79 is the first version that has IP_PMTUDISC_OMIT
log = { version = "0.4.22", optional = true }
parking_lot = { version = "0.12", optional = true }
moka = { version = "0.12.3", optional = true, features = ["future"] }
openssl = { version = "0.10.57", optional = true } # 0.10.57 upgrades to 'bitflags' 2.x
Expand All @@ -45,7 +46,7 @@ smallvec = { version = "1.3", optional = true }
tokio = { version = "1.33", optional = true, features = ["io-util", "macros", "net", "time", "sync", "rt-multi-thread" ] }
tokio-rustls = { version = "0.26", optional = true, default-features = false }
tokio-stream = { version = "0.1.1", optional = true }
tracing = { version = "0.1.40", optional = true }
tracing = { version = "0.1.40", optional = true, features = ["log"] }
tracing-subscriber = { version = "0.3.18", optional = true, features = ["env-filter"] }

[features]
Expand All @@ -57,6 +58,7 @@ heapless = ["dep:heapless", "octseq/heapless"]
serde = ["dep:serde", "octseq/serde"]
smallvec = ["dep:smallvec", "octseq/smallvec"]
std = ["dep:hashbrown", "bytes?/std", "octseq/std", "time/std"]
tracing = ["dep:log", "dep:tracing"]

# Cryptographic backends
ring = ["dep:ring"]
Expand Down
8 changes: 4 additions & 4 deletions src/net/server/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::Arc;

use arc_swap::ArcSwap;
use futures_util::StreamExt;
use log::{log_enabled, Level};
use octseq::Octets;
use tokio::io::{
AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadHalf, WriteHalf,
Expand All @@ -17,8 +18,7 @@ use tokio::sync::mpsc::error::TrySendError;
use tokio::sync::{mpsc, watch};
use tokio::time::Instant;
use tokio::time::{sleep_until, timeout};
use tracing::Level;
use tracing::{debug, enabled, error, trace, warn};
use tracing::{debug, error, trace, warn};

use crate::base::message_builder::AdditionalBuilder;
use crate::base::wire::Composer;
Expand Down Expand Up @@ -582,7 +582,7 @@ where
&mut self,
msg: StreamTarget<Svc::Target>,
) -> Result<(), ConnectionEvent> {
if enabled!(Level::TRACE) {
if log_enabled!(Level::Trace) {
let bytes = msg.as_dgram_slice();
let pcap_text = to_pcap_text(bytes, bytes.len());
trace!(addr = %self.addr, pcap_text, "Sending response");
Expand Down Expand Up @@ -647,7 +647,7 @@ where
Ok(buf) => {
let received_at = Instant::now();

if enabled!(Level::TRACE) {
if log_enabled!(Level::Trace) {
let pcap_text = to_pcap_text(&buf, buf.as_ref().len());
trace!(addr = %self.addr, pcap_text, "Received message");
}
Expand Down
9 changes: 4 additions & 5 deletions src/net/server/dgram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::sync::{Arc, Mutex};

use arc_swap::ArcSwap;
use futures_util::stream::StreamExt;
use log::{log_enabled, Level};
use octseq::Octets;
use tokio::io::ReadBuf;
use tokio::net::UdpSocket;
Expand All @@ -30,9 +31,7 @@ use tokio::time::interval;
use tokio::time::timeout;
use tokio::time::Instant;
use tokio::time::MissedTickBehavior;
use tracing::warn;
use tracing::Level;
use tracing::{enabled, error, trace};
use tracing::{error, trace, warn};

use crate::base::wire::Composer;
use crate::base::Message;
Expand Down Expand Up @@ -496,7 +495,7 @@ where
let received_at = Instant::now();
self.metrics.inc_num_received_requests();

if enabled!(Level::TRACE) {
if log_enabled!(Level::Trace) {
let pcap_text = to_pcap_text(&buf, bytes_read);
trace!(%addr, pcap_text, "Received message");
}
Expand Down Expand Up @@ -553,7 +552,7 @@ where
let bytes = target.as_dgram_slice();

// Logging
if enabled!(Level::TRACE) {
if log_enabled!(Level::Trace) {
let pcap_text = to_pcap_text(bytes, bytes.len());
trace!(%addr, pcap_text, "Sending response");
}
Expand Down
9 changes: 5 additions & 4 deletions src/net/server/middleware/edns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use core::marker::PhantomData;
use core::ops::ControlFlow;

use futures_util::stream::{once, Once, Stream};
use log::{log_enabled, Level};
use octseq::Octets;
use tracing::{debug, enabled, error, trace, warn, Level};
use tracing::{debug, error, trace, warn};

use crate::base::iana::OptRcode;
use crate::base::message_builder::AdditionalBuilder;
Expand Down Expand Up @@ -141,7 +142,7 @@ where
// "A DNS server that receives a query using UDP
// transport that includes the edns-tcp-keepalive
// option MUST ignore the option."
if enabled!(Level::DEBUG)
if log_enabled!(Level::Debug)
&& opt_rec.opt().tcp_keepalive().is_some()
{
debug!("RFC 7828 3.2.1 violation: ignoring edns-tcp-keepalive option received via UDP");
Expand All @@ -161,7 +162,7 @@ where
let requestors_udp_payload_size =
opt_rec.udp_payload_size();

if enabled!(Level::DEBUG)
if log_enabled!(Level::Debug)
&& requestors_udp_payload_size
< MINIMUM_RESPONSE_BYTE_LEN
{
Expand Down Expand Up @@ -200,7 +201,7 @@ where
None => clamped_requestors_udp_payload_size,
};

if enabled!(Level::TRACE) {
if log_enabled!(Level::Trace) {
trace!("EDNS(0) response size negotation concluded: client requested={}, server requested={:?}, chosen value={}",
opt_rec.udp_payload_size(), server_max_response_size_hint, negotiated_hint);
}
Expand Down

0 comments on commit b69b2cd

Please sign in to comment.