Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99e42a3e authored by Arpan Kaphle's avatar Arpan Kaphle
Browse files

Candidate Metric Utilized in Framework

This adds the candidate metric in the framework at various places and
goes up to emits, with TODOs left for the actual emits once the atoms
build as expected.

Bug: 270403549
Test: Builds for now
Change-Id: I1196dbf0a5d6d5c6573d5f8dbde15dc3bc916f86
parent 25126020
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ public final class CreateRequestSession extends RequestSession<CreateCredentialR

    private void respondToClientWithErrorAndFinish(String errorType, String errorMsg) {
        Log.i(TAG, "respondToClientWithErrorAndFinish");

        // TODO add exception bit
        if (mRequestSessionStatus == RequestSessionStatus.COMPLETE) {
            Log.i(TAG, "Request has already been completed. This is strange.");
            return;
+7 −3
Original line number Diff line number Diff line
@@ -580,9 +580,13 @@ public final class CredentialManagerService
        }

        private void finalizeAndEmitInitialPhaseMetric(RequestSession session) {
            try {
                var initMetric = session.mInitialPhaseMetric;
                initMetric.setCredentialServiceBeginQueryTimeNanoseconds(System.nanoTime());
                MetricUtilities.logApiCalled(initMetric);
            } catch (Exception e) {
                Log.w(TAG, "Unexpected error during metric logging: " + e);
            }
        }

        @Override
+30 −0
Original line number Diff line number Diff line
@@ -80,6 +80,34 @@ public class MetricUtilities {
        return (int) ((t2 - t1) / 1000);
    }

    /**
     * A logging utility used primarily for the candidate phase of the current metric setup.
     *
     * @param providers            a map with known providers
     * @param emitSequenceId       an emitted sequence id for the current session
     */
    protected static void logApiCalled(Map<String, ProviderSession> providers,
            int emitSequenceId) {
        try {
            var providerSessions = providers.values();
            int providerSize = providerSessions.size();
            int[] candidateUidList = new int[providerSize];
            int[] candidateQueryRoundTripTimeList = new int[providerSize];
            int[] candidateStatusList = new int[providerSize];
            int index = 0;
            for (var session : providerSessions) {
                CandidatePhaseMetric metric = session.mCandidatePhasePerProviderMetric;
                candidateUidList[index] = metric.getCandidateUid();
                candidateQueryRoundTripTimeList[index] = metric.getQueryLatencyMicroseconds();
                candidateStatusList[index] = metric.getProviderQueryStatus();
                index++;
            }
            // TODO Handle the emit here
        } catch (Exception e) {
            Log.w(TAG, "Unexpected error during metric logging: " + e);
        }
    }

    /**
     * The most common logging helper, handles the overall status of the API request with the
     * provider status and latencies. Other versions of this method may be more useful depending
@@ -90,6 +118,7 @@ public class MetricUtilities {
     * @param providers            a map with known providers
     * @param callingUid           the calling UID of the client app
     * @param chosenProviderFinalPhaseMetric the metric data type of the final chosen provider
     * TODO remove soon
     */
    protected static void logApiCalled(ApiName apiName, ApiStatus apiStatus,
            Map<String, ProviderSession> providers, int callingUid,
@@ -133,6 +162,7 @@ public class MetricUtilities {
     * contain default values for all other optional parameters.
     *
     * TODO(b/271135048) - given space requirements, this may be a good candidate for another atom
     * TODO immediately remove and carry over TODO to new log for this setup
     *
     * @param apiName    the api name to log
     * @param apiStatus  the status to log
+6 −11
Original line number Diff line number Diff line
@@ -42,7 +42,8 @@ public final class ProviderClearSession extends ProviderSession<ClearCredential
    private ClearCredentialStateException mProviderException;

    /** Creates a new provider session to be used by the request session. */
    @Nullable public static ProviderClearSession createNewSession(
    @Nullable
    public static ProviderClearSession createNewSession(
            Context context,
            @UserIdInt int userId,
            CredentialProviderInfo providerInfo,
@@ -90,6 +91,7 @@ public final class ProviderClearSession extends ProviderSession<ClearCredential
        if (exception instanceof ClearCredentialStateException) {
            mProviderException = (ClearCredentialStateException) exception;
        }
        captureCandidateFailure();
        updateStatusAndInvokeCallback(toStatus(errorCode));
    }

@@ -120,14 +122,7 @@ public final class ProviderClearSession extends ProviderSession<ClearCredential
    @Override
    protected void invokeSession() {
        if (mRemoteCredentialService != null) {
            /*
            InitialPhaseMetric initMetric = ((RequestSession)mCallbacks).initMetric;
            TODO immediately once the other change patched through
            mCandidateProviderMetric.setSessionId(initMetric
            .mInitialPhaseMetric.getSessionId());
            mCandidateProviderMetric.setStartTime(initMetric.getStartTime())
             */
            mCandidatePhasePerProviderMetric.setStartQueryTimeNanoseconds(System.nanoTime());
            startCandidateMetrics();
            mRemoteCredentialService.onClearCredentialState(mProviderRequest, this);
        }
    }
+31 −12
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import android.util.Log;
import android.util.Pair;
import android.util.Slog;

import com.android.server.credentials.metrics.EntryEnum;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -65,7 +67,8 @@ public final class ProviderCreateSession extends ProviderSession<
    private final ProviderResponseDataHandler mProviderResponseDataHandler;

    /** Creates a new provider session to be used by the request session. */
    @Nullable public static ProviderCreateSession createNewSession(
    @Nullable
    public static ProviderCreateSession createNewSession(
            Context context,
            @UserIdInt int userId,
            CredentialProviderInfo providerInfo,
@@ -155,6 +158,7 @@ public final class ProviderCreateSession extends ProviderSession<
            // Store query phase exception for aggregation with final response
            mProviderException = (CreateCredentialException) exception;
        }
        captureCandidateFailure();
        updateStatusAndInvokeCallback(toStatus(errorCode));
    }

@@ -175,14 +179,32 @@ public final class ProviderCreateSession extends ProviderSession<
        mProviderResponseDataHandler.addResponseContent(response.getCreateEntries(),
                response.getRemoteCreateEntry());
        if (mProviderResponseDataHandler.isEmptyResponse(response)) {
            gatheCandidateEntryMetrics(response);
            updateStatusAndInvokeCallback(Status.EMPTY_RESPONSE);
        } else {
            gatheCandidateEntryMetrics(response);
            updateStatusAndInvokeCallback(Status.SAVE_ENTRIES_RECEIVED);
        }
    }

    private void gatheCandidateEntryMetrics(BeginCreateCredentialResponse response) {
        try {
            var createEntries = response.getCreateEntries();
            int numCreateEntries = createEntries == null ? 0 : createEntries.size();
            // TODO confirm how to get types from slice
            if (numCreateEntries > 0) {
                createEntries.forEach(c ->
                        mCandidatePhasePerProviderMetric.addEntry(EntryEnum.CREDENTIAL_ENTRY));
            }
            mCandidatePhasePerProviderMetric.setNumEntriesTotal(numCreateEntries);
        } catch (Exception e) {
            Log.w(TAG, "Unexpected error during metric logging: " + e);
        }
    }

    @Override
    @Nullable protected CreateCredentialProviderData prepareUiData()
    @Nullable
    protected CreateCredentialProviderData prepareUiData()
            throws IllegalArgumentException {
        Log.i(TAG, "In prepareUiData");
        if (!ProviderSession.isUiInvokingStatus(getStatus())) {
@@ -226,14 +248,7 @@ public final class ProviderCreateSession extends ProviderSession<
    @Override
    protected void invokeSession() {
        if (mRemoteCredentialService != null) {
            /*
            InitialPhaseMetric initMetric = ((RequestSession)mCallbacks).initMetric;
            TODO immediately once the other change patched through
            mCandidateProviderMetric.setSessionId(initMetric
            .mInitialPhaseMetric.getSessionId());
            mCandidateProviderMetric.setStartTime(initMetric.getStartTime())
             */
            mCandidatePhasePerProviderMetric.setStartQueryTimeNanoseconds(System.nanoTime());
            startCandidateMetrics();
            mRemoteCredentialService.onCreateCredential(mProviderRequest, this);
        }
    }
@@ -305,12 +320,14 @@ public final class ProviderCreateSession extends ProviderSession<
    }

    private class ProviderResponseDataHandler {
        @Nullable private final ComponentName mExpectedRemoteEntryProviderService;
        @Nullable
        private final ComponentName mExpectedRemoteEntryProviderService;

        @NonNull
        private final Map<String, Pair<CreateEntry, Entry>> mUiCreateEntries = new HashMap<>();

        @Nullable private Pair<String, Pair<RemoteEntry, Entry>> mUiRemoteEntry = null;
        @Nullable
        private Pair<String, Pair<RemoteEntry, Entry>> mUiRemoteEntry = null;

        ProviderResponseDataHandler(@Nullable ComponentName expectedRemoteEntryProviderService) {
            mExpectedRemoteEntryProviderService = expectedRemoteEntryProviderService;
@@ -323,6 +340,7 @@ public final class ProviderCreateSession extends ProviderSession<
                setRemoteEntry(remoteEntry);
            }
        }

        public void addCreateEntry(CreateEntry createEntry) {
            String id = generateUniqueId();
            Entry entry = new Entry(SAVE_ENTRY_KEY,
@@ -373,6 +391,7 @@ public final class ProviderCreateSession extends ProviderSession<
        private boolean isEmptyResponse() {
            return mUiCreateEntries.isEmpty() && mUiRemoteEntry == null;
        }

        @Nullable
        public RemoteEntry getRemoteEntry(String entryKey) {
            return mUiRemoteEntry == null || mUiRemoteEntry
Loading