|
| 1 | +package com.instabug.apm.networking; |
| 2 | + |
| 3 | +import androidx.annotation.Nullable; |
| 4 | + |
| 5 | +import com.facebook.react.bridge.ReadableMap; |
| 6 | +import com.instabug.apm.networking.mapping.NetworkRequestAttributes; |
| 7 | +import com.instabug.apm.networkinterception.cp.APMCPNetworkLog; |
| 8 | + |
| 9 | +public class ApmNetworkLoggerHelper { |
| 10 | + |
| 11 | + /// Log network request to the Android SDK using a package private API [APMNetworkLogger.log] |
| 12 | + static public void log(final double requestStartTime, |
| 13 | + final double requestDuration, |
| 14 | + final String requestHeaders, |
| 15 | + final String requestBody, |
| 16 | + final double requestBodySize, |
| 17 | + final String requestMethod, |
| 18 | + final String requestUrl, |
| 19 | + final String requestContentType, |
| 20 | + final String responseHeaders, |
| 21 | + final String responseBody, |
| 22 | + final double responseBodySize, |
| 23 | + final double statusCode, |
| 24 | + final String responseContentType, |
| 25 | + @Nullable final String errorDomain, |
| 26 | + @Nullable final ReadableMap w3cAttributes, |
| 27 | + @Nullable final String gqlQueryName, |
| 28 | + @Nullable final String serverErrorMessage |
| 29 | + ) { |
| 30 | + try { |
| 31 | + final APMNetworkLogger apmNetworkLogger = new APMNetworkLogger(); |
| 32 | + final boolean hasError = errorDomain != null && !errorDomain.isEmpty(); |
| 33 | + final String errorMessage = hasError ? errorDomain : null; |
| 34 | + boolean isW3cHeaderFound = false; |
| 35 | + Long partialId = null; |
| 36 | + Long networkStartTimeInSeconds = null; |
| 37 | + |
| 38 | + |
| 39 | + try { |
| 40 | + if (!w3cAttributes.isNull("isW3cHeaderFound")) { |
| 41 | + isW3cHeaderFound = w3cAttributes.getBoolean("isW3cHeaderFound"); |
| 42 | + } |
| 43 | + |
| 44 | + if (!w3cAttributes.isNull("partialId")) { |
| 45 | + partialId = (long) w3cAttributes.getDouble("partialId"); |
| 46 | + networkStartTimeInSeconds = (long) w3cAttributes.getDouble("networkStartTimeInSeconds"); |
| 47 | + } |
| 48 | + |
| 49 | + } catch (Exception e) { |
| 50 | + e.printStackTrace(); |
| 51 | + } |
| 52 | + APMCPNetworkLog.W3CExternalTraceAttributes w3cExternalTraceAttributes = |
| 53 | + new APMCPNetworkLog.W3CExternalTraceAttributes( |
| 54 | + isW3cHeaderFound, |
| 55 | + partialId, |
| 56 | + networkStartTimeInSeconds, |
| 57 | + w3cAttributes.getString("w3cGeneratedHeader"), |
| 58 | + w3cAttributes.getString("w3cCaughtHeader") |
| 59 | + ); |
| 60 | + NetworkRequestAttributes requestAttributes = new NetworkRequestAttributes( |
| 61 | + (long) requestStartTime * 1000, |
| 62 | + (long) requestDuration, |
| 63 | + requestHeaders, |
| 64 | + requestBody, |
| 65 | + (long) requestBodySize, |
| 66 | + requestMethod, |
| 67 | + requestUrl, |
| 68 | + requestContentType, |
| 69 | + responseHeaders, |
| 70 | + responseBody, |
| 71 | + (long) responseBodySize, |
| 72 | + (int) statusCode, |
| 73 | + responseContentType, |
| 74 | + gqlQueryName, |
| 75 | + errorMessage, |
| 76 | + serverErrorMessage |
| 77 | + ); |
| 78 | + |
| 79 | + apmNetworkLogger.log( |
| 80 | + requestAttributes, |
| 81 | + w3cExternalTraceAttributes |
| 82 | + ); |
| 83 | + } catch (Throwable e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments