Skip to content

Commit a3ffaba

Browse files
committed
[pinpoint-apm#9633] Add ScopeUtils
1 parent 5aad118 commit a3ffaba

File tree

15 files changed

+192
-393
lines changed

15 files changed

+192
-393
lines changed

bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventEndPointInterceptor.java

+4-31
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
2424
import com.navercorp.pinpoint.bootstrap.context.Trace;
2525
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
26-
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
2726
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
2827
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
28+
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;
2929

3030
import java.util.Objects;
3131

@@ -66,7 +66,7 @@ public void before(Object target, Object[] args) {
6666
logger.debug("Asynchronous invocation. asyncTraceId={}, trace={}", asyncContext, trace);
6767
}
6868
// entry scope.
69-
entryAsyncTraceScope(trace);
69+
ScopeUtils.entryAsyncTraceScope(trace);
7070

7171
try {
7272
// trace event for default & async.
@@ -107,7 +107,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
107107
}
108108

109109
// leave scope.
110-
if (!leaveAsyncTraceScope(trace)) {
110+
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
111111
if (logger.isWarnEnabled()) {
112112
logger.warn("Failed to leave scope of async trace {}.", trace);
113113
}
@@ -125,7 +125,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
125125
}
126126
} finally {
127127
trace.traceBlockEnd();
128-
if (isAsyncTraceDestination(trace)) {
128+
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
129129
if (isDebug) {
130130
logger.debug("Arrived at async trace destination. asyncTraceId={}", asyncContext);
131131
}
@@ -164,33 +164,6 @@ private void deleteAsyncTrace(final Trace trace) {
164164
trace.close();
165165
}
166166

167-
private void entryAsyncTraceScope(final Trace trace) {
168-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
169-
if (scope != null) {
170-
scope.tryEnter();
171-
}
172-
}
173-
174-
private boolean leaveAsyncTraceScope(final Trace trace) {
175-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
176-
if (scope != null) {
177-
if (scope.canLeave()) {
178-
scope.leave();
179-
} else {
180-
return false;
181-
}
182-
}
183-
return true;
184-
}
185-
186-
private boolean isAsyncTraceDestination(final Trace trace) {
187-
if (!trace.isAsync()) {
188-
return false;
189-
}
190-
191-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
192-
return scope != null && !scope.isActive();
193-
}
194167

195168
private void finishAsyncState(final AsyncContext asyncContext) {
196169
if (AsyncContextUtils.asyncStateFinish(asyncContext)) {

bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventSimpleAroundInterceptor.java

+4-32
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
2222
import com.navercorp.pinpoint.bootstrap.context.Trace;
2323
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
24-
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
2524
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
2625
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
26+
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;
2727

2828
import java.util.Objects;
2929

@@ -60,7 +60,7 @@ public void before(Object target, Object[] args) {
6060
}
6161

6262
// entry scope.
63-
entryAsyncTraceScope(trace);
63+
ScopeUtils.entryAsyncTraceScope(trace);
6464

6565
try {
6666
// trace event for default & async.
@@ -95,7 +95,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
9595
}
9696

9797
// leave scope.
98-
if (!leaveAsyncTraceScope(trace)) {
98+
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
9999
if (logger.isWarnEnabled()) {
100100
logger.warn("Failed to leave scope of async trace {}.", trace);
101101
}
@@ -113,7 +113,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
113113
}
114114
} finally {
115115
trace.traceBlockEnd();
116-
if (isAsyncTraceDestination(trace)) {
116+
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
117117
deleteAsyncContext(trace, asyncContext);
118118
}
119119
}
@@ -153,32 +153,4 @@ private void deleteAsyncContext(final Trace trace, AsyncContext asyncContext) {
153153
asyncContext.close();
154154
}
155155

156-
private void entryAsyncTraceScope(final Trace trace) {
157-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
158-
if (scope != null) {
159-
scope.tryEnter();
160-
}
161-
}
162-
163-
private boolean leaveAsyncTraceScope(final Trace trace) {
164-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
165-
if (scope != null) {
166-
if (scope.canLeave()) {
167-
scope.leave();
168-
} else {
169-
return false;
170-
}
171-
}
172-
return true;
173-
}
174-
175-
private boolean isAsyncTraceDestination(final Trace trace) {
176-
if (!trace.isAsync()) {
177-
return false;
178-
}
179-
180-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
181-
return scope != null && !scope.isActive();
182-
}
183-
184156
}

bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/SpanRecursiveAroundInterceptor.java

+13-21
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
2424
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
2525
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
26+
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;
2627

2728
import java.util.Objects;
2829

@@ -175,41 +176,32 @@ private boolean initScope(final Trace trace) {
175176
}
176177

177178
private void entryScope(final Trace trace) {
178-
final TraceScope scope = trace.getScope(this.scopeName);
179-
if (scope != null) {
180-
scope.tryEnter();
179+
if (ScopeUtils.entryScope(trace, this.scopeName)) {
181180
if (isDebug) {
182-
logger.debug("Try enter trace scope={}", scope.getName());
181+
logger.debug("Try enter trace scope={}", scopeName);
183182
}
184183
}
185184
}
186185

187186
private boolean leaveScope(final Trace trace) {
188-
final TraceScope scope = trace.getScope(this.scopeName);
189-
if (scope != null) {
190-
if (scope.canLeave()) {
191-
scope.leave();
192-
if (isDebug) {
193-
logger.debug("Leave trace scope={}", scope.getName());
194-
}
195-
} else {
196-
if (logger.isInfoEnabled()) {
197-
logger.info("Failed to leave scope. trace={}", trace);
198-
}
199-
return false;
187+
if (ScopeUtils.leaveScope(trace, this.scopeName)) {
188+
if (isDebug) {
189+
logger.debug("Leave trace scope={}", scopeName);
200190
}
191+
return true;
201192
}
202-
return true;
193+
if (logger.isInfoEnabled()) {
194+
logger.info("Failed to leave scope. trace={}", trace);
195+
}
196+
return false;
203197
}
204198

205199
private boolean hasScope(final Trace trace) {
206-
final TraceScope scope = trace.getScope(this.scopeName);
207-
return scope != null;
200+
return ScopeUtils.hasScope(trace, this.scopeName);
208201
}
209202

210203
private boolean isEndScope(final Trace trace) {
211-
final TraceScope scope = trace.getScope(this.scopeName);
212-
return scope != null && !scope.isActive();
204+
return ScopeUtils.isEndScope(trace, this.scopeName);
213205
}
214206

215207
private void deleteTrace(final Trace trace) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.navercorp.pinpoint.bootstrap.util;
2+
3+
import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
4+
import com.navercorp.pinpoint.bootstrap.context.Trace;
5+
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
6+
7+
public final class ScopeUtils {
8+
9+
public static final String ASYNC_TRACE_SCOPE = AsyncContext.ASYNC_TRACE_SCOPE;
10+
11+
private ScopeUtils() {
12+
}
13+
14+
public static boolean entryAsyncTraceScope(final Trace trace) {
15+
return entryScope(trace, ASYNC_TRACE_SCOPE);
16+
}
17+
18+
public static boolean entryScope(final Trace trace, final String scopeName) {
19+
final TraceScope scope = trace.getScope(scopeName);
20+
if (scope != null) {
21+
scope.tryEnter();
22+
return true;
23+
}
24+
return false;
25+
}
26+
27+
public static boolean leaveAsyncTraceScope(final Trace trace) {
28+
return leaveScope(trace, ASYNC_TRACE_SCOPE);
29+
}
30+
31+
public static boolean leaveScope(final Trace trace, final String scopeName) {
32+
final TraceScope scope = trace.getScope(scopeName);
33+
if (scope != null) {
34+
if (scope.canLeave()) {
35+
scope.leave();
36+
} else {
37+
return false;
38+
}
39+
}
40+
return true;
41+
}
42+
43+
public static boolean isAsyncTraceEndScope(final Trace trace) {
44+
return isAsyncTraceEndScope(trace, ASYNC_TRACE_SCOPE);
45+
}
46+
47+
public static boolean isAsyncTraceEndScope(final Trace trace, final String scopeName) {
48+
if (!trace.isAsync()) {
49+
return false;
50+
}
51+
return isEndScope(trace, scopeName);
52+
}
53+
54+
public static boolean isEndScope(final Trace trace, final String scopeName) {
55+
final TraceScope scope = trace.getScope(scopeName);
56+
return scope != null && !scope.isActive();
57+
}
58+
59+
public static boolean hasScope(final Trace trace, final String scopeName) {
60+
final TraceScope scope = trace.getScope(scopeName);
61+
return scope != null;
62+
}
63+
}

plugins/akka-http/src/main/java/com/navercorp/pinpoint/plugin/akka/http/interceptor/AsyncContextSpanEventEndPointInterceptor.java

+5-33
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
2424
import com.navercorp.pinpoint.bootstrap.context.Trace;
2525
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
26-
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
2726
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
2827
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
2928
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
29+
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;
3030

3131
import java.util.Objects;
3232

@@ -37,7 +37,6 @@ public abstract class AsyncContextSpanEventEndPointInterceptor implements Around
3737

3838
protected final PLogger logger = PLoggerFactory.getLogger(getClass());
3939
protected final boolean isDebug = logger.isDebugEnabled();
40-
protected static final String ASYNC_TRACE_SCOPE = AsyncContext.ASYNC_TRACE_SCOPE;
4140

4241
protected final MethodDescriptor methodDescriptor;
4342
protected final TraceContext traceContext;
@@ -68,7 +67,7 @@ public void before(Object target, Object[] args) {
6867
logger.debug("Asynchronous invocation. asyncTraceId={}, trace={}", asyncContext, trace);
6968
}
7069
// entry scope.
71-
entryAsyncTraceScope(trace);
70+
ScopeUtils.entryAsyncTraceScope(trace);
7271

7372
try {
7473
// trace event for default & async.
@@ -107,7 +106,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
107106
}
108107

109108
// leave scope.
110-
if (!leaveAsyncTraceScope(trace)) {
109+
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
111110
if (logger.isWarnEnabled()) {
112111
logger.warn("Failed to leave scope of async trace {}.", trace);
113112
}
@@ -125,8 +124,8 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
125124
}
126125
} finally {
127126
trace.traceBlockEnd();
128-
if (isAsyncTraceDestination(trace)) {
129-
if(isDebug) {
127+
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
128+
if (isDebug) {
130129
logger.debug("Arrived at async trace destination. asyncTraceId={}", asyncContext);
131130
}
132131
deleteAsyncTrace(trace);
@@ -164,33 +163,6 @@ private void deleteAsyncTrace(final Trace trace) {
164163
trace.close();
165164
}
166165

167-
private void entryAsyncTraceScope(final Trace trace) {
168-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
169-
if (scope != null) {
170-
scope.tryEnter();
171-
}
172-
}
173-
174-
private boolean leaveAsyncTraceScope(final Trace trace) {
175-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
176-
if (scope != null) {
177-
if (scope.canLeave()) {
178-
scope.leave();
179-
} else {
180-
return false;
181-
}
182-
}
183-
return true;
184-
}
185-
186-
private boolean isAsyncTraceDestination(final Trace trace) {
187-
if (!trace.isAsync()) {
188-
return false;
189-
}
190-
191-
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
192-
return scope != null && !scope.isActive();
193-
}
194166

195167
private void finishAsyncState(final AsyncContext asyncContext) {
196168
if (AsyncContextUtils.asyncStateFinish(asyncContext)) {

0 commit comments

Comments
 (0)