Skip to content

refactor: Replace APM API that uses reflection by a package private API #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ rootProject.allprojects {
repositories {
google()
mavenCentral()
maven {
url "https://mvn.instabug.com/nexus/repository/instabug-internal/"
credentials {
username "instabug"
password System.getenv("INSTABUG_REPOSITORY_PASSWORD")
}
}
}
}


apply plugin: 'com.android.library'

android {
Expand All @@ -44,11 +52,10 @@ android {
}

dependencies {
api 'com.instabug.library:instabug:14.3.0'
api 'com.instabug.library:instabug:14.3.0.6752106-SNAPSHOT'
testImplementation 'junit:junit:4.13.2'
testImplementation "org.mockito:mockito-inline:3.12.1"
testImplementation "io.mockk:mockk:1.13.13"

}

// add upload_symbols task
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.instabug.apm.networking;


import androidx.annotation.NonNull;

import com.instabug.apm.networking.mapping.NetworkRequestAttributes;
import com.instabug.apm.networkinterception.cp.APMCPNetworkLog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class ApmNetworkLoggerHelper {

/// Log network request to the Android SDK using a package private API [APMNetworkLogger.log]
static public void log(@NonNull Map<String, Object> data) {
try {
APMNetworkLogger apmNetworkLogger = new APMNetworkLogger();
final String requestUrl = (String) data.get("url");
final String requestBody = (String) data.get("requestBody");
final String responseBody = (String) data.get("responseBody");
final String requestMethod = (String) data.get("method");
//--------------------------------------------
final String requestContentType = (String) data.get("requestContentType");
final String responseContentType = (String) data.get("responseContentType");
//--------------------------------------------
final long requestBodySize = ((Number) data.get("requestBodySize")).longValue();
final long responseBodySize = ((Number) data.get("responseBodySize")).longValue();
//--------------------------------------------
final String errorDomain = (String) data.get("errorDomain");
final Integer statusCode = (Integer) data.get("responseCode");
final long requestDuration = ((Number) data.get("duration")).longValue() / 1000;
final long requestStartTime = ((Number) data.get("startTime")).longValue() * 1000;
final String requestHeaders = (new JSONObject((HashMap<String, String>) data.get("requestHeaders"))).toString(4);
final String responseHeaders = (new JSONObject((HashMap<String, String>) data.get("responseHeaders"))).toString(4);
final String errorMessage;

if (errorDomain.equals("")) {
errorMessage = null;
} else {
errorMessage = errorDomain;
}
//--------------------------------------------------
String gqlQueryName = null;
if (data.containsKey("gqlQueryName")) {
gqlQueryName = (String) data.get("gqlQueryName");
}
String serverErrorMessage = "";
if (data.containsKey("serverErrorMessage")) {
serverErrorMessage = (String) data.get("serverErrorMessage");
}
Boolean isW3cHeaderFound = null;
Number partialId = null;
Number networkStartTimeInSeconds = null;
String w3CGeneratedHeader = null;
String w3CCaughtHeader = null;

if (data.containsKey("isW3cHeaderFound")) {
isW3cHeaderFound = (Boolean) data.get("isW3cHeaderFound");
}

if (data.containsKey("partialId")) {


partialId = ((Number) data.get("partialId"));

}
if (data.containsKey("networkStartTimeInSeconds")) {
networkStartTimeInSeconds = ((Number) data.get("networkStartTimeInSeconds"));
}

if (data.containsKey("w3CGeneratedHeader")) {

w3CGeneratedHeader = (String) data.get("w3CGeneratedHeader");


}
if (data.containsKey("w3CCaughtHeader")) {
w3CCaughtHeader = (String) data.get("w3CCaughtHeader");

}

NetworkRequestAttributes requestAttributes = new NetworkRequestAttributes(
requestStartTime * 1000,
requestDuration,
requestHeaders,
requestBody,
requestBodySize,
requestMethod,
requestUrl,
requestContentType,
responseHeaders,
responseBody,
responseBodySize,
statusCode,
responseContentType,
gqlQueryName,
errorMessage,
serverErrorMessage
);

APMCPNetworkLog.W3CExternalTraceAttributes w3cExternalTraceAttributes =
null;
if (isW3cHeaderFound != null) {
w3cExternalTraceAttributes = new APMCPNetworkLog.W3CExternalTraceAttributes(
isW3cHeaderFound, partialId == null ? null : partialId.longValue(),
networkStartTimeInSeconds == null ? null : networkStartTimeInSeconds.longValue(),
w3CGeneratedHeader, w3CCaughtHeader

);
}

apmNetworkLogger.log(requestAttributes, w3cExternalTraceAttributes);

} catch (Exception e) {
e.printStackTrace();
}
}
}
Loading