Skip to content

Commit

Permalink
add a debug_span for dns resolution to disambiguate metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Jul 19, 2024
1 parent 9087273 commit 0f690ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ http-body-util = "0.1.0"
tokio = { version = "1", features = ["macros", "test-util", "signal"] }
tokio-test = "0.4"
pretty_env_logger = "0.5"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dev-dependencies]
pnet_datalink = "0.35.0"
Expand Down
17 changes: 16 additions & 1 deletion examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,23 @@ use std::env;
use http_body_util::Empty;
use hyper::Request;
use hyper_util::client::legacy::{connect::HttpConnector, Client};
use tracing::{info_span, Instrument};
use tracing_subscriber::{
fmt::{self, format::FmtSpan},
prelude::*,
EnvFilter,
};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let fmt_layer = fmt::layer()
.with_span_events(FmtSpan::CLOSE) // show time elapsed in spans
.with_timer(fmt::time::Uptime::default());
tracing_subscriber::registry()
.with(EnvFilter::from_default_env())
.with(fmt_layer)
.init();

let url = match env::args().nth(1) {
Some(url) => url,
None => {
Expand All @@ -28,7 +42,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.uri(url)
.body(Empty::<bytes::Bytes>::new())?;

let resp = client.request(req).await?;
let span = info_span!("request", uri = %req.uri());
let resp = client.request(req).instrument(span).await?;

eprintln!("{:?} {:?}", resp.version(), resp.status());
eprintln!("{:#?}", resp.headers());
Expand Down
8 changes: 4 additions & 4 deletions src/client/legacy/connect/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::{fmt, io, vec};

use tokio::task::JoinHandle;
use tower_service::Service;
use tracing::debug;
use tracing::{debug, debug_span};

pub(super) use self::sealed::Resolve;

Expand Down Expand Up @@ -118,10 +118,10 @@ impl Service<Name> for GaiResolver {
}

fn call(&mut self, name: Name) -> Self::Future {
let current_span = tracing::Span::current();
let span = debug_span!("resolve", host = %name.host).or_current();
let blocking = tokio::task::spawn_blocking(move || {
let _enter = current_span.enter();
debug!("resolving host={:?}", name.host);
let _enter = span.enter();
debug!(host = name.host, "resolving");
(&*name.host, 0)
.to_socket_addrs()
.map(|i| SocketAddrs { iter: i })
Expand Down

0 comments on commit 0f690ae

Please sign in to comment.