Search in sources :

Example 1 with HttpRequestContext

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);
}
Also used : Scope(io.opencensus.common.Scope) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext)

Example 2 with HttpRequestContext

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Scope(io.opencensus.common.Scope) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) AsyncContext(javax.servlet.AsyncContext)

Example 3 with HttpRequestContext

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();
        }
    }
}
Also used : Scope(io.opencensus.common.Scope) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) IOException(java.io.IOException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse)

Example 4 with HttpRequestContext

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)));
}
Also used : HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext)

Example 5 with HttpRequestContext

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();
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) TagContext(io.opencensus.tags.TagContext) HttpRequestContext(io.opencensus.contrib.http.HttpRequestContext) Span(io.opencensus.trace.Span) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) Test(org.junit.Test)

Aggregations

HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)8 Scope (io.opencensus.common.Scope)3 TagContext (io.opencensus.tags.TagContext)2 Span (io.opencensus.trace.Span)2 Test (org.junit.Test)2 FakeSpan (io.opencensus.contrib.http.jaxrs.JaxrsClientFilterTest.FakeSpan)1 IOException (java.io.IOException)1 AsyncContext (javax.servlet.AsyncContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 ClientRequestContext (javax.ws.rs.client.ClientRequestContext)1 ClientResponseContext (javax.ws.rs.client.ClientResponseContext)1 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)1 ContainerResponseContext (javax.ws.rs.container.ContainerResponseContext)1 UriInfo (javax.ws.rs.core.UriInfo)1 ClientHttpResponse (org.springframework.http.client.ClientHttpResponse)1