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

com.eg.agent.android.instrumentation.okhttp3.OkHttp3TransactionStateUtil Maven / Gradle / Ivy

There is a newer version: 2.1.3
Show newest version
package com.eg.agent.android.instrumentation.okhttp3;

/*import com.newrelic.agent.android.Measurements;
import com.newrelic.agent.android.TaskQueue;
import com.newrelic.agent.android.api.common.TransactionData;
import com.newrelic.agent.android.instrumentation.TransactionState;
import com.newrelic.agent.android.instrumentation.TransactionStateUtil;
import com.newrelic.agent.android.logging.AgentLog;
import com.newrelic.agent.android.logging.AgentLogManager;
import com.newrelic.agent.android.measurement.http.HttpTransactionMeasurement;*/

import com.eg.agent.android.Measurements;
import com.eg.agent.android.common.TransactionData;
import com.eg.agent.android.TaskQueue;
import com.eg.agent.android.instrumentation.TransactionState;
import com.eg.agent.android.instrumentation.TransactionStateUtil;
import com.eg.agent.android.logging.AgentLog;
import com.eg.agent.android.logging.AgentLogManager;
import com.eg.agent.android.measurement.http.HttpTransactionMeasurement;
import com.eg.google.gson.Gson;

import org.apache.http.HttpStatus;

import java.util.Map;
import java.util.TreeMap;

import okhttp3.Request;
import okhttp3.Response;

public class OkHttp3TransactionStateUtil extends TransactionStateUtil {
    private static final AgentLog log = AgentLogManager.getAgentLog();

    public static void inspectAndInstrument(TransactionState transactionState, Request request) {
        System.out.println("test ok http 0 :: ");
        if (request == null) {
            log.warning("Missing request");
        } else {
            TransactionStateUtil.inspectAndInstrument(transactionState, request.url().toString(), request.method());
        }
    }

    public static Response inspectAndInstrumentResponse(TransactionState transactionState, Response response) {
        int statusCode;
        String appData = "";
        long contentLength = 0;
        if (response == null) {
            statusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
            log.warning("Missing response");
        } else {
            appData = response.header(TransactionStateUtil.APP_DATA_HEADER);
            statusCode = response.code();
            try {
                contentLength = response.body().contentLength();
            } catch (Exception e) {
                log.warning("Missing body or content length");
            }
        }
        TransactionStateUtil.inspectAndInstrumentResponse(transactionState, appData, (int) contentLength, statusCode);
        System.out.println("test ok http :: "+ new Gson().toJson(transactionState.asJsonObject()));
        return addTransactionAndErrorData(transactionState, response);
    }

    private static Response addTransactionAndErrorData(TransactionState transactionState, Response response) {
        System.out.println("test ok http error :: ");
        TransactionData transactionData = transactionState.end();
        if (transactionData != null) {
            TaskQueue.queue(new HttpTransactionMeasurement(transactionData.getUrl(), transactionData.getHttpMethod(), transactionData.getStatusCode(), transactionData.getErrorCode(), transactionData.getTimestamp(), (double) transactionData.getTime(), transactionData.getBytesSent(), transactionData.getBytesReceived(), transactionData.getAppData()));
            if (((long) transactionState.getStatusCode()) >= 400 && response != null) {
                String contentTypeHeader = response.header("Content-Type");
                Map params = new TreeMap();
                if (!(contentTypeHeader == null || contentTypeHeader.length() <= 0 || "".equals(contentTypeHeader))) {
                    params.put("content_type", null);
                }
                params.put("content_length", transactionState.getBytesReceived() + "");
                String responseBodyString = "";
                try {
                    responseBodyString = response.peekBody(response.body().contentLength()).string();
                } catch (Exception e) {
                    if (response.message() != null) {
                        log.warning("Missing response body, using response message");
                        responseBodyString = response.message();
                    }
                }
                Measurements.addHttpError(transactionData.getUrl(), transactionData.getHttpMethod(), transactionData.getStatusCode(), responseBodyString, params);
            }
        }
        return response;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy