Skip to content

Commit

Permalink
Compile regex pattern for MetricFilter only once (#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkolehtisalo authored and joschi committed Aug 9, 2016
1 parent 45cc198 commit 3d06f69
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
Expand Up @@ -32,6 +32,8 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.codahale.metrics.MetricRegistry.name;
import static org.graylog2.shared.metrics.MetricUtils.filterSingleMetric;
Expand All @@ -41,6 +43,15 @@ public class ThroughputCalculator extends Periodical {

private final MetricRegistry metricRegistry;

protected static final Pattern incomingMessagesPattern = Pattern.compile("org\\.graylog2\\.plugin\\.streams\\.Stream\\..*?\\.incomingMessages");
protected static final MetricFilter streamMetricFilter = new MetricFilter() {
@Override
public boolean matches(String name, Metric metric) {
Matcher matcher = incomingMessagesPattern.matcher(name);
return matcher.matches();
}
};

private ConcurrentMap<String, CounterSample> sampledCounters = Maps.newConcurrentMap();

@Inject
Expand Down Expand Up @@ -99,12 +110,7 @@ public void doRun() {
);

// StreamMetrics isn't accessible here, so we need to use a metrics filter instead.
final SortedMap<String, ? extends Counting> streamMeters = metricRegistry.getMeters(new MetricFilter() {
@Override
public boolean matches(String name, Metric metric) {
return name.matches("org\\.graylog2\\.plugin\\.streams\\.Stream\\..*?\\.incomingMessages");
}
});
final SortedMap<String, ? extends Counting> streamMeters = metricRegistry.getMeters(streamMetricFilter);

final Iterable<Map.Entry<String, ? extends Counting>> entries = Iterables.concat(counters.entrySet(),
inputCounters.entrySet(),
Expand Down
@@ -0,0 +1,29 @@
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.periodical;

import org.junit.Test;

import static org.junit.Assert.*;

public class ThroughputCalculatorTest {

@Test
public void testStreamMetricFilter() {
assertTrue("Filter should match stream incomingMessages", ThroughputCalculator.streamMetricFilter.matches("org.graylog2.plugin.streams.Stream.579657c468e16405f90345b0.incomingMessages", null));
}
}

0 comments on commit 3d06f69

Please sign in to comment.