Procházet zdrojové kódy

Add exceptionPredicate in Tracer for customized exception filtering logic (#1496)

master
pleasecheckhere2016 GitHub před 4 roky
rodič
revize
5523fd0d42
V databázi nebyl nalezen žádný známý klíč pro tento podpis ID GPG klíče: 4AEE18F83AFDEB23
2 změnil soubory, kde provedl 50 přidání a 0 odebrání
  1. +25
    -0
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java
  2. +25
    -0
      sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java

+ 25
- 0
sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java Zobrazit soubor

@@ -20,6 +20,7 @@ import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.context.NullContext;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.function.Predicate;

/**
* This class is used to record other exceptions except block exception.
@@ -32,6 +33,8 @@ public class Tracer {
protected static Class<? extends Throwable>[] traceClasses;
protected static Class<? extends Throwable>[] ignoreClasses;

protected static Predicate<Throwable> exceptionPredicate;

protected Tracer() {}

/**
@@ -164,6 +167,24 @@ public class Tracer {
return ignoreClasses;
}

/**
* Get exception predicate
* @return the exception predicate.
*/
public static Predicate<? extends Throwable> getExceptionPredicate() {
return exceptionPredicate;
}

/**
* set an exception predicate which indicates the exception should be traced(return true) or ignored(return false)
* except for {@link BlockException}
* @param exceptionPredicate the exception predicate
*/
public static void setExceptionPredicate(Predicate<Throwable> exceptionPredicate) {
AssertUtil.notNull(exceptionPredicate, "exception predicate must not be null");
Tracer.exceptionPredicate = exceptionPredicate;
}

private static void checkNotNull(Class<? extends Throwable>[] classes) {
AssertUtil.notNull(classes, "trace or ignore classes must not be null");
for (Class<? extends Throwable> clazz : classes) {
@@ -181,6 +202,10 @@ public class Tracer {
if (t == null || t instanceof BlockException) {
return false;
}
if (exceptionPredicate != null) {
return exceptionPredicate.test(t);
}

if (ignoreClasses != null) {
for (Class<? extends Throwable> clazz : ignoreClasses) {
if (clazz != null && clazz.isAssignableFrom(t.getClass())) {


+ 25
- 0
sentinel-core/src/test/java/com/alibaba/csp/sentinel/TracerTest.java Zobrazit soubor

@@ -2,6 +2,7 @@ package com.alibaba.csp.sentinel;

import com.alibaba.csp.sentinel.context.ContextTestUtil;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.util.function.Predicate;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -52,6 +53,25 @@ public class TracerTest extends Tracer {
Assert.assertFalse(Tracer.shouldTrace(new Exception()));
}

@Test
public void setExceptionPredicate() {

Predicate<Throwable> throwablePredicate = new Predicate<Throwable>() {
@Override
public boolean test(Throwable throwable) {
if (throwable instanceof TraceException) {
return true;
} else if (throwable instanceof IgnoreException) {
return false;
}
return false;
}
};
Tracer.setExceptionPredicate(throwablePredicate);
Assert.assertTrue(Tracer.shouldTrace(new TraceException()));
Assert.assertFalse(Tracer.shouldTrace(new IgnoreException()));
}

@Test
public void setExceptionsToIgnore() {
Tracer.ignoreClasses = null;
@@ -88,6 +108,11 @@ public class TracerTest extends Tracer {
Tracer.setExceptionsToIgnore(IgnoreException.class, null);
}

@Test(expected = IllegalArgumentException.class)
public void testNull3() {
Tracer.setExceptionPredicate(null);
}

private class TraceException extends Exception {}

private class TraceException2 extends Exception {}


Načítá se…
Zrušit
Uložit