导航菜单

页面标题

页面副标题

xDrip+ v04633772025.07.16 - NewRelic.java 源代码

正在查看: xDrip+ v04633772025.07.16 应用的 NewRelic.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.newrelic.agent.android;

import android.content.Context;
import android.text.TextUtils;
import com.newrelic.agent.android.agentdata.AgentDataController;
import com.newrelic.agent.android.analytics.AnalyticAttribute;
import com.newrelic.agent.android.analytics.AnalyticsControllerImpl;
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.logging.AndroidAgentLog;
import com.newrelic.agent.android.logging.NullAgentLog;
import com.newrelic.agent.android.measurement.http.HttpTransactionMeasurement;
import com.newrelic.agent.android.metric.MetricUnit;
import com.newrelic.agent.android.stats.StatsEngine;
import com.newrelic.agent.android.tracing.TraceMachine;
import com.newrelic.agent.android.tracing.TracingInactiveException;
import com.newrelic.agent.android.util.Constants;
import com.newrelic.agent.android.util.NetworkFailure;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.apache.http.Header;
import org.apache.http.HttpResponse;

public final class NewRelic {
    private static final String UNKNOWN_HTTP_REQUEST_TYPE = "unknown";
    protected static final AgentLog log = AgentLogManager.getAgentLog();
    protected static final AgentConfiguration agentConfiguration = new AgentConfiguration();
    protected static boolean started = false;
    protected boolean loggingEnabled = true;
    protected int logLevel = 3;

    protected NewRelic(String str) {
        agentConfiguration.setApplicationToken(str);
    }

    public static NewRelic withApplicationToken(String str) {
        return new NewRelic(str);
    }

    public NewRelic usingSsl(boolean z) {
        agentConfiguration.setUseSsl(z);
        return this;
    }

    public NewRelic usingCollectorAddress(String str) {
        agentConfiguration.setCollectorHost(str);
        return this;
    }

    public NewRelic usingCrashCollectorAddress(String str) {
        agentConfiguration.setCrashCollectorHost(str);
        return this;
    }

    public NewRelic withLocationServiceEnabled(boolean z) {
        agentConfiguration.setUseLocationService(z);
        return this;
    }

    public NewRelic withLoggingEnabled(boolean z) {
        this.loggingEnabled = z;
        return this;
    }

    public NewRelic withLogLevel(int i) {
        this.logLevel = i;
        return this;
    }

    public NewRelic withCrashReportingEnabled(boolean z) {
        agentConfiguration.setReportCrashes(z);
        if (z) {
            enableFeature(FeatureFlag.CrashReporting);
        } else {
            disableFeature(FeatureFlag.CrashReporting);
        }
        return this;
    }

    public NewRelic withHttpResponseBodyCaptureEnabled(boolean z) {
        if (z) {
            enableFeature(FeatureFlag.HttpResponseBodyCapture);
        } else {
            disableFeature(FeatureFlag.HttpResponseBodyCapture);
        }
        return this;
    }

    public NewRelic withApplicationVersion(String str) {
        if (str != null) {
            agentConfiguration.setCustomApplicationVersion(str);
        }
        return this;
    }

    public NewRelic withApplicationFramework(ApplicationPlatform applicationPlatform) {
        if (applicationPlatform != null) {
            agentConfiguration.setApplicationPlatform(applicationPlatform);
        }
        return this;
    }

    @Deprecated
    public NewRelic withAnalyticsEvents(boolean z) {
        agentConfiguration.setEnableAnalyticsEvents(z);
        return this;
    }

    public NewRelic withInteractionTracing(boolean z) {
        if (z) {
            enableFeature(FeatureFlag.InteractionTracing);
        } else {
            disableFeature(FeatureFlag.InteractionTracing);
        }
        return this;
    }

    public NewRelic withDefaultInteractions(boolean z) {
        if (z) {
            enableFeature(FeatureFlag.DefaultInteractions);
        } else {
            disableFeature(FeatureFlag.DefaultInteractions);
        }
        return this;
    }

    public static void enableFeature(FeatureFlag featureFlag) {
        log.debug("Enable feature: " + featureFlag.name());
        FeatureFlag.enableFeature(featureFlag);
    }

    public static void disableFeature(FeatureFlag featureFlag) {
        log.debug("Disable feature: " + featureFlag.name());
        FeatureFlag.disableFeature(featureFlag);
    }

    @Deprecated
    public NewRelic withBuildIdentifier(String str) {
        StatsEngine.get().inc("Supportability/AgentHealth/Deprecated/WithBuildIdentifier");
        return withApplicationBuild(str);
    }

    public NewRelic withApplicationBuild(String str) {
        if (!TextUtils.isEmpty(str)) {
            agentConfiguration.setCustomBuildIdentifier(str);
        }
        return this;
    }

    public void start(Context context) {
        if (started) {
            log.debug("NewRelic is already running.");
            return;
        }
        try {
            AgentLogManager.setAgentLog(this.loggingEnabled ? new AndroidAgentLog() : new NullAgentLog());
            AgentLog agentLog = log;
            agentLog.setLevel(this.logLevel);
            if (!InstantApps.isInstantApp(context) && !isInstrumented()) {
                agentLog.error("Failed to detect New Relic instrumentation.  Something likely went wrong during your build process and you should visit http://support.newrelic.com.");
                return;
            }
            AndroidAgentImpl.init(context, agentConfiguration);
            started = true;
            if (agentLog.getLevel() >= 6) {
                StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
                if (stackTrace.length > 3) {
                    StackTraceElement stackTraceElement = stackTrace[3];
                    agentLog.audit("Agent started from " + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName() + ":" + stackTraceElement.getLineNumber());
                }
            }
        } catch (Throwable th) {
            log.error("Error occurred while starting the New Relic agent!", th);
        }
    }

    public static boolean isStarted() {
        return started;
    }

    @Deprecated
    public static void shutdown() {
        StatsEngine.get().inc("Supportability/AgentHealth/Deprecated/Shutdown");
        if (started) {
            try {
                Agent.getImpl().stop();
            } finally {
                Agent.setImpl(NullAgentImpl.instance);
                started = false;
            }
        }
    }

    private boolean isInstrumented() {
        log.info("isInstrumented: checking for Mono instrumentation flag - " + Agent.getMonoInstrumentationFlag());
        return Agent.getMonoInstrumentationFlag().equals("YES");
    }

    public static String startInteraction(String str) {
        checkNull(str, "startInteraction: actionName must be an action/method name.");
        log.debug("NewRelic.startInteraction invoked. actionName: " + str);
        TraceMachine.startTracing(str.replace("/", "."), true, FeatureFlag.featureEnabled(FeatureFlag.InteractionTracing));
        try {
            return TraceMachine.getActivityTrace().getId();
        } catch (TracingInactiveException unused) {
            return null;
        }
    }

    @Deprecated
    public static String startInteraction(Context context, String str) {
        checkNull(context, "startInteraction: context must be an Activity instance.");
        checkNull(str, "startInteraction: actionName must be an action/method name.");
        TraceMachine.startTracing(context.getClass().getSimpleName() + "#" + str.replace("/", "."), false, FeatureFlag.featureEnabled(FeatureFlag.InteractionTracing));
        try {
            return TraceMachine.getActivityTrace().getId();
        } catch (TracingInactiveException unused) {
            return null;
        }
    }

    @Deprecated
    public static String startInteraction(Context context, String str, boolean z) {
        if (TraceMachine.isTracingActive() && !z) {
            log.warning("startInteraction: An interaction is already being traced, and invalidateActiveTrace is false. This interaction will not be traced.");
            return null;
        }
        return startInteraction(context, str);
    }

    public static void endInteraction(String str) {
        log.debug("NewRelic.endInteraction invoked. id: " + str);
        TraceMachine.endTrace(str);
    }

    public static void setInteractionName(String str) {
        TraceMachine.setRootDisplayName(str);
    }

    public static void startMethodTrace(String str) {
        checkNull(str, "startMethodTrace: actionName must be an action/method name.");
        TraceMachine.enterMethod(str);
    }

    public static void endMethodTrace() {
        log.debug("NewRelic.endMethodTrace invoked.");
        TraceMachine.exitMethod();
    }

    public static void recordMetric(String str, String str2, int i, double d, double d2) {
        recordMetric(str, str2, i, d, d2, null, null);
    }

    public static void recordMetric(String str, String str2, int i, double d, double d2, MetricUnit metricUnit, MetricUnit metricUnit2) {
        log.debug("NewRelic.recordMeric invoked for name " + str + ", category: " + str2 + ", count: " + i + ", totalValue " + d + ", exclusiveValue: " + d2 + ", countUnit: " + metricUnit + ", valueUnit: " + metricUnit2);
        checkNull(str2, "recordMetric: category must not be null. If no MetricCategory is applicable, use MetricCategory.NONE.");
        checkEmpty(str, "recordMetric: name must not be empty.");
        if (checkNegative(i, "recordMetric: count must not be negative.")) {
            return;
        }
        Measurements.addCustomMetric(str, str2, i, d, d2, metricUnit, metricUnit2);
    }

    public static void recordMetric(String str, String str2, double d) {
        recordMetric(str, str2, 1, d, d, null, null);
    }

    public static void recordMetric(String str, String str2) {
        recordMetric(str, str2, 1.0d);
    }

    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4) {
        _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, null, null, null);
    }

    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3) {
        _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, null, null);
    }

    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3, Map<String, String> map) {
        _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, null);
    }

    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3, Map<String, String> map, String str4) {
        _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, str4);
    }

    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3, Map<String, String> map, URLConnection uRLConnection) {
        String headerField;
        if (uRLConnection != null && (headerField = uRLConnection.getHeaderField(Constants.Network.CROSS_PROCESS_ID_HEADER)) != null && headerField.length() > 0) {
            _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, headerField);
        } else {
            _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, null);
        }
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3, Map<String, String> map, HttpResponse httpResponse) {
        Header firstHeader;
        if (httpResponse != null && (firstHeader = httpResponse.getFirstHeader(Constants.Network.CROSS_PROCESS_ID_HEADER)) != null && firstHeader.getValue() != null && firstHeader.getValue().length() > 0) {
            _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, firstHeader.getValue());
        } else {
            _noticeHttpTransaction(str, str2, i, j, j2, j3, j4, str3, map, null);
        }
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4, String str2, Map<String, String> map, HttpResponse httpResponse) {
        noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, str2, map, httpResponse);
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4, String str2, Map<String, String> map, URLConnection uRLConnection) {
        noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, str2, map, uRLConnection);
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4) {
        _noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, null, null, null);
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4, String str2) {
        _noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, str2, null, null);
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4, String str2, Map<String, String> map) {
        _noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, str2, map, null);
    }

    @Deprecated
    public static void noticeHttpTransaction(String str, int i, long j, long j2, long j3, long j4, String str2, Map<String, String> map, String str3) {
        _noticeHttpTransaction(str, "unknown", i, j, j2, j3, j4, str2, map, str3);
    }

    protected static void _noticeHttpTransaction(String str, String str2, int i, long j, long j2, long j3, long j4, String str3, Map<String, String> map, String str4) {
        checkEmpty(str, "noticeHttpTransaction: url must not be empty.");
        checkEmpty(str2, "noticeHttpTransaction: httpMethod must not be empty.");
        try {
            new URL(str);
            double d = j2 - j;
            if (checkNegative((int) d, "noticeHttpTransaction: the startTimeMs is later than the endTimeMs, resulting in a negative total time.")) {
                return;
            }
            TaskQueue.queue(new HttpTransactionMeasurement(str, str2, i, 0, j, d / 1000.0d, j3, j4, str4));
            if (i >= 400) {
                Measurements.addHttpError(str, str2, i, str3, map);
            }
        } catch (MalformedURLException unused) {
            throw new IllegalArgumentException("noticeHttpTransaction: URL is malformed: " + str);
        }
    }

    private static void noticeNetworkFailureDelegate(String str, String str2, long j, long j2, NetworkFailure networkFailure, String str3) {
        TransactionState transactionState = new TransactionState();
        TransactionStateUtil.inspectAndInstrument(transactionState, str, str2);
        transactionState.setErrorCode(networkFailure.getErrorCode());
        TransactionData end = transactionState.end();
        TreeMap treeMap = new TreeMap();
        treeMap.put(Constants.Transactions.CONTENT_LENGTH, "0");
        treeMap.put(Constants.Transactions.CONTENT_TYPE, "text/html");
        end.setResponseBody(str3);
        end.setParams(treeMap);
        TaskQueue.queue(new HttpTransactionMeasurement(end));
        Measurements.addHttpError(end);
    }

    public static void noticeNetworkFailure(String str, String str2, long j, long j2, NetworkFailure networkFailure, String str3) {
        noticeNetworkFailureDelegate(str, str2, j, j2, networkFailure, str3);
    }

    public static void noticeNetworkFailure(String str, String str2, long j, long j2, NetworkFailure networkFailure) {
        noticeNetworkFailure(str, str2, j, j2, networkFailure, "");
    }

    public static void noticeNetworkFailure(String str, String str2, long j, long j2, Exception exc) {
        checkEmpty(str, "noticeHttpException: url must not be empty.");
        noticeNetworkFailure(str, str2, j, j2, NetworkFailure.exceptionToNetworkFailure(exc), exc.getMessage());
    }

    @Deprecated
    public static void noticeNetworkFailure(String str, long j, long j2, NetworkFailure networkFailure) {
        noticeNetworkFailure(str, "unknown", j, j2, networkFailure);
    }

    @Deprecated
    public static void noticeNetworkFailure(String str, long j, long j2, Exception exc) {
        noticeNetworkFailure(str, "unknown", j, j2, exc);
    }

    private static void checkNull(Object obj, String str) {
        if (obj == null) {
            throw new IllegalArgumentException(str);
        }
    }

    private static void checkEmpty(String str, String str2) {
        checkNull(str, str2);
        if (str.length() == 0) {
            throw new IllegalArgumentException(str2);
        }
    }

    private static boolean checkNegative(int i, String str) {
        if (i >= 0) {
            return false;
        }
        log.error(str);
        return true;
    }

    public static void crashNow() {
        crashNow("This is a demonstration crash courtesy of New Relic");
    }

    public static void crashNow(String str) {
        throw new RuntimeException(str);
    }

    public static boolean setAttribute(String str, String str2) {
        return AnalyticsControllerImpl.getInstance().setAttribute(str, str2);
    }

    public static boolean setAttribute(String str, double d) {
        return AnalyticsControllerImpl.getInstance().setAttribute(str, d);
    }

    public static boolean setAttribute(String str, boolean z) {
        return AnalyticsControllerImpl.getInstance().setAttribute(str, z);
    }

    public static boolean incrementAttribute(String str) {
        return AnalyticsControllerImpl.getInstance().incrementAttribute(str, 1.0d);
    }

    public static boolean incrementAttribute(String str, double d) {
        return AnalyticsControllerImpl.getInstance().incrementAttribute(str, d);
    }

    public static boolean removeAttribute(String str) {
        return AnalyticsControllerImpl.getInstance().removeAttribute(str);
    }

    public static boolean removeAllAttributes() {
        return AnalyticsControllerImpl.getInstance().removeAllAttributes();
    }

    public static boolean setUserId(String str) {
        return AnalyticsControllerImpl.getInstance().setAttribute(AnalyticAttribute.USER_ID_ATTRIBUTE, str);
    }

    @Deprecated
    public static boolean recordEvent(String str, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        return AnalyticsControllerImpl.getInstance().recordEvent(str, map);
    }

    public static boolean recordCustomEvent(String str, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        return AnalyticsControllerImpl.getInstance().recordCustomEvent(str, map);
    }

    public static boolean recordCustomEvent(String str, String str2, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        if (str2 != null && !str2.isEmpty()) {
            map.put("name", str2);
        }
        return recordCustomEvent(str, map);
    }

    public static boolean recordBreadcrumb(String str) {
        return recordBreadcrumb(str, null);
    }

    public static boolean recordBreadcrumb(String str, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        if (str != null && !str.isEmpty()) {
            map.put("name", str);
        }
        return AnalyticsControllerImpl.getInstance().recordBreadcrumb(str, map);
    }

    public static boolean recordHandledException(Exception exc) {
        return recordHandledException(exc, new HashMap());
    }

    public static boolean recordHandledException(Exception exc, Map<String, Object> map) {
        if (map == null) {
            map = new HashMap<>();
        }
        return AgentDataController.sendAgentData(exc, map);
    }

    public static void setMaxEventPoolSize(int i) {
        AnalyticsControllerImpl.getInstance().setMaxEventPoolSize(i);
    }

    public static void setMaxEventBufferTime(int i) {
        AnalyticsControllerImpl.getInstance().setMaxEventBufferTime(i);
    }

    public static String currentSessionId() {
        return agentConfiguration.getSessionID();
    }
}