From d85331124c45104f6c6620214e6422e70a47f8a7 Mon Sep 17 00:00:00 2001 From: Peter Paul Bakker Date: Thu, 29 Oct 2020 08:55:54 +0100 Subject: [PATCH] reuse compiled regexp Patterns to save some cpu in TraceAction.call. Fixes gh-1760 (#1761) --- .../rxjava/SleuthRxJavaSchedulersHook.java | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaSchedulersHook.java b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaSchedulersHook.java index 2be5ef1d0..66eb82fb6 100644 --- a/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaSchedulersHook.java +++ b/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/rxjava/SleuthRxJavaSchedulersHook.java @@ -16,7 +16,10 @@ package org.springframework.cloud.sleuth.instrument.rxjava; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import java.util.regex.Pattern; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,13 +47,13 @@ class SleuthRxJavaSchedulersHook extends RxJavaSchedulersHook { private final Tracer tracer; - private final List threadsToSample; + private final List threadsToIgnore; private RxJavaSchedulersHook delegate; - SleuthRxJavaSchedulersHook(Tracer tracer, List threadsToSample) { + SleuthRxJavaSchedulersHook(Tracer tracer, List threadsToIgnore) { this.tracer = tracer; - this.threadsToSample = threadsToSample; + this.threadsToIgnore = toPatternList(threadsToIgnore); try { this.delegate = RxJavaPlugins.getInstance().getSchedulersHook(); if (this.delegate instanceof SleuthRxJavaSchedulersHook) { @@ -70,6 +73,17 @@ class SleuthRxJavaSchedulersHook extends RxJavaSchedulersHook { } } + private List toPatternList(List threadsToIgnore) { + if (threadsToIgnore == null || threadsToIgnore.size() == 0) { + return Collections.emptyList(); + } + List patterns = new ArrayList<>(threadsToIgnore.size()); + for (String thread : threadsToIgnore) { + patterns.add(Pattern.compile(thread)); + } + return Collections.unmodifiableList(patterns); + } + private void logCurrentStateOfRxJavaPlugins(RxJavaErrorHandler errorHandler, RxJavaObservableExecutionHook observableExecutionHook) { if (log.isDebugEnabled()) { @@ -89,7 +103,7 @@ class SleuthRxJavaSchedulersHook extends RxJavaSchedulersHook { if (wrappedAction instanceof TraceAction) { return action; } - return super.onSchedule(new TraceAction(this.tracer, wrappedAction, this.threadsToSample)); + return super.onSchedule(new TraceAction(this.tracer, wrappedAction, this.threadsToIgnore)); } /** @@ -107,9 +121,9 @@ class SleuthRxJavaSchedulersHook extends RxJavaSchedulersHook { private final Span parent; - private final List threadsToIgnore; + private final List threadsToIgnore; - TraceAction(Tracer tracer, Action0 actual, List threadsToIgnore) { + TraceAction(Tracer tracer, Action0 actual, List threadsToIgnore) { this.tracer = tracer; this.threadsToIgnore = threadsToIgnore; this.parent = this.tracer.currentSpan(); @@ -120,9 +134,9 @@ class SleuthRxJavaSchedulersHook extends RxJavaSchedulersHook { @Override public void call() { // don't create a span if the thread name is on a list of threads to ignore - for (String threadToIgnore : this.threadsToIgnore) { - String threadName = Thread.currentThread().getName(); - if (threadName.matches(threadToIgnore)) { + String threadName = Thread.currentThread().getName(); + for (Pattern threadToIgnore : this.threadsToIgnore) { + if (threadToIgnore.matcher(threadName).matches()) { if (log.isTraceEnabled()) { log.trace(String.format( "Thread with name [%s] matches the regex [%s]. A span will not be created for this Thread.",