Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trace: Apache Common Log Format access logging #1319

Merged
merged 64 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
41fd82d
Add access logging as requested in linkerd/linkerd2#1913
tustvold Jul 20, 2020
1b7a3cb
Merge branch 'main' into pr/tustvold/601
olix0r May 14, 2021
acbe77e
fixup pin-project dependency
olix0r May 14, 2021
11d2076
fixup fuzzer cargo lock files
olix0r May 14, 2021
cd80bf4
review feedback
tustvold May 17, 2021
d4c5337
misc style cleanup
hawkw May 17, 2021
e9c5892
enable/disable with env var
hawkw May 18, 2021
6b5fb78
add client info to access log
hawkw May 18, 2021
dce15dd
ensure the tracing filter enables access log spans
hawkw May 18, 2021
4d76632
change access log spans to `INFO`
hawkw May 18, 2021
0990eb9
review feedback
tustvold May 31, 2021
126ffc2
Merge remote-tracking branch 'upstream/main' into access-logging
tustvold May 31, 2021
c46bf15
load access log setting from environment
tustvold May 31, 2021
b5de8d8
remove filtering from AccessLogWriter layer
tustvold May 31, 2021
d5fb34b
fix replay test compilation
tustvold May 31, 2021
93af5d9
update fuzz lockfiles
tustvold Jun 2, 2021
da3332f
Merge remote-tracking branch 'upstream/main' into access-logging
tustvold Jun 2, 2021
777e0e5
mark inline; use CONTENT_LENGTH constant
olix0r Jun 2, 2021
449fef0
tracing: Simplify initialization
olix0r Jun 2, 2021
88bbd3b
clone golf
olix0r Jun 2, 2021
892bc63
clippay
olix0r Jun 2, 2021
f829918
simplify env filter construction
olix0r Jun 2, 2021
992647e
malkovich
olix0r Jun 2, 2021
ebf7813
Merge branch 'ver/tracing-functions' into pr/tustvold/601
olix0r Jun 2, 2021
c53ce37
Move struct defintions to the top
olix0r Jun 2, 2021
72132a3
Merge branch 'ver/tracing-functions' into pr/tustvold/601
olix0r Jun 2, 2021
4b56f3d
Reintroduce access logging with a TODO
olix0r Jun 3, 2021
cf3440d
Merge branch 'main' into pr/tustvold/601
olix0r Jun 3, 2021
af63421
Split the access-log module and the log writer
olix0r Jun 3, 2021
b97ac8e
Constify the special tracing target for access logs
olix0r Jun 3, 2021
705d437
+inline
olix0r Jun 3, 2021
ad9b54c
lowercase headers
olix0r Jun 3, 2021
2b7bd42
Only capture time when tracing is enabled
olix0r Jun 3, 2021
2ac1804
Make the access log conditional
olix0r Jun 3, 2021
53e45b8
fuzzer deps
olix0r Jun 3, 2021
b6005d9
Only app::inbound need depend on access-log
olix0r Jun 3, 2021
bf48a56
Rename the module to linkerd-http-access-log
olix0r Jun 3, 2021
da3d1ae
forbid unsafe code
olix0r Jun 3, 2021
65d372a
Merge branch 'main' into pr/tustvold/601
olix0r Jun 17, 2021
72ecb24
Merge branch 'main' into access-logging
hawkw Oct 7, 2021
9e5ecf6
wip plf integration
hawkw Oct 8, 2021
219b220
tracing: simplify subscriber construction with `Box`ed layers
hawkw Oct 8, 2021
2d20daa
Merge branch 'eliza/box-layers' into access-logging
hawkw Oct 8, 2021
00d56ed
finish wiring it up
hawkw Oct 8, 2021
f69fbdf
...actually we should probably always filter out
hawkw Oct 8, 2021
af72a69
allow enabling the access log in tests
hawkw Oct 8, 2021
b89dc5f
hmm so having a layer filter there breaks it?
hawkw Oct 8, 2021
e86e64d
make access log layer format its own fields
hawkw Oct 8, 2021
69c4cef
implement apache common log format
hawkw Oct 14, 2021
6968620
Merge branch 'main' into eliza/access-logging-2
hawkw Oct 14, 2021
c391b7b
Update linkerd/http-access-log/src/lib.rs
hawkw Oct 20, 2021
00b7235
Merge branch 'main' into eliza/access-logging-2
hawkw Oct 28, 2021
21e2a78
remove `chrono` dep
hawkw Oct 28, 2021
128d4a1
update access log to `tracing-subscriber` 0.3
hawkw Oct 28, 2021
4da351a
sort deps
hawkw Oct 28, 2021
b204ea0
fix unused field warning
hawkw Oct 28, 2021
6117f72
Merge branch 'main' into eliza/access-logging-2
olix0r Nov 3, 2021
b1ceafd
Merge branch 'main' into eliza/access-logging-2
olix0r Nov 5, 2021
7bf594e
Merge branch 'main' into eliza/access-logging-2
hawkw Jan 18, 2022
6985abe
trace: write the access log to stderr
hawkw Jan 18, 2022
8f37ccc
move env configuration out of access_log module
hawkw Jan 19, 2022
74226c3
rm dead code
hawkw Jan 19, 2022
52d6d0a
rm unneeded futures deps
hawkw Jan 19, 2022
6c6a8a5
add some more comments
hawkw Jan 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,12 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6456b8a6c8f33fee7d958fcd1b60d55b11940a79e63ae87013e6d22e26034440"

[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"

[[package]]
name = "hyper"
version = "0.14.16"
Expand Down Expand Up @@ -867,6 +873,7 @@ dependencies = [
"libfuzzer-sys",
"linkerd-app-core",
"linkerd-app-test",
"linkerd-http-access-log",
"linkerd-io",
"linkerd-meshtls",
"linkerd-meshtls-rustls",
Expand Down Expand Up @@ -1061,6 +1068,22 @@ dependencies = [
"tokio",
]

[[package]]
name = "linkerd-http-access-log"
version = "0.1.0"
dependencies = [
"futures-core",
"http",
"humantime",
"linkerd-identity",
"linkerd-proxy-transport",
"linkerd-stack",
"linkerd-tls",
"linkerd-tracing",
"pin-project",
"tracing",
]

[[package]]
name = "linkerd-http-box"
version = "0.1.0"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"linkerd/errno",
"linkerd/error-respond",
"linkerd/exp-backoff",
"linkerd/http-access-log",
"linkerd/http-box",
"linkerd/http-classify",
"linkerd/http-metrics",
Expand Down
1 change: 1 addition & 0 deletions linkerd/app/inbound/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bytes = "1"
http = "0.2"
futures = { version = "0.3", default-features = false }
linkerd-app-core = { path = "../core" }
linkerd-http-access-log = { path = "../../http-access-log" }
linkerd-server-policy = { path = "../../server-policy" }
linkerd-tonic-watch = { path = "../../tonic-watch" }
linkerd2-proxy-api = { version = "0.3", features = ["client", "inbound"] }
Expand Down
7 changes: 5 additions & 2 deletions linkerd/app/inbound/src/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ use linkerd_app_core::{
proxy::http,
svc::{self, ExtractParam, Param},
tls,
transport::OrigDstAddr,
transport::{ClientAddr, OrigDstAddr, Remote},
Error, Result,
};
use linkerd_http_access_log::NewAccessLog;
use tracing::debug_span;

#[derive(Copy, Clone, Debug)]
Expand All @@ -26,7 +27,8 @@ impl<H> Inbound<H> {
+ Param<http::normalize_uri::DefaultAuthority>
+ Param<tls::ConditionalServerTls>
+ Param<ServerLabel>
+ Param<OrigDstAddr>,
+ Param<OrigDstAddr>
+ Param<Remote<ClientAddr>>,
T: Clone + Send + Unpin + 'static,
I: io::AsyncRead + io::AsyncWrite + io::PeerAddr + Send + Unpin + 'static,
H: svc::NewService<T, Service = HSvc> + Clone + Send + Sync + Unpin + 'static,
Expand Down Expand Up @@ -79,6 +81,7 @@ impl<H> Inbound<H> {
.push(http::BoxResponse::layer()),
)
.check_new_service::<T, http::Request<_>>()
.push(NewAccessLog::layer())
.instrument(|t: &T| debug_span!("http", v = %Param::<Version>::param(t)))
.push(http::NewServeHttp::layer(h2_settings, rt.drain.clone()))
.push_on_service(svc::BoxService::layer())
Expand Down
19 changes: 19 additions & 0 deletions linkerd/http-access-log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "linkerd-http-access-log"
version = "0.1.0"
authors = ["Linkerd Developers <[email protected]>"]
license = "Apache-2.0"
edition = "2018"
publish = false

[dependencies]
futures-core = "0.3"
http = "0.2"
humantime = "2"
pin-project = "1"
linkerd-stack = { path = "../stack" }
linkerd-identity = { path = "../identity" }
linkerd-tls = { path = "../tls" }
linkerd-proxy-transport = { path = "../proxy/transport" }
linkerd-tracing = { path = "../tracing" }
tracing = "0.1.19"
207 changes: 207 additions & 0 deletions linkerd/http-access-log/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#![deny(warnings, rust_2018_idioms)]
#![forbid(unsafe_code)]

use futures_core::TryFuture;
use linkerd_identity as identity;
use linkerd_proxy_transport::{ClientAddr, Remote};
use linkerd_stack as svc;
use linkerd_tls as tls;
use linkerd_tracing::access_log::TRACE_TARGET;
use pin_project::pin_project;
use std::{
future::Future,
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
time::{Duration, Instant, SystemTime},
};
use svc::{NewService, Param};
use tracing::{field, span, Level, Span};

#[derive(Clone, Debug)]
pub struct NewAccessLog<N> {
inner: N,
}

#[derive(Clone, Debug)]
pub struct AccessLogContext<S> {
inner: S,
client_addr: SocketAddr,
client_id: Option<identity::Name>,
}

struct ResponseFutureInner {
span: Span,
start: Instant,
processing: Duration,
}

#[pin_project]
pub struct AccessLogFuture<F> {
data: Option<ResponseFutureInner>,

#[pin]
inner: F,
}

impl<N> NewAccessLog<N> {
/// Returns a new `NewAccessLog` layer that wraps an inner service with
/// access logging middleware.
///
/// The access log is recorded by adding a `tracing` span to the service's
/// future. If access logging is not enabled by the `tracing` subscriber,
/// this span will never be enabled, and it can be skipped cheaply. When
/// access logging *is* enabled, additional data will be recorded when the
/// response future completes.
///
/// Recording the access log will introduce additional overhead in the
/// request path, but this is largely avoided when access logging is not
/// enabled.
#[inline]
pub fn layer() -> impl svc::layer::Layer<N, Service = Self> {
svc::layer::mk(|inner| NewAccessLog { inner })
}
}

impl<N, T> NewService<T> for NewAccessLog<N>
where
T: Param<tls::ConditionalServerTls> + Param<Remote<ClientAddr>>,
N: NewService<T>,
{
type Service = AccessLogContext<N::Service>;

fn new_service(&self, target: T) -> Self::Service {
let Remote(ClientAddr(client_addr)) = target.param();
let tls: tls::ConditionalServerTls = target.param();
let client_id = tls
.value()
.and_then(|tls| tls.client_id().map(|tls::ClientId(name)| name.clone()));
let inner = self.inner.new_service(target);
AccessLogContext {
inner,
client_addr,
client_id,
}
}
}

impl<S, B1, B2> svc::Service<http::Request<B1>> for AccessLogContext<S>
where
S: svc::Service<http::Request<B1>, Response = http::Response<B2>>,
{
type Response = S::Response;
type Error = S::Error;
type Future = AccessLogFuture<S::Future>;

#[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), S::Error>> {
self.inner.poll_ready(cx)
}

fn call(&mut self, request: http::Request<B1>) -> Self::Future {
let get_header = |name: http::header::HeaderName| {
request
.headers()
.get(name)
.and_then(|x| x.to_str().ok())
.unwrap_or_default()
};

let trace_id = || {
let headers = request.headers();
headers
.get("x-b3-traceid")
.or_else(|| headers.get("x-request-id"))
.or_else(|| headers.get("x-amzn-trace-id"))
.and_then(|x| x.to_str().ok())
.unwrap_or_default()
};

let span = span!(target: TRACE_TARGET, Level::INFO, "http",
client.addr = %self.client_addr,
client.id = self.client_id.as_ref().map(|n| n.as_str()).unwrap_or("-"),
timestamp = %now(),
method = request.method().as_str(),
uri = %request.uri(),
version = ?request.version(),
trace_id = trace_id(),
request_bytes = get_header(http::header::CONTENT_LENGTH),
status = field::Empty,
response_bytes = field::Empty,
total_ns = field::Empty,
processing_ns = field::Empty,
user_agent = get_header(http::header::USER_AGENT),
host = get_header(http::header::HOST),
);

// The access log span is only enabled by the `tracing` subscriber if
// access logs are being recorded. If it's disabled, we can skip
// recording additional data in the response future.
if span.is_disabled() {
return AccessLogFuture {
data: None,
inner: self.inner.call(request),
};
}

AccessLogFuture {
data: Some(ResponseFutureInner {
span,
start: Instant::now(),
processing: Duration::from_secs(0),
}),
inner: self.inner.call(request),
}
}
}

impl<F, B2> Future for AccessLogFuture<F>
where
F: TryFuture<Ok = http::Response<B2>>,
{
type Output = Result<F::Ok, F::Error>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();

let data: &mut ResponseFutureInner = match &mut this.data {
Some(data) => data,
None => return this.inner.try_poll(cx),
};

let _enter = data.span.enter();
let poll_start = Instant::now();

let response: http::Response<B2> = match this.inner.try_poll(cx) {
Poll::Pending => {
data.processing += Instant::now().duration_since(poll_start);
return Poll::Pending;
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Ready(Ok(response)) => response,
};

let now = Instant::now();
let total_ns = now.duration_since(data.start).as_nanos();
let processing_ns = (now.duration_since(poll_start) + data.processing).as_nanos();

let span = &data.span;

response
.headers()
.get(http::header::CONTENT_LENGTH)
.and_then(|x| x.to_str().ok())
.map(|x| span.record("response_bytes", &x));

span.record("status", &response.status().as_u16());
span.record("total_ns", &field::display(total_ns));
span.record("processing_ns", &field::display(processing_ns));

Poll::Ready(Ok(response))
}
}

#[inline]
fn now() -> humantime::Rfc3339Timestamp {
humantime::format_rfc3339(SystemTime::now())
}
2 changes: 1 addition & 1 deletion linkerd/http-retry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ mod tests {
tx: Tx(tx),
initial,
replay,
_trace: linkerd_tracing::test::with_default_filter("linkerd_http_retry=debug"),
_trace: linkerd_tracing::test::with_default_filter("linkerd_http_retry=debug").0,
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions linkerd/tls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,17 @@ impl fmt::Display for NoServerTls {
}
}

// === impl ServerTls ===

impl ServerTls {
pub fn client_id(&self) -> Option<&ClientId> {
match self {
ServerTls::Established { ref client_id, .. } => client_id.as_ref(),
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 2 additions & 1 deletion linkerd/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ tracing-log = "0.1.2"

[dependencies.tracing-subscriber]
version = "0.3"
features = ["env-filter","smallvec", "tracing-log", "json", "parking_lot"]
default-features = false
features = ["env-filter", "fmt", "smallvec", "tracing-log", "json", "parking_lot", "registry"]

Loading