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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@

import com.pinpoint.test.common.view.ApiLinkPage;
import com.pinpoint.test.common.view.HrefTag;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import org.apache.commons.io.IOUtils;
Expand Down Expand Up @@ -56,6 +73,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

@RestController
Expand Down Expand Up @@ -305,6 +323,7 @@ public String get() {

return "OK";
}

@RequestMapping(value = "/jdk/connect/https")
public String getHttps() {

Expand All @@ -318,6 +337,7 @@ public String getHttps() {

return "OK";
}

@RequestMapping(value = "/jdk/connect/duplicated")
public String getDuplicated() {

Expand Down Expand Up @@ -351,6 +371,7 @@ public String get2() {
return "OK";
}


@RequestMapping(value = "/jdk/connect2/https")
public String get2Https() {

Expand Down Expand Up @@ -382,4 +403,82 @@ private URL newURL(String spec) {
throw new IllegalArgumentException("invalid url" + spec, exception);
}
}

@RequestMapping(value = "/netty/listener")
public String nettyListener() throws Exception {
final CountDownLatch awaitLatch = new CountDownLatch(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);
Bootstrap bootstrap = client(workerGroup);
Channel channel = bootstrap.connect("httpbin.org", 80).sync().channel();

channel.pipeline().addLast(new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) {
awaitLatch.countDown();
}
});

try {
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.GET, "/");
channel.writeAndFlush(request);
boolean await = awaitLatch.await(3000, TimeUnit.MILLISECONDS);
} finally {
channel.close().sync();
workerGroup.shutdownGracefully().sync();
}

return "OK";
}

@RequestMapping(value = "/netty/write")
public String nettyWrite() throws Exception {
final CountDownLatch awaitLatch = new CountDownLatch(1);

EventLoopGroup workerGroup = new NioEventLoopGroup(2);
Bootstrap bootstrap = client(workerGroup);
final ChannelFuture connect = bootstrap.connect("httpbin.org", 80);

connect.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Channel channel = future.channel();
channel.pipeline().addLast(new SimpleChannelInboundHandler() {

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
awaitLatch.countDown();
}

});
HttpRequest request = new DefaultFullHttpRequest(
HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.GET, "/");
future.channel().writeAndFlush(request);
}
}

});

boolean await = awaitLatch.await(3000, TimeUnit.MILLISECONDS);

final Channel channel = connect.channel();
channel.close().sync();
workerGroup.shutdownGracefully().sync();

return "OK";
}

