summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/RuntimeTelemetry.java
blob: 1fad0cb17e52bbfbd6e2fdc2b8fe568accb121c1 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * vim: ts=4 sw=4 expandtab:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.geckoview;

import androidx.annotation.AnyThread;
import androidx.annotation.NonNull;
import org.mozilla.gecko.GeckoThread;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.mozglue.JNIObject;

/** The telemetry API gives access to telemetry data of the Gecko runtime. */
public final class RuntimeTelemetry {
  protected RuntimeTelemetry() {}

  /**
   * The runtime telemetry metric object.
   *
   * @param <T> type of the underlying metric sample
   */
  public static class Metric<T> {
    /** The runtime metric name. */
    public final @NonNull String name;

    /** The metric values. */
    public final @NonNull T value;

    /* package */ Metric(final String name, final T value) {
      this.name = name;
      this.value = value;
    }

    @Override
    public String toString() {
      return "name: " + name + ", value: " + value;
    }

    // For testing
    protected Metric() {
      name = null;
      value = null;
    }
  }

  /** The Histogram telemetry metric object. */
  public static class Histogram extends Metric<long[]> {
    /** Whether or not this is a Categorical Histogram. */
    public final boolean isCategorical;

    /* package */ Histogram(final boolean isCategorical, final String name, final long[] value) {
      super(name, value);
      this.isCategorical = isCategorical;
    }

    // For testing
    protected Histogram() {
      super(null, null);
      isCategorical = false;
    }
  }

  /**
   * The runtime telemetry delegate. Implement this if you want to receive runtime (Gecko) telemetry
   * and attach it via {@link GeckoRuntimeSettings.Builder#telemetryDelegate}.
   */
  public interface Delegate {
    /**
     * A runtime telemetry histogram metric has been received.
     *
     * @param metric The runtime metric details.
     */
    @AnyThread
    default void onHistogram(final @NonNull Histogram metric) {}

    /**
     * A runtime telemetry boolean scalar has been received.
     *
     * @param metric The runtime metric details.
     */
    @AnyThread
    default void onBooleanScalar(final @NonNull Metric<Boolean> metric) {}

    /**
     * A runtime telemetry long scalar has been received.
     *
     * @param metric The runtime metric details.
     */
    @AnyThread
    default void onLongScalar(final @NonNull Metric<Long> metric) {}

    /**
     * A runtime telemetry string scalar has been received.
     *
     * @param metric The runtime metric details.
     */
    @AnyThread
    default void onStringScalar(final @NonNull Metric<String> metric) {}
  }

  // The proxy connects to telemetry core and forwards telemetry events
  // to the attached delegate.
  /* package */ static final class Proxy extends JNIObject {
    private final Delegate mDelegate;

    public Proxy(final @NonNull Delegate delegate) {
      mDelegate = delegate;
    }

    // Attach to current runtime.
    // We might have different mechanics of attaching to specific runtimes
    // in future, for which case we should split the delegate assignment in
    // the setup phase from the attaching.
    public void attach() {
      if (GeckoThread.isRunning()) {
        registerDelegateProxy(this);
      } else {
        GeckoThread.queueNativeCall(Proxy.class, "registerDelegateProxy", Proxy.class, this);
      }
    }

    public @NonNull Delegate getDelegate() {
      return mDelegate;
    }

    @WrapForJNI(dispatchTo = "gecko")
    private static native void registerDelegateProxy(Proxy proxy);

    @WrapForJNI(calledFrom = "gecko")
    /* package */ void dispatchHistogram(
        final boolean isCategorical, final String name, final long[] values) {
      if (mDelegate == null) {
        // TODO throw?
        return;
      }
      mDelegate.onHistogram(new Histogram(isCategorical, name, values));
    }

    @WrapForJNI(calledFrom = "gecko")
    /* package */ void dispatchStringScalar(final String name, final String value) {
      if (mDelegate == null) {
        return;
      }
      mDelegate.onStringScalar(new Metric<>(name, value));
    }

    @WrapForJNI(calledFrom = "gecko")
    /* package */ void dispatchBooleanScalar(final String name, final boolean value) {
      if (mDelegate == null) {
        return;
      }
      mDelegate.onBooleanScalar(new Metric<>(name, value));
    }

    @WrapForJNI(calledFrom = "gecko")
    /* package */ void dispatchLongScalar(final String name, final long value) {
      if (mDelegate == null) {
        return;
      }
      mDelegate.onLongScalar(new Metric<>(name, value));
    }

    @Override // JNIObject
    protected void disposeNative() {
      // We don't hold native references.
      throw new UnsupportedOperationException();
    }
  }
}