Search in sources :

Example 1 with ApplicationProcessState

use of com.google.firebase.perf.v1.ApplicationProcessState in project firebase-android-sdk by firebase.

the class GaugeManager method startCollectingGauges.

/**
 * Starts the collection of available gauges for the given {@code sessionId} and {@code
 * applicationProcessState}. The collected Gauge Metrics will be flushed at regular intervals.
 *
 * <p>GaugeManager can only collect gauges for one session at a time, and if this method is called
 * again with the same or new sessionId while it's already collecting gauges, all future gauges
 * will then be associated with the same or new sessionId and applicationProcessState.
 *
 * @param session The {@link PerfSession} to which the collected gauges will be associated with.
 * @param applicationProcessState The {@link ApplicationProcessState} the collected GaugeMetrics
 *     will be associated with.
 * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link
 *     this.stopCollectingGauges()} should always be called from the same thread.
 */
public void startCollectingGauges(PerfSession session, ApplicationProcessState applicationProcessState) {
    if (this.sessionId != null) {
        stopCollectingGauges();
    }
    long collectionFrequency = startCollectingGauges(applicationProcessState, session.getTimer());
    if (collectionFrequency == INVALID_GAUGE_COLLECTION_FREQUENCY) {
        logger.warn("Invalid gauge collection frequency. Unable to start collecting Gauges.");
        return;
    }
    this.sessionId = session.sessionId();
    this.applicationProcessState = applicationProcessState;
    // This is needed, otherwise the Runnable might use a stale value.
    final String sessionIdForScheduledTask = sessionId;
    final ApplicationProcessState applicationProcessStateForScheduledTask = applicationProcessState;
    try {
        gaugeManagerDataCollectionJob = gaugeManagerExecutor.get().scheduleAtFixedRate(() -> {
            syncFlush(sessionIdForScheduledTask, applicationProcessStateForScheduledTask);
        }, /*initialDelay=*/
        collectionFrequency * APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC, /*period=*/
        collectionFrequency * APPROX_NUMBER_OF_DATA_POINTS_PER_GAUGE_METRIC, TimeUnit.MILLISECONDS);
    } catch (RejectedExecutionException e) {
        logger.warn("Unable to start collecting Gauges: " + e.getMessage());
    }
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 2 with ApplicationProcessState

use of com.google.firebase.perf.v1.ApplicationProcessState in project firebase-android-sdk by firebase.

the class GaugeManager method stopCollectingGauges.

/**
 * Stops the collection of gauges if it was currently collecting. Does nothing otherwise.
 *
 * @note: This method is NOT thread safe - {@link this.startCollectingGauges()} and {@link
 *     this.stopCollectingGauges()} should always be called from the same thread.
 */
public void stopCollectingGauges() {
    if (this.sessionId == null) {
        return;
    }
    // This is needed, otherwise the Runnable might use a stale value.
    final String sessionIdForScheduledTask = sessionId;
    final ApplicationProcessState applicationProcessStateForScheduledTask = applicationProcessState;
    cpuGaugeCollector.get().stopCollecting();
    memoryGaugeCollector.get().stopCollecting();
    if (gaugeManagerDataCollectionJob != null) {
        gaugeManagerDataCollectionJob.cancel(false);
    }
    // Flush any data that was collected for this session one last time.
    @SuppressWarnings("FutureReturnValueIgnored") ScheduledFuture unusedFuture = gaugeManagerExecutor.get().schedule(() -> {
        syncFlush(sessionIdForScheduledTask, applicationProcessStateForScheduledTask);
    }, TIME_TO_WAIT_BEFORE_FLUSHING_GAUGES_QUEUE_MS, TimeUnit.MILLISECONDS);
    this.sessionId = null;
    this.applicationProcessState = ApplicationProcessState.APPLICATION_PROCESS_STATE_UNKNOWN;
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 3 with ApplicationProcessState

use of com.google.firebase.perf.v1.ApplicationProcessState in project firebase-android-sdk by firebase.

the class GaugeManager method logGaugeMetadata.

/**
 * Log the Gauge Metadata information to the transport.
 *
 * @param sessionId The {@link PerfSession#sessionId()} to which the collected Gauge Metrics
 *     should be associated with.
 * @param appState The {@link ApplicationProcessState} for which these gauges are collected.
 * @return true if GaugeMetadata was logged, false otherwise.
 */
public boolean logGaugeMetadata(String sessionId, ApplicationProcessState appState) {
    if (gaugeMetadataManager != null) {
        GaugeMetric gaugeMetric = GaugeMetric.newBuilder().setSessionId(sessionId).setGaugeMetadata(getGaugeMetadata()).build();
        transportManager.log(gaugeMetric, appState);
        return true;
    }
    return false;
}
Also used : GaugeMetric(com.google.firebase.perf.v1.GaugeMetric)

Example 4 with ApplicationProcessState

use of com.google.firebase.perf.v1.ApplicationProcessState in project firebase-android-sdk by firebase.

the class TransportManager method syncLog.

// endregion
// region Transport Private APIs
@WorkerThread
private void syncLog(PerfMetric.Builder perfMetricBuilder, ApplicationProcessState appState) {
    if (!isInitialized()) {
        if (isAllowedToCache(perfMetricBuilder)) {
            logger.debug("Transport is not initialized yet, %s will be queued for to be dispatched later", getLogcatMsg(perfMetricBuilder));
            pendingEventsQueue.add(new PendingPerfEvent(perfMetricBuilder, appState));
        }
        return;
    }
    PerfMetric perfMetric = setApplicationInfoAndBuild(perfMetricBuilder, appState);
    if (isAllowedToDispatch(perfMetric)) {
        dispatchLog(perfMetric);
        // TODO(b/172008005): This might not be the best place for this call, consider utilizing a
        // callback in the SessionManager itself.
        SessionManager.getInstance().updatePerfSessionIfExpired();
    }
}
Also used : PerfMetric(com.google.firebase.perf.v1.PerfMetric) WorkerThread(androidx.annotation.WorkerThread)

Example 5 with ApplicationProcessState

use of com.google.firebase.perf.v1.ApplicationProcessState in project firebase-android-sdk by firebase.

the class TransportManagerTest method validGaugeMetric_withCpuReadings_isLogged.

// endregion
// region Gauge Specific
@Test
public void validGaugeMetric_withCpuReadings_isLogged() {
    ApplicationProcessState expectedAppState = ApplicationProcessState.FOREGROUND;
    // Construct a list of Cpu metric readings
    List<CpuMetricReading> expectedCpuMetricReadings = new ArrayList<>();
    expectedCpuMetricReadings.add(createValidCpuMetricReading(/* userTimeUs= */
    10, /* systemTimeUs= */
    20));
    expectedCpuMetricReadings.add(createValidCpuMetricReading(/* userTimeUs= */
    20, /* systemTimeUs= */
    30));
    GaugeMetric validGauge = GaugeMetric.newBuilder().setSessionId("sessionId").addAllCpuMetricReadings(expectedCpuMetricReadings).build();
    testTransportManager.log(validGauge, expectedAppState);
    fakeExecutorService.runAll();
    PerfMetric loggedPerfMetric = getLastLoggedEvent(times(1));
    assertThat(loggedPerfMetric.getGaugeMetric().getCpuMetricReadingsList()).containsExactlyElementsIn(expectedCpuMetricReadings);
    assertThat(loggedPerfMetric.getGaugeMetric().getSessionId()).isEqualTo("sessionId");
}
Also used : ApplicationProcessState(com.google.firebase.perf.v1.ApplicationProcessState) CpuMetricReading(com.google.firebase.perf.v1.CpuMetricReading) PerfMetric(com.google.firebase.perf.v1.PerfMetric) ArrayList(java.util.ArrayList) GaugeMetric(com.google.firebase.perf.v1.GaugeMetric) Test(org.junit.Test)

Aggregations

ApplicationProcessState (com.google.firebase.perf.v1.ApplicationProcessState)5 PerfMetric (com.google.firebase.perf.v1.PerfMetric)5 GaugeMetric (com.google.firebase.perf.v1.GaugeMetric)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)2 WorkerThread (androidx.annotation.WorkerThread)1 AndroidMemoryReading (com.google.firebase.perf.v1.AndroidMemoryReading)1 CpuMetricReading (com.google.firebase.perf.v1.CpuMetricReading)1 GaugeMetadata (com.google.firebase.perf.v1.GaugeMetadata)1 TraceMetric (com.google.firebase.perf.v1.TraceMetric)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1