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

access-log: Add JSON support #1350

Merged
merged 64 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 60 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
a2257ec
access-log: Add JSON support
hawkw Nov 1, 2021
66b496f
Merge branch 'main' into eliza/access-log-json
hawkw Jan 21, 2022
5a4aeff
update JSON access log formatter
hawkw Jan 21, 2022
381e013
allow enabling access logging in tests
hawkw Jan 21, 2022
bc19360
remove unused `tracing-appender` dep
hawkw Jan 21, 2022
9885a6d
make access log format parsing infallible
hawkw Jan 21, 2022
e5af176
Update linkerd/tracing/src/lib.rs
hawkw Jan 21, 2022
921cc58
only accept 'json' or 'apache'
hawkw Jan 21, 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
42 changes: 42 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,26 @@ dependencies = [
"cfg-if",
]

[[package]]
name = "crossbeam-channel"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
dependencies = [
"cfg-if",
"crossbeam-utils",
]

[[package]]
name = "crossbeam-utils"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db"
dependencies = [
"cfg-if",
"lazy_static",
]

[[package]]
name = "data-encoding"
version = "2.3.2"
Expand Down Expand Up @@ -1625,6 +1645,7 @@ dependencies = [
"linkerd-error",
"tokio",
"tracing",
"tracing-appender",
"tracing-log",
"tracing-subscriber",
]
Expand Down Expand Up @@ -2353,6 +2374,16 @@ dependencies = [
"once_cell",
]

[[package]]
name = "time"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99beeb0daeac2bd1e86ac2c21caddecb244b39a093594da1a661ec2060c7aedd"
dependencies = [
"itoa 0.4.8",
"libc",
]

[[package]]
name = "tinyvec"
version = "1.5.1"
Expand Down Expand Up @@ -2572,6 +2603,17 @@ dependencies = [
"tracing-core",
]

[[package]]
name = "tracing-appender"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94571df2eae3ed4353815ea5a90974a594a1792d8782ff2cbcc9392d1101f366"
dependencies = [
"crossbeam-channel",
"time",
"tracing-subscriber",
]

[[package]]
name = "tracing-attributes"
version = "0.1.18"
Expand Down
1 change: 1 addition & 0 deletions linkerd/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansi = ["tracing-subscriber/ansi"]
linkerd-error = { path = "../error" }
tokio = { version = "1", features = ["time"] }
tracing = "0.1.29"
tracing-appender = "0.2"
hawkw marked this conversation as resolved.
Show resolved Hide resolved
tracing-log = "0.1.2"

[dependencies.tracing-subscriber]
Expand Down
41 changes: 30 additions & 11 deletions linkerd/tracing/src/access_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use tracing_subscriber::{

pub const TRACE_TARGET: &str = "_access_log";

pub(super) type AccessLogLayer<S> = Filtered<Writer, FilterFn, S>;
pub(super) type AccessLogLayer<S> =
Filtered<Box<dyn Layer<S> + Send + Sync + 'static>, FilterFn, S>;

#[derive(Default)]
pub(super) struct Writer<F = ApacheCommon> {
formatter: F,
}
Expand All @@ -21,16 +23,27 @@ pub(super) struct ApacheCommon {
_p: (),
}

#[derive(Copy, Clone, Debug)]
pub(super) enum Format {
Apache,
Json,
}

struct ApacheCommonVisitor<'writer> {
res: fmt::Result,
writer: format::Writer<'writer>,
}

pub(super) fn build<S>() -> (AccessLogLayer<S>, Directive)
pub(super) fn build<S>(format: Format) -> (AccessLogLayer<S>, Directive)
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
let writer = Writer::new().with_filter(
let writer: Box<dyn Layer<S> + Send + Sync + 'static> = match format {
Format::Apache => Box::new(Writer::<ApacheCommon>::default()),
Format::Json => Box::new(Writer::<format::JsonFields>::default()),
};

let writer = writer.with_filter(
FilterFn::new(
(|meta| meta.level() == &Level::INFO && meta.target().starts_with(TRACE_TARGET))
as fn(&Metadata<'_>) -> bool,
Expand All @@ -49,14 +62,6 @@ where

// === impl Writer ===

impl Writer {
pub fn new() -> Self {
Self {
formatter: Default::default(),
}
}
}

impl<S, F> Layer<S> for Writer<F>
where
S: Subscriber + for<'span> LookupSpan<'span>,
Expand Down Expand Up @@ -154,3 +159,17 @@ impl field::Visit for ApacheCommonVisitor<'_> {
}
}
}

// === impl Format ===

impl std::str::FromStr for Format {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
s if s.eq_ignore_ascii_case("json") => Ok(Self::Json),
// Default to the Apache Common Log Format if another format isn't
// specified.
_ => Ok(Self::Apache),
}
}
}
16 changes: 9 additions & 7 deletions linkerd/tracing/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(warnings, rust_2018_idioms, clippy::disallowed_method)]
#![deny(warnings, rust_2018_idioms)]
hawkw marked this conversation as resolved.
Show resolved Hide resolved
#![forbid(unsafe_code)]

pub mod access_log;
Expand Down Expand Up @@ -29,7 +29,7 @@ const DEFAULT_LOG_FORMAT: &str = "PLAIN";
pub struct Settings {
filter: Option<String>,
format: Option<String>,
access_log: bool,
access_log: Option<access_log::Format>,
is_test: bool,
}

Expand Down Expand Up @@ -81,16 +81,18 @@ impl Settings {
Some(Self {
filter,
format: std::env::var(ENV_LOG_FORMAT).ok(),
access_log: std::env::var(ENV_ACCESS_LOG).is_ok(),
access_log: std::env::var(ENV_ACCESS_LOG)
.ok()
.and_then(|env| env.parse().ok()),
hawkw marked this conversation as resolved.
Show resolved Hide resolved
is_test: false,
})
}

fn for_test(filter: String, format: String) -> Self {
fn for_test(filter: String, format: String, access_log: Option<String>) -> Self {
Self {
filter: Some(filter),
format: Some(format),
access_log: false,
access_log: access_log.and_then(|env| env.parse().ok()),
is_test: true,
}
}
Expand Down Expand Up @@ -154,8 +156,8 @@ impl Settings {
let mut filter = EnvFilter::new(log_level);

// If access logging is enabled, build the access log layer.
let access_log = if self.access_log {
let (access_log, directive) = access_log::build();
let access_log = if let Some(format) = self.access_log {
let (access_log, directive) = access_log::build(format);
filter = filter.add_directive(directive);
Some(access_log)
} else {
Expand Down
7 changes: 6 additions & 1 deletion linkerd/tracing/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ pub fn trace_subscriber(default: impl ToString) -> (Dispatch, Handle) {
// This may fail, since the global log compat layer may have been
// initialized by another test.
let _ = init_log_compat();
Settings::for_test(log_level, log_format).build()
Settings::for_test(
log_level,
log_format,
env::var("LINKERD2_PROXY_ACCESS_LOG").ok(),
)
.build()
}

pub fn with_default_filter(
Expand Down