package com.google.firebase.perf.metrics; import android.os.Parcel; import android.os.Parcelable; import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.google.firebase.perf.application.AppStateMonitor; import com.google.firebase.perf.application.AppStateUpdateHandler; import com.google.firebase.perf.config.ConfigResolver; import com.google.firebase.perf.logging.AndroidLogger; import com.google.firebase.perf.metrics.validator.PerfMetricValidator; import com.google.firebase.perf.session.PerfSession; import com.google.firebase.perf.session.SessionAwareObject; import com.google.firebase.perf.session.SessionManager; import com.google.firebase.perf.session.gauges.GaugeManager; import com.google.firebase.perf.transport.TransportManager; import com.google.firebase.perf.util.Clock; import com.google.firebase.perf.util.Timer; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /* loaded from: classes3.dex */ public class Trace extends AppStateUpdateHandler implements Parcelable, SessionAwareObject { public final Clock clock; public final Map counterNameToCounterMap; public final Map customAttributesMap; public Timer endTime; public final GaugeManager gaugeManager; public final String name; public final Trace parent; public final WeakReference sessionAwareObject; public final List sessions; public Timer startTime; public final List subtraces; public final TransportManager transportManager; public static final AndroidLogger logger = AndroidLogger.getInstance(); public static final Map traceNameToTraceMap = new ConcurrentHashMap(); @Keep public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { // from class: com.google.firebase.perf.metrics.Trace.1 @Override // android.os.Parcelable.Creator public Trace createFromParcel(Parcel parcel) { return new Trace(parcel, false); } @Override // android.os.Parcelable.Creator public Trace[] newArray(int i) { return new Trace[i]; } }; public static final Parcelable.Creator CREATOR_DATAONLY = new Parcelable.Creator() { // from class: com.google.firebase.perf.metrics.Trace.2 @Override // android.os.Parcelable.Creator public Trace createFromParcel(Parcel parcel) { return new Trace(parcel, true); } @Override // android.os.Parcelable.Creator public Trace[] newArray(int i) { return new Trace[i]; } }; @Override // android.os.Parcelable @Keep public int describeContents() { return 0; } public Map getCounters() { return this.counterNameToCounterMap; } public Timer getEndTime() { return this.endTime; } public String getName() { return this.name; } public Timer getStartTime() { return this.startTime; } public List getSubtraces() { return this.subtraces; } public boolean hasStarted() { return this.startTime != null; } public boolean isStopped() { return this.endTime != null; } @Override // com.google.firebase.perf.session.SessionAwareObject public void updateSession(PerfSession perfSession) { if (perfSession == null) { logger.warn("Unable to add new SessionId to the Trace. Continuing without it."); } else { if (!hasStarted() || isStopped()) { return; } this.sessions.add(perfSession); } } public Trace(String str, TransportManager transportManager, Clock clock, AppStateMonitor appStateMonitor) { this(str, transportManager, clock, appStateMonitor, GaugeManager.getInstance()); } public Trace(String str, TransportManager transportManager, Clock clock, AppStateMonitor appStateMonitor, GaugeManager gaugeManager) { super(appStateMonitor); this.sessionAwareObject = new WeakReference(this); this.parent = null; this.name = str.trim(); this.subtraces = new ArrayList(); this.counterNameToCounterMap = new ConcurrentHashMap(); this.customAttributesMap = new ConcurrentHashMap(); this.clock = clock; this.transportManager = transportManager; this.sessions = Collections.synchronizedList(new ArrayList()); this.gaugeManager = gaugeManager; } public Trace(Parcel parcel, boolean z) { super(z ? null : AppStateMonitor.getInstance()); this.sessionAwareObject = new WeakReference(this); this.parent = (Trace) parcel.readParcelable(Trace.class.getClassLoader()); this.name = parcel.readString(); ArrayList arrayList = new ArrayList(); this.subtraces = arrayList; parcel.readList(arrayList, Trace.class.getClassLoader()); ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap(); this.counterNameToCounterMap = concurrentHashMap; this.customAttributesMap = new ConcurrentHashMap(); parcel.readMap(concurrentHashMap, Counter.class.getClassLoader()); this.startTime = (Timer) parcel.readParcelable(Timer.class.getClassLoader()); this.endTime = (Timer) parcel.readParcelable(Timer.class.getClassLoader()); List synchronizedList = Collections.synchronizedList(new ArrayList()); this.sessions = synchronizedList; parcel.readList(synchronizedList, PerfSession.class.getClassLoader()); if (z) { this.transportManager = null; this.clock = null; this.gaugeManager = null; } else { this.transportManager = TransportManager.getInstance(); this.clock = new Clock(); this.gaugeManager = GaugeManager.getInstance(); } } @Keep public void start() { if (!ConfigResolver.getInstance().isPerformanceMonitoringEnabled()) { logger.debug("Trace feature is disabled."); return; } String validateTraceName = PerfMetricValidator.validateTraceName(this.name); if (validateTraceName != null) { logger.error("Cannot start trace '%s'. Trace name is invalid.(%s)", this.name, validateTraceName); return; } if (this.startTime != null) { logger.error("Trace '%s' has already started, should not start again!", this.name); return; } this.startTime = this.clock.getTime(); registerForAppState(); PerfSession perfSession = SessionManager.getInstance().perfSession(); SessionManager.getInstance().registerForSessionUpdates(this.sessionAwareObject); updateSession(perfSession); if (perfSession.isGaugeAndEventCollectionEnabled()) { this.gaugeManager.collectGaugeMetricOnce(perfSession.getTimer()); } } @Keep public void stop() { if (!hasStarted()) { logger.error("Trace '%s' has not been started so unable to stop!", this.name); return; } if (isStopped()) { logger.error("Trace '%s' has already stopped, should not stop again!", this.name); return; } SessionManager.getInstance().unregisterForSessionUpdates(this.sessionAwareObject); unregisterForAppState(); Timer time = this.clock.getTime(); this.endTime = time; if (this.parent == null) { setEndTimeOfLastStage(time); if (!this.name.isEmpty()) { this.transportManager.log(new TraceMetricBuilder(this).build(), getAppState()); if (SessionManager.getInstance().perfSession().isGaugeAndEventCollectionEnabled()) { this.gaugeManager.collectGaugeMetricOnce(SessionManager.getInstance().perfSession().getTimer()); return; } return; } logger.error("Trace name is empty, no log is sent to server"); } } public final void setEndTimeOfLastStage(Timer timer) { if (this.subtraces.isEmpty()) { return; } Trace trace = (Trace) this.subtraces.get(this.subtraces.size() - 1); if (trace.endTime == null) { trace.endTime = timer; } } public final Counter obtainOrCreateCounterByName(String str) { Counter counter = (Counter) this.counterNameToCounterMap.get(str); if (counter != null) { return counter; } Counter counter2 = new Counter(str); this.counterNameToCounterMap.put(str, counter2); return counter2; } @Keep public void incrementMetric(@NonNull String str, long j) { String validateMetricName = PerfMetricValidator.validateMetricName(str); if (validateMetricName != null) { logger.error("Cannot increment metric '%s'. Metric name is invalid.(%s)", str, validateMetricName); return; } if (!hasStarted()) { logger.warn("Cannot increment metric '%s' for trace '%s' because it's not started", str, this.name); } else { if (isStopped()) { logger.warn("Cannot increment metric '%s' for trace '%s' because it's been stopped", str, this.name); return; } Counter obtainOrCreateCounterByName = obtainOrCreateCounterByName(str.trim()); obtainOrCreateCounterByName.increment(j); logger.debug("Incrementing metric '%s' to %d on trace '%s'", str, Long.valueOf(obtainOrCreateCounterByName.getCount()), this.name); } } @Keep public long getLongMetric(@NonNull String str) { Counter counter = str != null ? (Counter) this.counterNameToCounterMap.get(str.trim()) : null; if (counter == null) { return 0L; } return counter.getCount(); } @Keep public void putMetric(@NonNull String str, long j) { String validateMetricName = PerfMetricValidator.validateMetricName(str); if (validateMetricName != null) { logger.error("Cannot set value for metric '%s'. Metric name is invalid.(%s)", str, validateMetricName); return; } if (!hasStarted()) { logger.warn("Cannot set value for metric '%s' for trace '%s' because it's not started", str, this.name); } else if (isStopped()) { logger.warn("Cannot set value for metric '%s' for trace '%s' because it's been stopped", str, this.name); } else { obtainOrCreateCounterByName(str.trim()).setCount(j); logger.debug("Setting metric '%s' to '%s' on trace '%s'", str, Long.valueOf(j), this.name); } } public void finalize() { try { if (isActive()) { logger.warn("Trace '%s' is started but not stopped when it is destructed!", this.name); incrementTsnsCount(1); } } finally { super.finalize(); } } public boolean isActive() { return hasStarted() && !isStopped(); } @Override // android.os.Parcelable @Keep public void writeToParcel(@NonNull Parcel parcel, int i) { parcel.writeParcelable(this.parent, 0); parcel.writeString(this.name); parcel.writeList(this.subtraces); parcel.writeMap(this.counterNameToCounterMap); parcel.writeParcelable(this.startTime, 0); parcel.writeParcelable(this.endTime, 0); synchronized (this.sessions) { parcel.writeList(this.sessions); } } @Keep public void putAttribute(@NonNull String str, @NonNull String str2) { boolean z = false; try { str = str.trim(); str2 = str2.trim(); checkAttribute(str, str2); logger.debug("Setting attribute '%s' to '%s' on trace '%s'", str, str2, this.name); z = true; } catch (Exception e) { logger.error("Can not set attribute '%s' with value '%s' (%s)", str, str2, e.getMessage()); } if (z) { this.customAttributesMap.put(str, str2); } } public final void checkAttribute(String str, String str2) { if (isStopped()) { throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Trace '%s' has been stopped", this.name)); } if (!this.customAttributesMap.containsKey(str) && this.customAttributesMap.size() >= 5) { throw new IllegalArgumentException(String.format(Locale.ENGLISH, "Exceeds max limit of number of attributes - %d", 5)); } PerfMetricValidator.validateAttribute(str, str2); } @Keep public void removeAttribute(@NonNull String str) { if (isStopped()) { logger.error("Can't remove a attribute from a Trace that's stopped."); } else { this.customAttributesMap.remove(str); } } @Nullable @Keep public String getAttribute(@NonNull String str) { return (String) this.customAttributesMap.get(str); } @NonNull @Keep public Map getAttributes() { return new HashMap(this.customAttributesMap); } public List getSessions() { List unmodifiableList; synchronized (this.sessions) { try { ArrayList arrayList = new ArrayList(); for (PerfSession perfSession : this.sessions) { if (perfSession != null) { arrayList.add(perfSession); } } unmodifiableList = Collections.unmodifiableList(arrayList); } catch (Throwable th) { throw th; } } return unmodifiableList; } }