Skip to content

Commit

Permalink
Merge pull request #17826 from bcluap/#16106
Browse files Browse the repository at this point in the history
Only close the span after all writing has finished
geoand authored Jun 10, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 44e736f + 1f5e033 commit 5e9422d
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
import io.quarkus.resteasy.common.spi.ResteasyJaxrsProviderBuildItem;
import io.quarkus.resteasy.reactive.spi.CustomContainerResponseFilterBuildItem;
import io.quarkus.resteasy.reactive.spi.DynamicFeatureBuildItem;
import io.quarkus.resteasy.reactive.spi.WriterInterceptorBuildItem;
import io.quarkus.smallrye.opentracing.runtime.QuarkusSmallRyeTracingDynamicFeature;
import io.quarkus.smallrye.opentracing.runtime.QuarkusSmallRyeTracingStandaloneContainerResponseFilter;
import io.quarkus.smallrye.opentracing.runtime.QuarkusSmallRyeTracingStandaloneVertxDynamicFeature;
@@ -45,6 +46,7 @@ void setupFilter(
BuildProducer<FeatureBuildItem> feature,
BuildProducer<CustomContainerResponseFilterBuildItem> customResponseFilters,
BuildProducer<DynamicFeatureBuildItem> dynamicFeatures,
BuildProducer<WriterInterceptorBuildItem> writerInterceptors,
Capabilities capabilities) {

feature.produce(new FeatureBuildItem(Feature.SMALLRYE_OPENTRACING));
@@ -69,6 +71,9 @@ void setupFilter(
customResponseFilters.produce(new CustomContainerResponseFilterBuildItem(
QuarkusSmallRyeTracingStandaloneContainerResponseFilter.class.getName()));
dynamicFeatures.produce(new DynamicFeatureBuildItem(QuarkusSmallRyeTracingDynamicFeature.class.getName()));
writerInterceptors.produce(
new WriterInterceptorBuildItem.Builder(
QuarkusSmallRyeTracingStandaloneContainerResponseFilter.class.getName()).build());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package io.quarkus.smallrye.opentracing.runtime;

import java.io.IOException;

import javax.annotation.Priority;
import javax.interceptor.Interceptor;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;

import org.jboss.resteasy.reactive.server.ServerResponseFilter;

import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.tag.Tags;

public class QuarkusSmallRyeTracingStandaloneContainerResponseFilter {
@Provider
// We must close the span after everything else has finished
@Priority(Interceptor.Priority.PLATFORM_BEFORE)
public class QuarkusSmallRyeTracingStandaloneContainerResponseFilter implements WriterInterceptor {

@ServerResponseFilter(priority = Priorities.HEADER_DECORATOR - 1) // this needs to be executed after ServerTracingFilter
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext, Throwable t) {
@@ -18,11 +28,23 @@ public void filter(ContainerRequestContext requestContext, ContainerResponseCont
return;
}
SpanWrapper wrapper = (SpanWrapper) wrapperObj;
wrapper.getScope().close();
Tags.HTTP_STATUS.set(wrapper.get(), responseContext.getStatus());
if (t != null) {
FilterUtil.addExceptionLogs(wrapper.get(), t);
}
wrapper.finish();
}

@Override
public void aroundWriteTo(WriterInterceptorContext wic) throws IOException {
try {
wic.proceed();
} finally {
Object wrapperObj = wic.getProperty(SpanWrapper.PROPERTY_NAME);
if (wrapperObj instanceof SpanWrapper) {
SpanWrapper wrapper = (SpanWrapper) wrapperObj;
wrapper.getScope().close();
wrapper.finish();
}
}
}
}

0 comments on commit 5e9422d

Please sign in to comment.