1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.setupcompat.logging;
import android.content.Context;
import androidx.annotation.NonNull;
import com.google.android.setupcompat.internal.Preconditions;
import com.google.android.setupcompat.internal.SetupCompatServiceInvoker;
import com.google.android.setupcompat.logging.internal.MetricBundleConverter;
import com.google.android.setupcompat.logging.internal.SetupMetricsLoggingConstants.MetricType;
import com.google.android.setupcompat.util.Logger;
import java.util.concurrent.TimeUnit;
/** SetupMetricsLogger provides an easy way to log custom metrics to SetupWizard. */
public class SetupMetricsLogger {
private static final Logger LOG = new Logger("SetupMetricsLogger");
/** Logs an instance of {@link CustomEvent} to SetupWizard. */
public static void logCustomEvent(@NonNull Context context, @NonNull CustomEvent customEvent) {
Preconditions.checkNotNull(context, "Context cannot be null.");
Preconditions.checkNotNull(customEvent, "CustomEvent cannot be null.");
SetupCompatServiceInvoker.get(context)
.logMetricEvent(
MetricType.CUSTOM_EVENT, MetricBundleConverter.createBundleForLogging(customEvent));
}
/** Increments the counter value with the name {@code counterName} by {@code times}. */
public static void logCounter(
@NonNull Context context, @NonNull MetricKey counterName, int times) {
Preconditions.checkNotNull(context, "Context cannot be null.");
Preconditions.checkNotNull(counterName, "CounterName cannot be null.");
Preconditions.checkArgument(times > 0, "Counter cannot be negative.");
SetupCompatServiceInvoker.get(context)
.logMetricEvent(
MetricType.COUNTER_EVENT,
MetricBundleConverter.createBundleForLoggingCounter(counterName, times));
}
/**
* Logs the {@link Timer}'s duration by calling {@link #logDuration(Context, MetricKey, long)}.
*/
public static void logDuration(@NonNull Context context, @NonNull Timer timer) {
Preconditions.checkNotNull(context, "Context cannot be null.");
Preconditions.checkNotNull(timer, "Timer cannot be null.");
Preconditions.checkArgument(
timer.isStopped(), "Timer should be stopped before calling logDuration.");
logDuration(
context, timer.getMetricKey(), TimeUnit.NANOSECONDS.toMillis(timer.getDurationInNanos()));
}
/** Logs a duration event to SetupWizard. */
public static void logDuration(
@NonNull Context context, @NonNull MetricKey timerName, long timeInMillis) {
Preconditions.checkNotNull(context, "Context cannot be null.");
Preconditions.checkNotNull(timerName, "Timer name cannot be null.");
Preconditions.checkArgument(timeInMillis >= 0, "Duration cannot be negative.");
SetupCompatServiceInvoker.get(context)
.logMetricEvent(
MetricType.DURATION_EVENT,
MetricBundleConverter.createBundleForLoggingTimer(timerName, timeInMillis));
}
/**
* Logs setup collection metrics (go/suw-metrics-collection-api)
*/
public static void logMetrics(
@NonNull Context context, @NonNull ScreenKey screenKey, @NonNull SetupMetric... metrics) {
Preconditions.checkNotNull(context, "Context cannot be null.");
Preconditions.checkNotNull(screenKey, "ScreenKey cannot be null.");
Preconditions.checkNotNull(metrics, "SetupMetric cannot be null.");
for (SetupMetric metric : metrics) {
LOG.atDebug("Log metric: " + screenKey + ", " + metric);
SetupCompatServiceInvoker.get(context).logMetricEvent(
MetricType.SETUP_COLLECTION_EVENT,
MetricBundleConverter.createBundleForLoggingSetupMetric(screenKey, metric));
}
}
}
|