public Bootstrap client(EventLoopGroup workerGroup) {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(workerGroup).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpClientCodec());
ch.pipeline().addLast(new HttpObjectAggregator(65535));
}
});
return bootstrap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public interface StackOperation {
void traceBlockEnd(int stackId);

boolean isRootStack();

int getCallStackFrameId();

TraceBlock traceBlockBeginAndGet();
TraceBlock getTraceBlock();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public interface TraceBlock extends SpanEventRecorder, AutoCloseable {

Trace getTrace();

void begin();

boolean isBegin();

@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,24 @@
// entry scope.
ScopeUtils.entryAsyncTraceScope(trace);

final TraceBlock traceBlock = trace.getTraceBlock();

Check warning on line 50 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java#L50

Added line #L50 was not covered by tests
try {
// trace event for default & async.
final TraceBlock traceBlock = trace.traceBlockBeginAndGet();
beforeTrace(asyncContext, trace, traceBlock, target, apiId, args);
doInBeforeTrace(traceBlock, asyncContext, target, apiId, args);
return traceBlock;
if (checkBeforeTraceBlockBegin(asyncContext, trace, target, apiId, args)) {
traceBlock.begin();
beforeTrace(asyncContext, trace, traceBlock, target, apiId, args);
doInBeforeTrace(traceBlock, asyncContext, target, apiId, args);

Check warning on line 55 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java#L53-L55

Added lines #L53 - L55 were not covered by tests
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("BEFORE. Caused:{}", th.getMessage(), th);
}
}

return null;
return traceBlock;

Check warning on line 63 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java#L63

Added line #L63 was not covered by tests
}

protected boolean checkBeforeTraceBlockBegin(AsyncContext asyncContext, Trace trace, Object target, int apiId, Object[] args) {
return true;

Check warning on line 67 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java#L67

Added line #L67 was not covered by tests
}

protected void beforeTrace(final AsyncContext asyncContext, final Trace trace, final SpanEventRecorder recorder, final Object target, int apiId, final Object[] args) {
Expand Down Expand Up @@ -98,8 +103,10 @@
}

try (TraceBlock traceBlock = block) {
afterTrace(asyncContext, trace, traceBlock, target, apiId, args, result, throwable);
doInAfterTrace(traceBlock, target, apiId, args, result, throwable);
if (traceBlock.isBegin()) {
afterTrace(asyncContext, trace, traceBlock, target, apiId, args, result, throwable);
doInAfterTrace(traceBlock, target, apiId, args, result, throwable);

Check warning on line 108 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockApiIdAwareAroundInterceptor.java#L107-L108

Added lines #L107 - L108 were not covered by tests
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@
package com.navercorp.pinpoint.bootstrap.interceptor;

import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
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.TraceBlock;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.util.ScopeUtils;

import java.util.Objects;

public abstract class AsyncContextSpanEventNestedBlockApiIdAwareAroundInterceptor extends AbstractAsyncContextSpanEventInterceptor implements BlockApiIdAwareAroundInterceptor {
public abstract class AsyncContextSpanEventBlockSimpleAroundInterceptor extends AbstractAsyncContextSpanEventInterceptor implements BlockAroundInterceptor {

private final String traceScopeName;
protected final MethodDescriptor methodDescriptor;

public AsyncContextSpanEventNestedBlockApiIdAwareAroundInterceptor(TraceContext traceContext, String traceScopeName) {
public AsyncContextSpanEventBlockSimpleAroundInterceptor(TraceContext traceContext, MethodDescriptor methodDescriptor) {
super(traceContext);
this.traceScopeName = Objects.requireNonNull(traceScopeName, "traceScopeName");
this.methodDescriptor = Objects.requireNonNull(methodDescriptor, "methodDescriptor");

Check warning on line 34 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L34

Added line #L34 was not covered by tests
}

@Override
public TraceBlock before(Object target, int apiId, Object[] args) {
public TraceBlock before(Object target, Object[] args) {
if (isDebug) {
logger.beforeInterceptor(target, args);
}
Expand All @@ -50,39 +50,35 @@
return null;
}

// try entry API.
if (Boolean.FALSE == tryEnter(trace)) {
if (isDebug) {
logger.debug("Skip nested async API.before(), trace={}, traceScopeName={}", trace, traceScopeName);
}
return null;
}

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

final TraceBlock traceBlock = trace.getTraceBlock();

Check warning on line 55 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L55

Added line #L55 was not covered by tests
try {
// trace event for default & async.
final TraceBlock traceBlock = trace.traceBlockBeginAndGet();
beforeTrace(asyncContext, trace, traceBlock, target, apiId, args);
doInBeforeTrace(traceBlock, asyncContext, target, apiId, args);
return traceBlock;
if (checkBeforeTraceBlockBegin(asyncContext, trace, target, args)) {
traceBlock.begin();
beforeTrace(asyncContext, trace, traceBlock, target, args);
doInBeforeTrace(traceBlock, asyncContext, target, args);

Check warning on line 60 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L58-L60

Added lines #L58 - L60 were not covered by tests
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("BEFORE. Caused:{}", th.getMessage(), th);
}
}

return null;
return traceBlock;

Check warning on line 68 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L68

Added line #L68 was not covered by tests
}

protected boolean checkBeforeTraceBlockBegin(AsyncContext asyncContext, Trace trace, Object target, Object[] args) {
return true;

Check warning on line 72 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L72

Added line #L72 was not covered by tests
}

protected void beforeTrace(final AsyncContext asyncContext, final Trace trace, final SpanEventRecorder recorder, final Object target, final int apiId, final Object[] args) {
protected void beforeTrace(final AsyncContext asyncContext, final Trace trace, final SpanEventRecorder recorder, final Object target, final Object[] args) {
}

protected abstract void doInBeforeTrace(SpanEventRecorder recorder, AsyncContext asyncContext, Object target, int apiId, Object[] args);
protected abstract void doInBeforeTrace(SpanEventRecorder recorder, AsyncContext asyncContext, Object target, Object[] args);

@Override
public void after(TraceBlock block, Object target, int apiId, Object[] args, Object result, Throwable throwable) {
public void after(TraceBlock block, Object target, Object[] args, Object result, Throwable throwable) {
if (isDebug) {
logger.afterInterceptor(target, args, result, throwable);
}
Expand All @@ -92,15 +88,12 @@
return;
}

final Trace trace = asyncContext.currentAsyncTraceObject();
if (trace == null) {
if (block == null) {
return;
}

if (Boolean.FALSE == canLeave(trace)) {
if (isDebug) {
logger.debug("Skip nested async API.after(), trace={}, traceScopeName={}", trace, traceScopeName);
}
final Trace trace = block.getTrace();

Check warning on line 95 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L95

Added line #L95 was not covered by tests
if (trace == null) {
return;
}

Expand All @@ -115,8 +108,10 @@
}

try (TraceBlock traceBlock = block) {
afterTrace(asyncContext, trace, traceBlock, target, apiId, args, result, throwable);
doInAfterTrace(traceBlock, target, apiId, args, result, throwable);
if (traceBlock.isBegin()) {
afterTrace(asyncContext, trace, traceBlock, target, args, result, throwable);
doInAfterTrace(traceBlock, target, args, result, throwable);

Check warning on line 113 in agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java

View check run for this annotation

Codecov / codecov/patch

agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/interceptor/AsyncContextSpanEventBlockSimpleAroundInterceptor.java#L112-L113

Added lines #L112 - L113 were not covered by tests
}
} catch (Throwable th) {
if (logger.isWarnEnabled()) {
logger.warn("AFTER error. Caused:{}", th.getMessage(), th);
Expand All @@ -128,36 +123,8 @@
}
}

protected void afterTrace(final AsyncContext asyncContext, final Trace trace, final SpanEventRecorder recorder, final Object target, int apiId, final Object[] args, final Object result, final Throwable throwable) {
}

protected abstract void doInAfterTrace(SpanEventRecorder recorder, Object target, int apiId, Object[] args, Object result, Throwable throwable);

public boolean tryEnter(final Trace trace) {
TraceScope scope = trace.getScope(traceScopeName);
if (scope == null) {
trace.addBoundaryScope(traceScopeName);
scope = trace.getScope(traceScopeName);
}
if (scope != null) {
boolean result = scope.tryEnter();
return result;
}
if (isDebug) {
logger.debug("Skip to enter, not found scope, scopeName={}", traceScopeName);
}
return false;
protected void afterTrace(final AsyncContext asyncContext, final Trace trace, final SpanEventRecorder recorder, final Object target, final Object[] args, final Object result, final Throwable throwable) {
}

public boolean canLeave(final Trace trace) {
TraceScope scope = trace.getScope(traceScopeName);
if (scope != null) {
if (scope.canLeave()) {
scope.leave();
return true;
}
}

return false;
}
protected abstract void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable);
}
Loading