summaryrefslogtreecommitdiff
path: root/main/java/com/google/android/setupcompat/logging/SetupMetricsLogger.java
blob: 2869c6585f803ae8a47cbd14ed8419bd9911384a (plain)
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
/*
 * 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 android.os.Bundle;
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.SetupMetricsLoggingConstants.MetricBundleKeys;
import com.google.android.setupcompat.logging.internal.SetupMetricsLoggingConstants.MetricType;
import java.util.concurrent.TimeUnit;

/** SetupMetricsLogger provides an easy way to log custom metrics to SetupWizard. */
public class 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.");
    Bundle bundle = new Bundle();
    bundle.putParcelable(MetricBundleKeys.CUSTOM_EVENT, customEvent);
    SetupCompatServiceInvoker.get(context).logMetricEvent(MetricType.CUSTOM_EVENT, bundle);
  }

  /** 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.");
    Bundle bundle = new Bundle();
    bundle.putParcelable(MetricBundleKeys.METRIC_KEY, counterName);
    bundle.putInt(MetricBundleKeys.COUNTER_INT, times);
    SetupCompatServiceInvoker.get(context).logMetricEvent(MetricType.COUNTER_EVENT, bundle);
  }

  /**
   * 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.");
    Bundle bundle = new Bundle();
    bundle.putParcelable(MetricBundleKeys.METRIC_KEY, timerName);
    bundle.putLong(MetricBundleKeys.TIME_MILLIS_LONG, timeInMillis);
    SetupCompatServiceInvoker.get(context).logMetricEvent(MetricType.DURATION_EVENT, bundle);
  }
}