Search in sources :

Example 1 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfHttpClient method execute.

/**
 * Send network metric for Apache HttpClient.execute(HttpHost target, HttpRequest request)
 *
 * @return HttpResponse from executing request
 * @throws IOException if unable to execute request or receive response
 */
static HttpResponse execute(final HttpClient client, final HttpHost target, final HttpRequest request, final Timer timer, final TransportManager transportManager) throws IOException {
    HttpResponse response = null;
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        builder.setUrl(target.toURI() + request.getRequestLine().getUri()).setHttpMethod(request.getRequestLine().getMethod());
        Long requestContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(request);
        if (requestContentLength != null) {
            builder.setRequestPayloadBytes(requestContentLength);
        }
        // Execute network Request
        timer.reset();
        builder.setRequestStartTimeMicros(timer.getMicros());
        response = client.execute(target, request);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        builder.setHttpResponseCode(response.getStatusLine().getStatusCode());
        Long responseContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(response);
        if (responseContentLength != null) {
            builder.setResponsePayloadBytes(responseContentLength);
        }
        String contentType = NetworkRequestMetricBuilderUtil.getApacheHttpResponseContentType(response);
        if (contentType != null) {
            builder.setResponseContentType(contentType);
        }
        builder.build();
    } catch (IOException e) {
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
    return response;
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Example 2 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfHttpClient method execute.

/**
 * Send network metric for Apache HttpClient.execute(HttpHost target, HttpRequest request,
 * ResponseHandler<? extends T> responseHandler, HttpContext context)
 *
 * @return <T> T
 * @throws IOException if unable to execute request or receive response
 */
static <T> T execute(final HttpClient client, final HttpHost target, final HttpRequest request, final ResponseHandler<? extends T> responseHandler, final HttpContext context, final Timer timer, final TransportManager transportManager) throws IOException {
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        builder.setUrl(target.toURI() + request.getRequestLine().getUri()).setHttpMethod(request.getRequestLine().getMethod());
        Long requestContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(request);
        if (requestContentLength != null) {
            builder.setRequestPayloadBytes(requestContentLength);
        }
        // Execute network Request
        timer.reset();
        builder.setRequestStartTimeMicros(timer.getMicros());
        return client.execute(target, request, new InstrumentApacheHttpResponseHandler<T>(responseHandler, timer, builder), context);
    } catch (IOException e) {
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
}
Also used : IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Example 3 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfHttpClient method execute.

/**
 * Send network metric for Apache HttpClient.execute(HttpUriRequest request, HttpContext context)
 *
 * @return HttpResponse from executing request
 * @throws IOException if unable to execute request or receive response
 */
static HttpResponse execute(final HttpClient client, final HttpUriRequest request, final HttpContext context, final Timer timer, final TransportManager transportManager) throws IOException {
    HttpResponse response = null;
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        builder.setUrl(request.getURI().toString()).setHttpMethod(request.getMethod());
        Long requestContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(request);
        if (requestContentLength != null) {
            builder.setRequestPayloadBytes(requestContentLength);
        }
        // Execute network Request
        timer.reset();
        builder.setRequestStartTimeMicros(timer.getMicros());
        response = client.execute(request, context);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        builder.setHttpResponseCode(response.getStatusLine().getStatusCode());
        Long responseContentLength = NetworkRequestMetricBuilderUtil.getApacheHttpMessageContentLength(response);
        if (responseContentLength != null) {
            builder.setResponsePayloadBytes(responseContentLength);
        }
        String contentType = NetworkRequestMetricBuilderUtil.getApacheHttpResponseContentType(response);
        if (contentType != null) {
            builder.setResponseContentType(contentType);
        }
        builder.build();
    } catch (IOException e) {
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
    return response;
}
Also used : HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)

Example 4 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfOkHttpClient method execute.

@Keep
public static Response execute(final Call call) throws IOException {
    final Response response;
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(TransportManager.getInstance());
    Timer timer = new Timer();
    long startTimeMicros = timer.getMicros();
    try {
        response = call.execute();
        long responseCompletedTimeMicros = timer.getDurationMicros();
        sendNetworkMetric(response, builder, startTimeMicros, responseCompletedTimeMicros);
    } catch (IOException e) {
        Request request = call.request();
        if (request != null) {
            HttpUrl url = request.url();
            if (url != null) {
                builder.setUrl(url.url().toString());
            }
            String method = request.method();
            if (method != null) {
                builder.setHttpMethod(request.method());
            }
        }
        builder.setRequestStartTimeMicros(startTimeMicros);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
    return response;
}
Also used : Response(okhttp3.Response) Timer(com.google.firebase.perf.util.Timer) Request(okhttp3.Request) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) HttpUrl(okhttp3.HttpUrl) Keep(androidx.annotation.Keep)

Example 5 with NetworkRequestMetricBuilder

use of com.google.firebase.perf.metrics.NetworkRequestMetricBuilder in project firebase-android-sdk by firebase.

the class FirebasePerfUrlConnection method getContent.

/**
 * Send network request metric for UrlConnection.getContent()
 *
 * @return the object fetched
 * @throws IOException if there is a problem with opening the connection or using the connection
 *     to get the content
 */
static Object getContent(final URLWrapper wrapper, TransportManager transportManager, Timer timer) throws IOException {
    timer.reset();
    long startTime = timer.getMicros();
    NetworkRequestMetricBuilder builder = NetworkRequestMetricBuilder.builder(transportManager);
    try {
        URLConnection connection = wrapper.openConnection();
        if (connection instanceof HttpsURLConnection) {
            return new InstrHttpsURLConnection((HttpsURLConnection) connection, timer, builder).getContent();
        } else if (connection instanceof HttpURLConnection) {
            return new InstrHttpURLConnection((HttpURLConnection) connection, timer, builder).getContent();
        }
        return connection.getContent();
    } catch (IOException e) {
        builder.setRequestStartTimeMicros(startTime);
        builder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
        builder.setUrl(wrapper.toString());
        NetworkRequestMetricBuilderUtil.logError(builder);
        throw e;
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) IOException(java.io.IOException) NetworkRequestMetricBuilder(com.google.firebase.perf.metrics.NetworkRequestMetricBuilder) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

NetworkRequestMetricBuilder (com.google.firebase.perf.metrics.NetworkRequestMetricBuilder)40 Test (org.junit.Test)27 NetworkRequestMetric (com.google.firebase.perf.v1.NetworkRequestMetric)13 IOException (java.io.IOException)12 HttpResponse (org.apache.http.HttpResponse)5 ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)4 HttpURLConnection (java.net.HttpURLConnection)3 URLConnection (java.net.URLConnection)3 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 HttpUrl (okhttp3.HttpUrl)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 TransportManager (com.google.firebase.perf.transport.TransportManager)2 RequestBody (okhttp3.RequestBody)2 ResponseBody (okhttp3.ResponseBody)2 Keep (androidx.annotation.Keep)1 GaugeManager (com.google.firebase.perf.session.gauges.GaugeManager)1 Timer (com.google.firebase.perf.util.Timer)1 ResponseHandler (org.apache.http.client.ResponseHandler)1