Search in sources :

Example 36 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)
 *
 * @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 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));
    } 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 37 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, 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 HttpUriRequest request, final ResponseHandler<T> responseHandler, final HttpContext context, final Timer timer, final TransportManager transportManager) throws IOException {
    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());
        return client.execute(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 38 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,
 * HttpContext context)
 *
 * @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 HttpContext context, 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, 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 39 with NetworkRequestMetricBuilder

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

the class FirebasePerfUrlConnection method openStream.

// Functions to collect information for the Network Request Metric
/**
 * Send network request metric for UrlConnection.getInputStream()
 *
 * @return InputStream the input stream from the url connection
 * @throws IOException if there is a problem with opening the connection or using the connection
 *     to open a stream
 */
static InputStream openStream(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).getInputStream();
        } else if (connection instanceof HttpURLConnection) {
            return new InstrHttpURLConnection((HttpURLConnection) connection, timer, builder).getInputStream();
        }
        return connection.getInputStream();
    } 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)

Example 40 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(classes)
 *
 * @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, @SuppressWarnings("rawtypes") final Class[] types, 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(types);
        } else if (connection instanceof HttpURLConnection) {
            return new InstrHttpURLConnection((HttpURLConnection) connection, timer, builder).getContent(types);
        }
        return connection.getContent(types);
    } 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