Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#9633] Add ScopeUtils #9635

Merged
merged 1 commit into from
Jan 13, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;

import java.util.Objects;

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

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

// leave scope.
if (!leaveAsyncTraceScope(trace)) {
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to leave scope of async trace {}.", trace);
}
Expand All @@ -125,7 +125,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
}
} finally {
trace.traceBlockEnd();
if (isAsyncTraceDestination(trace)) {
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
if (isDebug) {
logger.debug("Arrived at async trace destination. asyncTraceId={}", asyncContext);
}
Expand Down Expand Up @@ -164,33 +164,6 @@ private void deleteAsyncTrace(final Trace trace) {
trace.close();
}

private void entryAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
scope.tryEnter();
}
}

private boolean leaveAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
} else {
return false;
}
}
return true;
}

private boolean isAsyncTraceDestination(final Trace trace) {
if (!trace.isAsync()) {
return false;
}

final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
return scope != null && !scope.isActive();
}

private void finishAsyncState(final AsyncContext asyncContext) {
if (AsyncContextUtils.asyncStateFinish(asyncContext)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;

import java.util.Objects;

Expand Down Expand Up @@ -60,7 +60,7 @@ public void before(Object target, Object[] args) {
}

// entry scope.
entryAsyncTraceScope(trace);
ScopeUtils.entryAsyncTraceScope(trace);

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

// leave scope.
if (!leaveAsyncTraceScope(trace)) {
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to leave scope of async trace {}.", trace);
}
Expand All @@ -113,7 +113,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
}
} finally {
trace.traceBlockEnd();
if (isAsyncTraceDestination(trace)) {
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
deleteAsyncContext(trace, asyncContext);
}
}
Expand Down Expand Up @@ -153,32 +153,4 @@ private void deleteAsyncContext(final Trace trace, AsyncContext asyncContext) {
asyncContext.close();
}

private void entryAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
scope.tryEnter();
}
}

private boolean leaveAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
} else {
return false;
}
}
return true;
}

private boolean isAsyncTraceDestination(final Trace trace) {
if (!trace.isAsync()) {
return false;
}

final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
return scope != null && !scope.isActive();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;

import java.util.Objects;

Expand Down Expand Up @@ -175,41 +176,32 @@ private boolean initScope(final Trace trace) {
}

private void entryScope(final Trace trace) {
final TraceScope scope = trace.getScope(this.scopeName);
if (scope != null) {
scope.tryEnter();
if (ScopeUtils.entryScope(trace, this.scopeName)) {
if (isDebug) {
logger.debug("Try enter trace scope={}", scope.getName());
logger.debug("Try enter trace scope={}", scopeName);
}
}
}

private boolean leaveScope(final Trace trace) {
final TraceScope scope = trace.getScope(this.scopeName);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
if (isDebug) {
logger.debug("Leave trace scope={}", scope.getName());
}
} else {
if (logger.isInfoEnabled()) {
logger.info("Failed to leave scope. trace={}", trace);
}
return false;
if (ScopeUtils.leaveScope(trace, this.scopeName)) {
if (isDebug) {
logger.debug("Leave trace scope={}", scopeName);
}
return true;
}
return true;
if (logger.isInfoEnabled()) {
logger.info("Failed to leave scope. trace={}", trace);
}
return false;
}

private boolean hasScope(final Trace trace) {
final TraceScope scope = trace.getScope(this.scopeName);
return scope != null;
return ScopeUtils.hasScope(trace, this.scopeName);
}

private boolean isEndScope(final Trace trace) {
final TraceScope scope = trace.getScope(this.scopeName);
return scope != null && !scope.isActive();
return ScopeUtils.isEndScope(trace, this.scopeName);
}

private void deleteTrace(final Trace trace) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.navercorp.pinpoint.bootstrap.util;

import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;

public final class ScopeUtils {

public static final String ASYNC_TRACE_SCOPE = AsyncContext.ASYNC_TRACE_SCOPE;

private ScopeUtils() {
}

public static boolean entryAsyncTraceScope(final Trace trace) {
return entryScope(trace, ASYNC_TRACE_SCOPE);
}

public static boolean entryScope(final Trace trace, final String scopeName) {
final TraceScope scope = trace.getScope(scopeName);
if (scope != null) {
scope.tryEnter();
return true;
}
return false;
}

public static boolean leaveAsyncTraceScope(final Trace trace) {
return leaveScope(trace, ASYNC_TRACE_SCOPE);
}

public static boolean leaveScope(final Trace trace, final String scopeName) {
final TraceScope scope = trace.getScope(scopeName);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
} else {
return false;
}
}
return true;
}

public static boolean isAsyncTraceEndScope(final Trace trace) {
return isAsyncTraceEndScope(trace, ASYNC_TRACE_SCOPE);
}

public static boolean isAsyncTraceEndScope(final Trace trace, final String scopeName) {
if (!trace.isAsync()) {
return false;
}
return isEndScope(trace, scopeName);
}

public static boolean isEndScope(final Trace trace, final String scopeName) {
final TraceScope scope = trace.getScope(scopeName);
return scope != null && !scope.isActive();
}

public static boolean hasScope(final Trace trace, final String scopeName) {
final TraceScope scope = trace.getScope(scopeName);
return scope != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.bootstrap.interceptor.AroundInterceptor;
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;

import java.util.Objects;

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

protected final PLogger logger = PLoggerFactory.getLogger(getClass());
protected final boolean isDebug = logger.isDebugEnabled();
protected static final String ASYNC_TRACE_SCOPE = AsyncContext.ASYNC_TRACE_SCOPE;

protected final MethodDescriptor methodDescriptor;
protected final TraceContext traceContext;
Expand Down Expand Up @@ -68,7 +67,7 @@ public void before(Object target, Object[] args) {
logger.debug("Asynchronous invocation. asyncTraceId={}, trace={}", asyncContext, trace);
}
// entry scope.
entryAsyncTraceScope(trace);
ScopeUtils.entryAsyncTraceScope(trace);

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

// leave scope.
if (!leaveAsyncTraceScope(trace)) {
if (!ScopeUtils.leaveAsyncTraceScope(trace)) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to leave scope of async trace {}.", trace);
}
Expand All @@ -125,8 +124,8 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
}
} finally {
trace.traceBlockEnd();
if (isAsyncTraceDestination(trace)) {
if(isDebug) {
if (ScopeUtils.isAsyncTraceEndScope(trace)) {
if (isDebug) {
logger.debug("Arrived at async trace destination. asyncTraceId={}", asyncContext);
}
deleteAsyncTrace(trace);
Expand Down Expand Up @@ -164,33 +163,6 @@ private void deleteAsyncTrace(final Trace trace) {
trace.close();
}

private void entryAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
scope.tryEnter();
}
}

private boolean leaveAsyncTraceScope(final Trace trace) {
final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
} else {
return false;
}
}
return true;
}

private boolean isAsyncTraceDestination(final Trace trace) {
if (!trace.isAsync()) {
return false;
}

final TraceScope scope = trace.getScope(ASYNC_TRACE_SCOPE);
return scope != null && !scope.isActive();
}

private void finishAsyncState(final AsyncContext asyncContext) {
if (AsyncContextUtils.asyncStateFinish(asyncContext)) {
Expand Down
Loading