Skip to content

Commit

Permalink
[#9504] Refactor runOnContext trace
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 19, 2022
1 parent eb90f39 commit 7089c1b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ public void start(Promise<Void> startPromise) throws Exception {
router.get("/runOnContext/request").handler(routingContext -> {
runOnContextRequest(routingContext.request());
});
router.get("/runOnContext/error").handler(routingContext -> {
router.get("/runOnContext/throwException").handler(routingContext -> {
vertx.runOnContext(aVoid -> {
throw new RuntimeException("/runOnContext/error");
throw new RuntimeException("FAIL");
});
});
router.get("/runOnContext/statusCode500").handler(routingContext -> {
vertx.runOnContext(aVoid -> {
routingContext.response().setStatusCode(500).end("StatusCode:500");
});
});

Expand All @@ -101,6 +106,13 @@ public void start(Promise<Void> startPromise) throws Exception {
routingContext.response().end(arg1);
});

router.get("/throwException").handler(routingContext -> {
throw new RuntimeException("FAIL");
});
router.get("/statusCode500").handler(routingContext -> {
routingContext.response().setStatusCode(500).end("StatusCode:500");
});


vertx.createHttpServer().requestHandler(router).listen(18080, http -> {
if (http.succeeded()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ private URITemplate() {
}

public static final String NOT_FOUND = "/NOT_FOUND_URI";
public static final String NULL_URI = "/NULL";
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
return;
}

prepareAfter(trace, target, args, result, throwable);
if (!trace.canSampled()) {
return;
}

try {
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
doInAfterTrace(recorder, target, args, result, throwable);
Expand All @@ -136,6 +141,9 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
}
}

protected void prepareAfter(Trace trace, Object target, Object[] args, Object result, Throwable throwable) {
}

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

protected AsyncContext getAsyncContext(Object target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ public HttpServerResponseImplInterceptor(MethodDescriptor methodDescriptor, Trac
public void doInBeforeTrace(SpanEventRecorder recorder, AsyncContext asyncContext, Object target, Object[] args) {
}

@Override
protected void prepareAfter(Trace trace, Object target, Object[] args, Object result, Throwable throwable) {
if (target instanceof HttpServerResponse) {
// Trace trace = traceContext.currentRawTraceObject();
if (trace == null) {
return;
}
final HttpServerResponse response = (HttpServerResponse) target;
final SpanRecorder spanRecorder = trace.getSpanRecorder();
this.httpStatusCodeRecorder.record(spanRecorder, response.getStatusCode());
}
}

@Override
public void doInAfterTrace(SpanEventRecorder recorder, Object target, Object[] args, Object result, Throwable throwable) {
recorder.recordApi(methodDescriptor);
recorder.recordServiceType(VertxConstants.VERTX_HTTP_SERVER_INTERNAL);
recorder.recordException(throwable);

if (target instanceof HttpServerResponse) {
final HttpServerResponse response = (HttpServerResponse) target;
// TODO more simple.
final AsyncContext asyncContext = getAsyncContext(target);
if (asyncContext != null) {
final Trace trace = asyncContext.currentAsyncTraceObject();
if (trace != null) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
this.httpStatusCodeRecorder.record(spanRecorder, response.getStatusCode());
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.bootstrap.plugin.RequestRecorderFactory;
import com.navercorp.pinpoint.bootstrap.plugin.http.HttpStatusCodeRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.proxy.ProxyRequestRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.request.RequestAdaptor;
import com.navercorp.pinpoint.bootstrap.plugin.request.RequestTraceReader;
Expand Down Expand Up @@ -67,7 +68,7 @@ public class ServerConnectionHandleRequestInterceptor implements AroundIntercept
private final TraceContext traceContext;
private final MethodDescriptor descriptor;


private final HttpStatusCodeRecorder httpStatusCodeRecorder;

public ServerConnectionHandleRequestInterceptor(final TraceContext traceContext,
final MethodDescriptor methodDescriptor,
Expand All @@ -86,6 +87,8 @@ public ServerConnectionHandleRequestInterceptor(final TraceContext traceContext,
this.serverRequestRecorder = new ServerRequestRecorder<>(requestAdaptor);
this.requestTraceReader = new RequestTraceReader<>(traceContext, requestAdaptor, true);
traceContext.cacheApi(VERTX_HTTP_SERVER_METHOD_DESCRIPTOR);

this.httpStatusCodeRecorder = new HttpStatusCodeRecorder(traceContext.getProfilerConfig().getHttpStatusCodeErrors());
}


Expand Down Expand Up @@ -203,6 +206,13 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
return;
}

final boolean validate = validate(args);
if (validate) {
final HttpServerRequest request = (HttpServerRequest) args[0];
HttpServerResponse response = request.response();
this.httpStatusCodeRecorder.record(trace.getSpanRecorder(), response.getStatusCode());
}

if (!trace.canSampled()) {
deleteTrace(trace);
return;
Expand All @@ -212,7 +222,7 @@ public void after(Object target, Object[] args, Object result, Throwable throwab
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordApi(descriptor);
recorder.recordException(throwable);
if (validate(args)) {
if (validate) {
final HttpServerRequest request = (HttpServerRequest) args[0];
parameterRecorder.record(recorder, request, throwable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private AsyncQueueingUriStatStorage(int queueSize, String executorName, Executor
@Override
public void store(String uri, boolean status, long startTime, long endTime) {
if (uri == null) {
uri = URITemplate.NOT_FOUND;
uri = URITemplate.NULL_URI;
}
UriStatInfo uriStatInfo = new UriStatInfo(uri, status, startTime, endTime);
execute(uriStatInfo);
Expand Down

0 comments on commit 7089c1b

Please sign in to comment.