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.modules.expresslog;
18 
19 import android.annotation.NonNull;
20 
21 /** Counter encapsulates StatsD write API calls */
22 public final class Counter {
23 
24     // Not instantiable.
Counter()25     private Counter() {}
26 
27     /**
28      * Increments Telemetry Express Counter metric by 1
29      * @param metricId to log, no-op if metricId is not defined in the TeX catalog
30      */
logIncrement(@onNull String metricId)31     public static void logIncrement(@NonNull String metricId) {
32         logIncrement(metricId, 1);
33     }
34 
35     /**
36      * Increments Telemetry Express Counter metric by 1
37      * @param metricId to log, no-op if metricId is not defined in the TeX catalog
38      * @param uid used as a dimension for the count metric
39      */
logIncrementWithUid(@onNull String metricId, int uid)40     public static void logIncrementWithUid(@NonNull String metricId, int uid) {
41         logIncrementWithUid(metricId, uid, 1);
42     }
43 
44     /**
45      * Increments Telemetry Express Counter metric by arbitrary value
46      * @param metricId to log, no-op if metricId is not defined in the TeX catalog
47      * @param amount to increment counter
48      */
logIncrement(@onNull String metricId, long amount)49     public static void logIncrement(@NonNull String metricId, long amount) {
50         final long metricIdHash =
51                 MetricIds.getMetricIdHash(metricId, MetricIds.METRIC_TYPE_COUNTER);
52         StatsExpressLog.write(StatsExpressLog.EXPRESS_EVENT_REPORTED, metricIdHash, amount);
53     }
54 
55     /**
56      * Increments Telemetry Express Counter metric by arbitrary value
57      * @param metricId to log, no-op if metricId is not defined in the TeX catalog
58      * @param uid used as a dimension for the count metric
59      * @param amount to increment counter
60      */
logIncrementWithUid(@onNull String metricId, int uid, long amount)61     public static void logIncrementWithUid(@NonNull String metricId, int uid, long amount) {
62         final long metricIdHash =
63                 MetricIds.getMetricIdHash(metricId, MetricIds.METRIC_TYPE_COUNTER_WITH_UID);
64         StatsExpressLog.write(
65             StatsExpressLog.EXPRESS_UID_EVENT_REPORTED, metricIdHash, amount, uid);
66     }
67 }
68