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

kamon.instrumentation.apache.httpclient.ResponseHandlerProxy Maven / Gradle / Ivy

The newest version!
package kamon.instrumentation.apache.httpclient;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;

import kamon.Kamon;
import kamon.context.Context;
import kamon.context.Storage.Scope;
import kamon.instrumentation.http.HttpClientInstrumentation.RequestHandler;

public class ResponseHandlerProxy implements ResponseHandler {

    private final ResponseHandler delegate;
    private final RequestHandler handler;
    private Context parentContext;

    public ResponseHandlerProxy(RequestHandler handler, ResponseHandler delegate, Context parentContext) {
        this.handler = handler;
        this.delegate = delegate;
        this.parentContext = parentContext;
    }

    @Override
    public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
        ApacheHttpClientInstrumentation.processResponse(handler, response, null);
        // run original handler in parent context to avoid nesting of spans
        try (Scope ignored = Kamon.storeContext(parentContext)) {
            return delegate.handleResponse(response);
        }
    }

}