Skip to content

Commit b0c7181

Browse files
authored
Adopt tokio-tracing contexts (#374)
We had our own poorly-implemented logging contexts. This change replaces all uses of the old context implementation with `tracing` (usually via `tracing-futures`).
1 parent 9978b32 commit b0c7181

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+834
-1039
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ dependencies = [
646646
"tower-spawn-ready 0.1.0 (git+https://github.com/tower-rs/tower)",
647647
"tower-util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
648648
"tracing 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
649-
"tracing-futures 0.0.1-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)",
649+
"tracing-futures 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
650650
"tracing-log 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
651651
"tracing-subscriber 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
652652
"trust-dns-resolver 0.10.2 (git+https://github.com/bluejekyll/trust-dns?rev=7c8a0739dad495bf5a4fddfe86b8bbe2aa52d060)",
@@ -711,7 +711,7 @@ dependencies = [
711711
"tower 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
712712
"tower-util 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
713713
"tracing 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
714-
"tracing-futures 0.0.1-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)",
714+
"tracing-futures 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
715715
]
716716

717717
[[package]]
@@ -1916,7 +1916,7 @@ dependencies = [
19161916

19171917
[[package]]
19181918
name = "tracing-futures"
1919-
version = "0.0.1-alpha.1"
1919+
version = "0.1.0"
19201920
source = "registry+https://github.com/rust-lang/crates.io-index"
19211921
dependencies = [
19221922
"futures 0.1.26 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2402,7 +2402,7 @@ dependencies = [
24022402
"checksum tracing 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c21ff9457accc293386c20e8f754d0b059e67e325edf2284f04230d125d7e5ff"
24032403
"checksum tracing-attributes 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5e27d1065a1de5d8ad2637e41fe14d3cd14363d4a20cb99090b9012004955637"
24042404
"checksum tracing-core 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "528c8ebaaa16cdac34795180b046c031775b0d56402704d98c096788f33d646a"
2405-
"checksum tracing-futures 0.0.1-alpha.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08c7446f4fb35df7ba2c537b7e2f812f91b20a58aa2b846f028342c4d2429be0"
2405+
"checksum tracing-futures 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4d73cd4d483f6a9af4f9f6da37c1c162e1f99017ba708a3042e6e90e54d26e5c"
24062406
"checksum tracing-log 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc99e1286541d6ccc42d5fb37213d1cdde544f88b19fac3d94e3117b55163"
24072407
"checksum tracing-subscriber 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c63b1771a75314374703309107e685805628c04643e6dd2c7a5cf8f94348c62e"
24082408
"checksum trust-dns-proto 0.6.0 (git+https://github.com/bluejekyll/trust-dns?rev=7c8a0739dad495bf5a4fddfe86b8bbe2aa52d060)" = "<none>"

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ trust-dns-resolver = { git = "https://github.com/bluejekyll/trust-dns", rev = "7
105105

106106
# tracing
107107
tracing = "0.1.9"
108-
tracing-futures = "0.0.1-alpha.1"
108+
tracing-futures = "0.1"
109109
tracing-log = "0.1"
110110

111111
# tls

lib/linkerd2-proxy-core/src/listen.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ pub trait Listen {
99

1010
fn poll_accept(&mut self) -> Poll<Self::Connection, Self::Error>;
1111

12-
fn serve<A: Accept<Self::Connection>>(self, accept: A) -> Serve<Self, A>
12+
fn serve<A: Accept<Self::Connection>>(
13+
self,
14+
accept: A,
15+
) -> Serve<Self, A, tokio::executor::DefaultExecutor>
1316
where
1417
Self: Sized,
15-
Serve<Self, A>: Future,
18+
Serve<Self, A, tokio::executor::DefaultExecutor>: Future,
1619
{
1720
Serve {
1821
listen: self,
1922
accept,
23+
executor: tokio::executor::DefaultExecutor::current(),
2024
}
2125
}
2226
}
@@ -50,26 +54,43 @@ where
5054
}
5155
}
5256

53-
pub struct Serve<L, A> {
57+
pub struct Serve<L, A, E> {
5458
listen: L,
5559
accept: A,
60+
executor: E,
5661
}
5762

58-
impl<L, A> Future for Serve<L, A>
63+
impl<L, A> Serve<L, A, tokio::executor::DefaultExecutor>
5964
where
6065
L: Listen,
6166
A: Accept<L::Connection, Error = Never>,
6267
A::Future: Send + 'static,
68+
{
69+
pub fn with_executor<E: tokio::executor::Executor>(self, executor: E) -> Serve<L, A, E> {
70+
Serve {
71+
listen: self.listen,
72+
accept: self.accept,
73+
executor,
74+
}
75+
}
76+
}
77+
78+
impl<L, A, E> Future for Serve<L, A, E>
79+
where
80+
L: Listen,
81+
A: Accept<L::Connection, Error = Never>,
82+
A::Future: Send + 'static,
83+
E: tokio::executor::Executor,
6384
{
6485
type Item = Never;
65-
type Error = L::Error;
86+
type Error = Error;
6687

6788
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
6889
loop {
69-
try_ready!(self.accept.poll_ready().map_err(|e| match e {}));
70-
let conn = try_ready!(self.listen.poll_accept());
71-
72-
tokio::spawn(self.accept.accept(conn).map_err(|e| match e {}));
90+
try_ready!(self.accept.poll_ready());
91+
let conn = try_ready!(self.listen.poll_accept().map_err(Into::into));
92+
let accept = self.accept.accept(conn).map_err(|e| match e {});
93+
self.executor.spawn(Box::new(accept)).map_err(Error::from)?;
7394
}
7495
}
7596
}

lib/linkerd2-proxy-discover/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ indexmap = "1.0"
1414
tokio = "0.1"
1515
tower = "0.1"
1616
tracing = "0.1"
17-
tracing-futures = "0.0.1-alpha.1"
17+
tracing-futures = "0.1"
1818

1919
[dev-dependencies]
2020
tower-util = "0.1"

lib/linkerd2-proxy-transport/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ mod listen;
88
pub mod metrics;
99
mod peek;
1010
mod prefixed;
11+
mod source;
1112
pub mod tls;
1213

1314
pub use self::{
1415
addr_info::{AddrInfo, GetOriginalDst, SoOriginalDst},
1516
io::BoxedIo,
1617
listen::Listen,
1718
peek::Peek,
19+
source::Source,
1820
tls::Connection,
1921
};
2022

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use super::tls::PeerIdentity;
2+
use std::net::SocketAddr;
3+
4+
/// Describes an accepted connection.
5+
#[derive(Clone, Debug)]
6+
pub struct Source {
7+
pub remote: SocketAddr,
8+
pub local: SocketAddr,
9+
pub orig_dst: Option<SocketAddr>,
10+
pub tls_peer: PeerIdentity,
11+
}
12+
13+
impl Source {
14+
pub fn orig_dst_if_not_local(&self) -> Option<SocketAddr> {
15+
self.orig_dst.and_then(|orig_dst| {
16+
// If the original destination is actually the listening socket,
17+
// we don't want to create a loop.
18+
if Self::same_addr(orig_dst, self.local) {
19+
None
20+
} else {
21+
Some(orig_dst)
22+
}
23+
})
24+
}
25+
26+
fn same_addr(a0: SocketAddr, a1: SocketAddr) -> bool {
27+
use std::net::IpAddr::{V4, V6};
28+
(a0.port() == a1.port())
29+
&& match (a0.ip(), a1.ip()) {
30+
(V6(a0), V4(a1)) => a0.to_ipv4() == Some(a1),
31+
(V4(a0), V6(a1)) => Some(a0) == a1.to_ipv4(),
32+
(a0, a1) => (a0 == a1),
33+
}
34+
}
35+
}
36+
37+
// for logging context
38+
impl std::fmt::Display for Source {
39+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40+
self.remote.fmt(f)
41+
}
42+
}

src/app/config.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::control::ControlAddr;
22
use super::identity;
33
use crate::addr::{self, Addr};
4+
pub use crate::proxy::http::h2::Settings as H2Settings;
45
use crate::transport::tls;
56
use crate::{dns, Conditional};
67
use indexmap::IndexSet;
@@ -143,12 +144,6 @@ pub struct Config {
143144
pub h2_settings: H2Settings,
144145
}
145146

146-
#[derive(Copy, Clone, Debug, Default)]
147-
pub struct H2Settings {
148-
pub initial_stream_window_size: Option<u32>,
149-
pub initial_connection_window_size: Option<u32>,
150-
}
151-
152147
/// Configuration settings for the tap server
153148
#[derive(Debug)]
154149
pub struct TapSettings {

src/app/control.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::{transport::tls, Addr};
1+
use linkerd2_addr::Addr;
22
use std::fmt;
33

44
#[derive(Clone, Debug)]
55
pub struct ControlAddr {
66
pub addr: Addr,
7-
pub identity: tls::PeerIdentity,
7+
pub identity: crate::transport::tls::PeerIdentity,
88
}
99

1010
impl fmt::Display for ControlAddr {
@@ -16,9 +16,10 @@ impl fmt::Display for ControlAddr {
1616
/// Sets the request's URI from `Config`.
1717
pub mod add_origin {
1818
use super::ControlAddr;
19-
use crate::{svc, Error};
19+
use crate::svc;
2020
use futures::try_ready;
2121
use futures::{Future, Poll};
22+
use linkerd2_error::Error;
2223
use std::marker::PhantomData;
2324
use tower_request_modifier::{Builder, RequestModifier};
2425

@@ -145,10 +146,14 @@ pub mod add_origin {
145146
/// Resolves the controller's `addr` once before building a client.
146147
pub mod resolve {
147148
use super::{client, ControlAddr};
148-
use crate::{dns, logging, svc, Addr};
149+
use crate::dns;
150+
use crate::svc;
149151
use futures::{try_ready, Future, Poll};
152+
use linkerd2_addr::Addr;
150153
use std::net::SocketAddr;
151154
use std::{error, fmt};
155+
use tracing::info_span;
156+
use tracing_futures::{Instrument, Instrumented};
152157

153158
#[derive(Clone, Debug)]
154159
pub struct Layer {
@@ -177,7 +182,7 @@ pub mod resolve {
177182
config: ControlAddr,
178183
stack: M,
179184
},
180-
Inner(M::Future),
185+
Inner(Instrumented<M::Future>),
181186
}
182187

183188
#[derive(Debug)]
@@ -263,10 +268,10 @@ pub mod resolve {
263268
let target = client::Target {
264269
addr,
265270
server_name: dst.identity.clone(),
266-
log_ctx: logging::admin().client("control", dst.addr.clone()),
267271
};
268272

269-
State::Inner(mk_svc.call(target))
273+
info_span!("control", peer.addr = %addr, peer.identity = ?dst.identity)
274+
.in_scope(move || State::Inner(mk_svc.call(target).in_current_span()))
270275
}
271276
}
272277

@@ -289,15 +294,14 @@ pub mod resolve {
289294
pub mod client {
290295
use super::super::config::H2Settings;
291296
use crate::transport::{connect, tls};
292-
use crate::{logging, proxy::http, svc, task, Addr};
297+
use crate::{proxy::http, svc};
293298
use futures::Poll;
294299
use std::net::SocketAddr;
295300

296301
#[derive(Clone, Debug)]
297302
pub struct Target {
298303
pub(super) addr: SocketAddr,
299304
pub(super) server_name: tls::PeerIdentity,
300-
pub(super) log_ctx: logging::Client<&'static str, Addr>,
301305
}
302306

303307
#[derive(Debug)]
@@ -326,7 +330,7 @@ pub mod client {
326330
http::h2::Connect<C, B>: svc::Service<Target>,
327331
{
328332
svc::layer::mk(|mk_conn| {
329-
let inner = http::h2::Connect::new(mk_conn, task::LazyExecutor, H2Settings::default());
333+
let inner = http::h2::Connect::new(mk_conn, H2Settings::default());
330334
Client { inner }
331335
})
332336
}
@@ -341,13 +345,13 @@ pub mod client {
341345
type Error = <http::h2::Connect<C, B> as svc::Service<Target>>::Error;
342346
type Future = <http::h2::Connect<C, B> as svc::Service<Target>>::Future;
343347

348+
#[inline]
344349
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
345350
self.inner.poll_ready()
346351
}
347352

353+
#[inline]
348354
fn call(&mut self, target: Target) -> Self::Future {
349-
let exe = target.log_ctx.clone().with_remote(target.addr).executor();
350-
self.inner.set_executor(exe);
351355
self.inner.call(target)
352356
}
353357
}

src/app/errors.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Layer to map HTTP service errors into appropriate `http::Response`s.
22
3-
use crate::{proxy::http::HasH2Reason, svc, Error};
3+
use crate::proxy::http::HasH2Reason;
4+
use crate::svc;
45
use futures::{Future, Poll};
56
use http::{header, Request, Response, StatusCode, Version};
7+
use linkerd2_error::Error;
68
use tracing::{debug, error, warn};
79

810
/// Layer to map HTTP service errors into appropriate `http::Response`s.
@@ -27,6 +29,12 @@ pub struct ResponseFuture<F> {
2729
is_http2: bool,
2830
}
2931

32+
#[derive(Clone, Debug)]
33+
pub struct StatusError {
34+
pub status: http::StatusCode,
35+
pub message: String,
36+
}
37+
3038
impl<M> svc::Layer<M> for Layer {
3139
type Service = Stack<M>;
3240

@@ -107,7 +115,6 @@ where
107115
}
108116

109117
fn map_err_to_5xx(e: Error) -> StatusCode {
110-
use crate::app::outbound;
111118
use crate::proxy::buffer;
112119
use crate::proxy::http::router::error as router;
113120
use tower::load_shed::error as shed;
@@ -124,12 +131,20 @@ fn map_err_to_5xx(e: Error) -> StatusCode {
124131
} else if let Some(_) = e.downcast_ref::<router::NotRecognized>() {
125132
error!("could not recognize request");
126133
http::StatusCode::BAD_GATEWAY
127-
} else if let Some(err) = e.downcast_ref::<outbound::RequireIdentityError>() {
128-
error!("{}", err);
129-
http::StatusCode::FORBIDDEN
134+
} else if let Some(err) = e.downcast_ref::<StatusError>() {
135+
error!(%err.status, %err.message);
136+
err.status
130137
} else {
131138
// we probably should have handled this before?
132139
error!("unexpected error: {}", e);
133140
http::StatusCode::BAD_GATEWAY
134141
}
135142
}
143+
144+
impl std::fmt::Display for StatusError {
145+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146+
self.message.fmt(f)
147+
}
148+
}
149+
150+
impl std::error::Error for StatusError {}

0 commit comments

Comments
 (0)