All Downloads are FREE. Search and download functionalities are using the official Maven repository.

enhance.HttpClient_executessist Maven / Gradle / Ivy

There is a newer version: 2.3.4
Show newest version
{
    // Grab the useful parameters. ApacheHttpClientMethodHandler will either pass in
    // $N (where N is 1, 2, 3, 4, …) or null if this parameter isn't available in this
    // version of HttpClient.execute().
    HttpHost host = %HTTP_HOST_PARAM%;
    HttpRequest request = %HTTP_REQUEST_PARAM%;
    HttpUriRequest uriRequest = %HTTP_REQUEST_URI_PARAM%;

    // Start the span.
    final String name = String.format("%s %s", new Object[] {request.getRequestLine().getMethod(), request.getRequestLine().getUri()});
    Span span = SpanHelper.startSpan(name);

    // Set the request headers to link up this client request with the corresponding
    // service request.
    if (span.getTraceId() != null ) {
        request.setHeader(HttpHeaderNames.TRACE_ID, String.valueOf(span.getTraceId()));
    }
    if (span.getSpanId() != null) {
        request.setHeader(HttpHeaderNames.SPAN_ID, String.valueOf(span.getSpanId()));
    }
     org.slf4j.MDC.put("traceId", String.valueOf(span.getTraceId()));
     org.slf4j.MDC.put("spanId", String.valueOf(span.getSpanId()));
    try {
        // Generate the "client has sent its request" annotation.
        span.addAnnotation(AnnotationNames.CLIENT_SENT);

        // Execute the HTTP request, return the response.
        return $proceed($$);
    } catch (Exception e) {
        // Some exception happened. Let's generate an exception annotation without getting in the way.
        span.addAnnotation(AnnotationNames.CLIENT_EXCEPTION, e.getClass().getName());
        throw e;
    } finally {
        // Generate the "client has received the response" annotation.
        // TODO: Should this move to immediately after $proceed($$)? Is it such a bad thing if ClientReceived isn't logged when an exception is thrown?
        span.addAnnotation(AnnotationNames.CLIENT_RECEIVED);
        span.end();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy