use of io.opencensus.contrib.http.HttpRequestContext in project instrumentation-java by census-instrumentation.
the class JaxrsContainerFilter method filter.
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
HttpRequestContext context = (HttpRequestContext) requestContext.getProperty(CONTEXT_PROPERTY);
if (context == null) {
// request came through this filter
return;
}
Scope scope = (Scope) requestContext.getProperty(SPAN_PROPERTY);
if (scope != null) {
scope.close();
}
if (responseContext.getLength() > 0) {
handler.handleMessageSent(context, responseContext.getLength());
}
ExtendedContainerRequest extendedRequest = new ExtendedContainerRequest(requestContext, info);
handler.handleEnd(context, extendedRequest, responseContext, null);
}
use of io.opencensus.contrib.http.HttpRequestContext in project instrumentation-java by census-instrumentation.
the class OcHttpServletFilter method doFilter.
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// only interested in http requests
if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) {
HttpServletRequest httpReq = (HttpServletRequest) request;
HttpServletResponse httpResp = (HttpServletResponse) response;
HttpRequestContext context = handler.handleStart(httpReq, httpReq);
OcHttpServletListener listener = new OcHttpServletListener(handler, context);
httpReq.setAttribute(OcHttpServletUtil.OPENCENSUS_SERVLET_LISTENER, listener);
int length = httpReq.getContentLength();
if (length > 0) {
handler.handleMessageReceived(context, length);
}
Scope scope = Tracing.getTracer().withSpan(handler.getSpanFromContext(context));
try {
chain.doFilter(httpReq, httpResp);
} finally {
scope.close();
}
if (httpReq.isAsyncStarted()) {
AsyncContext async = httpReq.getAsyncContext();
async.addListener(listener, httpReq, httpResp);
} else {
OcHttpServletUtil.recordMessageSentEvent(handler, context, httpResp);
handler.handleEnd(context, httpReq, httpResp, null);
}
} else {
// pass request through unchanged
chain.doFilter(request, response);
}
}
use of io.opencensus.contrib.http.HttpRequestContext in project instrumentation-java by census-instrumentation.
the class TracingAsyncClientHttpRequestInterceptor method intercept.
/**
* It intercepts http requests and starts a span.
*
* @since 0.23.0
*/
public ListenableFuture<ClientHttpResponse> intercept(HttpRequest request, byte[] body, org.springframework.http.client.AsyncClientHttpRequestExecution execution) throws IOException {
HttpRequestContext context = handler.handleStart(tracer.getCurrentSpan(), request, request);
Scope ws = tracer.withSpan(handler.getSpanFromContext(context));
try {
ListenableFuture<ClientHttpResponse> result = execution.executeAsync(request, body);
result.addCallback(new TracingAsyncClientHttpRequestInterceptor.TraceListenableFutureCallback(context, handler));
return result;
} catch (IOException e) {
handler.handleEnd(context, null, null, e);
throw e;
} finally {
if (ws != null) {
ws.close();
}
}
}
use of io.opencensus.contrib.http.HttpRequestContext in project instrumentation-java by census-instrumentation.
the class JaxrsContainerFilter method filter.
@Override
// Close will happen in response filter method
@SuppressWarnings("MustBeClosedChecker")
public void filter(ContainerRequestContext requestContext) throws IOException {
ExtendedContainerRequest extendedRequest = new ExtendedContainerRequest(requestContext, info);
HttpRequestContext context = handler.handleStart(requestContext, extendedRequest);
requestContext.setProperty(CONTEXT_PROPERTY, context);
if (requestContext.getLength() > 0) {
handler.handleMessageReceived(context, requestContext.getLength());
}
requestContext.setProperty(SPAN_PROPERTY, Tracing.getTracer().withSpan(handler.getSpanFromContext(context)));
}
use of io.opencensus.contrib.http.HttpRequestContext in project instrumentation-java by census-instrumentation.
the class JaxrsClientFilterTest method testResponseFilter.
@Test
public void testResponseFilter() throws Exception {
Span span = new FakeSpan(SpanContext.INVALID, null);
TagContext tagContext = mock(TagContext.class);
HttpRequestContext context = createHttpRequestContext(span, tagContext);
ClientRequestContext requestContext = mock(ClientRequestContext.class);
when(requestContext.getProperty("opencensus.context")).thenReturn(context);
ClientResponseContext responseContext = mock(ClientResponseContext.class);
filter.filter(requestContext, responseContext);
verify(requestContext).getProperty("opencensus.context");
verify(responseContext, times(1)).getStatus();
}
Aggregations