1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ondevicepersonalization.services.util; 18 19 import android.adservices.ondevicepersonalization.CalleeMetadata; 20 import android.adservices.ondevicepersonalization.Constants; 21 import android.os.Bundle; 22 23 import com.android.odp.module.common.Clock; 24 import com.android.ondevicepersonalization.services.statsd.ApiCallStats; 25 import com.android.ondevicepersonalization.services.statsd.OdpStatsdLogger; 26 27 /** Utilities for stats logging */ 28 public class StatsUtils { 29 /** Subtracts callee reported latency from caller reported latency. */ getOverheadLatencyMillis(long callerLatencyMillis, Bundle result)30 public static long getOverheadLatencyMillis(long callerLatencyMillis, Bundle result) { 31 long calleeLatencyMillis = callerLatencyMillis; 32 if (result != null) { 33 CalleeMetadata metadata = 34 result.getParcelable(Constants.EXTRA_CALLEE_METADATA, CalleeMetadata.class); 35 if (metadata != null) { 36 if (metadata.getElapsedTimeMillis() > 0 37 && metadata.getElapsedTimeMillis() < callerLatencyMillis) { 38 calleeLatencyMillis = metadata.getElapsedTimeMillis(); 39 } 40 } 41 } 42 return callerLatencyMillis - calleeLatencyMillis; 43 } 44 45 /** Writes app request usage to statsd. */ writeAppRequestMetrics( int apiName, Clock clock, int responseCode, long startTimeMillis)46 public static void writeAppRequestMetrics( 47 int apiName, Clock clock, int responseCode, long startTimeMillis) { 48 int latencyMillis = (int) (clock.elapsedRealtime() - startTimeMillis); 49 ApiCallStats callStats = new ApiCallStats.Builder(apiName) 50 .setLatencyMillis(latencyMillis) 51 .setResponseCode(responseCode) 52 .build(); 53 OdpStatsdLogger.getInstance().logApiCallStats(callStats); 54 } 55 56 /** Writes service request usage to statsd. */ writeServiceRequestMetrics( int apiName, Bundle result, Clock clock, int responseCode, long startTimeMillis)57 public static void writeServiceRequestMetrics( 58 int apiName, Bundle result, Clock clock, int responseCode, long startTimeMillis) { 59 int latencyMillis = (int) (clock.elapsedRealtime() - startTimeMillis); 60 int overheadLatencyMillis = 61 (int) StatsUtils.getOverheadLatencyMillis(latencyMillis, result); 62 ApiCallStats callStats = new ApiCallStats.Builder(apiName) 63 .setLatencyMillis(latencyMillis) 64 .setOverheadLatencyMillis(overheadLatencyMillis) 65 .setResponseCode(responseCode) 66 .build(); 67 OdpStatsdLogger.getInstance().logApiCallStats(callStats); 68 } StatsUtils()69 private StatsUtils() {} 70 } 71