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

Avoid potential overhead of ThreadLocal.remove in LogbackMetrics #3952

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@NonNullFields
public class LogbackMetrics implements MeterBinder, AutoCloseable {

static ThreadLocal<Boolean> ignoreMetrics = new ThreadLocal<>();
static ThreadLocal<BooleanHolder> ignoreMetrics = new ThreadLocal<>();

private final Iterable<Tag> tags;

Expand Down Expand Up @@ -123,12 +123,17 @@ public void bindTo(MeterRegistry registry) {
* runnable.
*/
public static void ignoreMetrics(Runnable r) {
ignoreMetrics.set(true);
BooleanHolder flag = ignoreMetrics.get();
if (flag == null) {
flag = new BooleanHolder();
ignoreMetrics.set(flag);
}
flag.set(true);
try {
r.run();
}
finally {
ignoreMetrics.remove();
flag.set(false);
}
}

Expand All @@ -141,6 +146,17 @@ public void close() {
}
}

static class BooleanHolder {
private boolean flag;

public boolean get() {
return flag;
}

public void set(boolean flag) {
this.flag = flag;
}
}
}

@NonNullApi
Expand Down Expand Up @@ -210,8 +226,8 @@ public FilterReply decide(Marker marker, Logger logger, Level level, String form
return FilterReply.NEUTRAL;
}

Boolean ignored = LogbackMetrics.ignoreMetrics.get();
if (ignored != null && ignored) {
LogbackMetrics.BooleanHolder ignored = LogbackMetrics.ignoreMetrics.get();
if (ignored != null && ignored.get()) {
return FilterReply.NEUTRAL;
}

Expand Down