Skip to content

Commit a11b196

Browse files
authored
tracing: Support disabling tracing entirely (#723)
When the proxy log level is set to `off`, the proxy now skips all tracing initialization and disables the tracing admin endpoints. This allows us to minimize the overhead of tracing in benchmarks and performance-critical environments. This has a noticeable impact on _memory_. In local tests, we see `main` use around ~24MB with the log level set to `off`. With this branch, memory comes down to ~16MB.
1 parent 779c524 commit a11b196

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

linkerd/tracing/src/lib.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ type Formatter<F, E> = Layered<
3232
/// environment variable as the verbosity-level filter.
3333
pub fn init() -> Result<Handle, Error> {
3434
let log_level = env::var(ENV_LOG_LEVEL).unwrap_or(DEFAULT_LOG_LEVEL.to_string());
35-
let log_format = env::var(ENV_LOG_FORMAT).unwrap_or(DEFAULT_LOG_FORMAT.to_string());
35+
if let "OFF" = log_level.to_uppercase().trim() {
36+
return Ok(Handle(Inner::Disabled));
37+
}
3638

39+
let log_format = env::var(ENV_LOG_FORMAT).unwrap_or(DEFAULT_LOG_FORMAT.to_string());
3740
let (dispatch, handle) = with_filter_and_format(log_level, log_format);
3841

3942
// Set up log compatibility.
@@ -92,17 +95,23 @@ pub fn with_filter_and_format(
9295

9396
(
9497
dispatch,
95-
Handle {
98+
Handle(Inner::Enabled {
9699
level,
97100
tasks: tasks::Handle { tasks },
98-
},
101+
}),
99102
)
100103
}
101104

102105
#[derive(Clone)]
103-
pub struct Handle {
104-
level: level::Handle,
105-
tasks: tasks::Handle,
106+
pub struct Handle(Inner);
107+
108+
#[derive(Clone)]
109+
enum Inner {
110+
Disabled,
111+
Enabled {
112+
level: level::Handle,
113+
tasks: tasks::Handle,
114+
},
106115
}
107116

108117
// === impl Handle ===
@@ -114,14 +123,27 @@ impl Handle {
114123
&self,
115124
req: http::Request<hyper::Body>,
116125
) -> Result<http::Response<hyper::Body>, Error> {
117-
self.level.serve(req).await
126+
match self.0 {
127+
Inner::Enabled { ref level, .. } => level.serve(req).await,
128+
Inner::Disabled => Ok(Self::not_found()),
129+
}
118130
}
119131

120132
/// Serve requests for task dumps.
121133
pub async fn serve_tasks(
122134
&self,
123135
req: http::Request<hyper::Body>,
124136
) -> Result<http::Response<hyper::Body>, Error> {
125-
self.tasks.serve(req)
137+
match self.0 {
138+
Inner::Enabled { ref tasks, .. } => tasks.serve(req),
139+
Inner::Disabled => Ok(Self::not_found()),
140+
}
141+
}
142+
143+
fn not_found() -> http::Response<hyper::Body> {
144+
http::Response::builder()
145+
.status(http::StatusCode::NOT_FOUND)
146+
.body(hyper::Body::empty())
147+
.expect("Response must be valid")
126148
}
127149
}

0 commit comments

Comments
 (0)