Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 4 additions & 12 deletions sentinel-core/src/main/java/com/alibaba/csp/sentinel/Tracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.context.NullContext;
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.node.DefaultNode;
Expand Down Expand Up @@ -53,17 +54,7 @@ public static void trace(Throwable e) {
* @param count exception count to add
*/
public static void trace(Throwable e, int count) {
if (!shouldTrace(e)) {
return;
}

Context context = ContextUtil.getContext();
if (context == null) {
return;
}

DefaultNode curNode = (DefaultNode)context.getCurNode();
traceExceptionToNode(e, count, context.getCurEntry(), curNode);
traceContext(e, count, ContextUtil.getContext());
}

/**
Expand All @@ -77,7 +68,8 @@ public static void traceContext(Throwable e, int count, Context context) {
if (!shouldTrace(e)) {
return;
}
if (context == null) {

if (context == null || context instanceof NullContext) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public String getName() {
}

public Node getCurNode() {
return curEntry.getCurNode();
return curEntry == null ? null : curEntry.getCurNode();
}

public Context setCurNode(Node node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
package com.alibaba.csp.sentinel;

import com.alibaba.csp.sentinel.context.ContextTestUtil;
import com.alibaba.csp.sentinel.context.ContextUtil;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
* @author Carpenter Lee
*/
public class TracerTest extends Tracer {

@Before
public void setUp() {
ContextTestUtil.cleanUpContext();
ContextTestUtil.resetContextMap();
}

@After
public void tearDown() {
ContextTestUtil.cleanUpContext();
ContextTestUtil.resetContextMap();
}

@Test
public void testTraceWhenContextSizeExceedsThreshold() {
int i = 0;
for (; i < Constants.MAX_CONTEXT_NAME_SIZE; i++) {
ContextUtil.enter("test-context-" + i);
ContextUtil.exit();
}

try {
ContextUtil.enter("test-context-" + i);
throw new RuntimeException("test");
} catch (Exception e) {
Tracer.trace(e);
} finally {
ContextUtil.exit();
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to do some context clean-up here (using ContextTestUtil)?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's needed. Very precise!

}

@Test
public void setExceptionsToTrace() {
Tracer.ignoreClasses = null;
Expand Down