Skip to content

Commit

Permalink
#8 Filter statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
mjok committed Jun 23, 2020
1 parent b0ea099 commit aad392e
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 34 deletions.
4 changes: 4 additions & 0 deletions remora-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ Expected response:
{
"filterName" : "ingnoredStreams",
"filterClass" : "class com.jkoolcloud.remora.filters.ClassNameFilter",
"invokeCount" : 1001,
"excludeCount" : 1,
"properties" : {"mode" : "EXCLUDE",
"regex" : false,
"classNames" : ["java.net.SocketInputStream","java.util.jar.JarVerifier$VerifierStream"]
Expand All @@ -700,6 +702,8 @@ Expected response:
{
"filterName" : "ingnoredMysqlStreams",
"filterClass" : "class com.jkoolcloud.remora.filters.ClassNameFilter",
"invokeCount" : 1231,
"excludeCount" : 0,
"properties" : {"mode" : "EXCLUDE",
"regex" : true,
"classNames" : ["com\.mysql.*"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,25 @@
import org.takes.rs.RsText;

import com.jkoolcloud.remora.core.utils.ReflectionUtils;
import com.jkoolcloud.remora.filters.AdviceFilter;
import com.jkoolcloud.remora.filters.FilterManager;
import com.jkoolcloud.remora.filters.StatisticEnabledFilter;

public class TKFilters implements Take {

public static final String FILTER_RESPONSE_TEMPLATE = "'{'\n" //
+ " \"filterName\" : \"{0}\",\n"//
+ " \"filterClass\" : \"{1}\",\n" //
+ " \"properties\" : '{'{2}'}\n"//
+ " \"invokeCount\" : {2},\n" //
+ " \"excludeCount\" : {3},\n" //

+ " \"properties\" : '{'{4}'}\n"//
+ "'}'";

@Override
public Response act(Request req) throws Exception {
StringBuilder response = new StringBuilder();
response.append("[");
Map<String, AdviceFilter> filters = FilterManager.INSTANCE.getAll();
Map<String, StatisticEnabledFilter> filters = FilterManager.INSTANCE.getAll();
response.append(filters.entrySet().stream().map(stringAdviceFilterEntry -> {

List<String> properties = ReflectionUtils.getConfigurableFields(stringAdviceFilterEntry.getValue());
Expand All @@ -54,7 +57,10 @@ public Response act(Request req) throws Exception {
.collect(Collectors.joining(",\n"));

return format(FILTER_RESPONSE_TEMPLATE, stringAdviceFilterEntry.getKey(),
stringAdviceFilterEntry.getValue().getClass(), JSONUtils.addPadding(4, collect));
stringAdviceFilterEntry.getValue().getClass(),
JSONUtils.quote(stringAdviceFilterEntry.getValue().getInvokedCount()),
JSONUtils.quote(stringAdviceFilterEntry.getValue().getExcludedCount()),
JSONUtils.addPadding(4, collect));
}).collect(Collectors.joining(",\n")));
response.append("\n]");
return new RsText(response.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.tinylog.TaggedLogger;

import com.jkoolcloud.remora.RemoraConfig;
import com.jkoolcloud.remora.filters.AdviceFilter;
import com.jkoolcloud.remora.filters.FilterManager;
import com.jkoolcloud.remora.filters.StatisticEnabledFilter;

public class TkNewFilter implements Take {

Expand All @@ -45,9 +45,9 @@ public Response act(Request req) throws Exception {
String filterClass = TakesUtils.getValueForKey("class", body);
String filterName = TakesUtils.getValueForKey("name", body);

AdviceFilter filterInstance = null;
StatisticEnabledFilter filterInstance = null;
try {
filterInstance = (AdviceFilter) Class.forName(filterClass).newInstance();
filterInstance = (StatisticEnabledFilter) Class.forName(filterClass).newInstance();
for (Field field : Arrays.asList(filterInstance.getClass().getDeclaredFields())) {
if (field.isAnnotationPresent(RemoraConfig.Configurable.class)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class TKFiltersTest {
@Test
public void act() throws Exception {
ClassNameFilter filter = new ClassNameFilter();
for (int i = 0; i <= 1000; i++) {
filter.countInvoked();
}
filter.countExcluded();
filter.classNames = Arrays.asList(new String[] { "a.b.c", "a.c.b", "c.b.a" });
filter.mode = AdviceFilter.Mode.INCLUDE;
FilterManager.INSTANCE.add("TESTFILTER1", filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import com.jkoolcloud.remora.filters.AdviceFilter;
import com.jkoolcloud.remora.filters.FilterManager;
import com.jkoolcloud.remora.filters.StatisticEnabledFilter;

public enum RemoraConfig {
INSTANCE;
Expand Down Expand Up @@ -160,7 +161,7 @@ protected void configureFilters() {
try {
String filterClass = config.getProperty(PREFIX + filterName + SUFFIX);
Class<?> aClass = Class.forName(filterClass);
AdviceFilter adviceFilter = (AdviceFilter) aClass.newInstance();
StatisticEnabledFilter adviceFilter = (StatisticEnabledFilter) aClass.newInstance();

for (Field field : aClass.getFields()) {
if (field.isAnnotationPresent(Configurable.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,29 @@

public interface AdviceFilter {

void countExcluded();

void countInvoked();

boolean maches(Object thiz, Method method, Object... arguments);

Mode getMode();

default boolean intercept(Object thiz, Method method, Object... arguments) {
countInvoked();
boolean maches = maches(thiz, method, arguments);

if (getMode().equals(Mode.INCLUDE)) {
return maches(thiz, method, arguments);
if (!maches) {
countExcluded();
}
return maches;
}
if (getMode().equals(Mode.EXCLUDE)) {
return !maches(thiz, method, arguments);
if (maches) {
countExcluded();
}
return !maches;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import com.jkoolcloud.remora.RemoraConfig;

public class ClassNameFilter implements AdviceFilter {
public class ClassNameFilter extends StatisticEnabledFilter {

@RemoraConfig.Configurable
public List<String> classNames = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@
public enum FilterManager {
INSTANCE;

Map<String, AdviceFilter> filters = new HashMap<>(10);
Map<String, StatisticEnabledFilter> filters = new HashMap<>(10);

public List<AdviceFilter> get(List<?> list) {
return filters.entrySet().stream().filter(entry -> list.contains(entry.getKey())).map(entry -> entry.getValue())
.collect(Collectors.toList());
}

public void add(String filterName, AdviceFilter filter) {
public void add(String filterName, StatisticEnabledFilter filter) {
filters.put(filterName, filter);
}

public AdviceFilter get(String filterName) {
return filters.get(filterName);
}

public Map<String, AdviceFilter> getAll() {
public Map<String, StatisticEnabledFilter> getAll() {
return filters;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
package com.jkoolcloud.remora.filters;

import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

import com.jkoolcloud.remora.RemoraConfig;

public class LimitingFilter implements AdviceFilter {
AtomicInteger count = new AtomicInteger(0);

public class LimitingFilter extends StatisticEnabledFilter {
@RemoraConfig.Configurable
public Integer everyNth = 2;

Expand All @@ -32,11 +29,10 @@ public class LimitingFilter implements AdviceFilter {

@Override
public boolean maches(Object thiz, Method method, Object... arguments) {
if (count.get() == 0) {
count.set(everyNth);
if (everyNth <= 1) {
return true;
}
int i = count.decrementAndGet();
if (i == 0) {
if (getInvokedCount() % everyNth != 0) {
return false;
} else {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2019-2020 NASTEL TECHNOLOGIES, INC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.jkoolcloud.remora.filters;

import java.util.concurrent.atomic.AtomicLong;

public abstract class StatisticEnabledFilter implements AdviceFilter {

private AtomicLong invokedCount = new AtomicLong();
private AtomicLong excludedCount = new AtomicLong();

@Override
public void countExcluded() {
excludedCount.incrementAndGet();
}

@Override
public void countInvoked() {
invokedCount.incrementAndGet();
}

public long getInvokedCount() {
return invokedCount.get();
}

public long getExcludedCount() {
return excludedCount.get();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.jkoolcloud.remora.filters;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import org.junit.Test;

Expand All @@ -27,16 +26,18 @@ public class LimitingFilterTest {
public void maches() throws NoSuchMethodException {
LimitingFilter filter = new LimitingFilter();
filter.everyNth = 5;
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertFalse(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.maches(this, getClass().getMethod("maches")));
assertFalse(filter.maches(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertFalse(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertTrue(filter.intercept(this, getClass().getMethod("maches")));
assertFalse(filter.intercept(this, getClass().getMethod("maches")));
assertEquals(10, filter.getInvokedCount());
assertEquals(2, filter.getExcludedCount());

}
}

0 comments on commit aad392e

Please sign in to comment.