File: Labeled.h

package info (click to toggle)
firefox 147.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,532 kB
  • sloc: cpp: 7,607,356; javascript: 6,533,348; ansic: 3,775,236; python: 1,415,508; xml: 634,561; asm: 438,949; java: 186,241; sh: 62,760; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (347 lines) | stat: -rw-r--r-- 13,174 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef mozilla_glean_Labeled_h
#define mozilla_glean_Labeled_h

#include "nsISupports.h"
#include "nsWrapperCache.h"
#include "mozilla/ResultVariant.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/GleanBinding.h"
#include "mozilla/dom/Record.h"
#include "mozilla/glean/bindings/Boolean.h"
#include "mozilla/glean/bindings/Counter.h"
#include "mozilla/glean/bindings/CustomDistribution.h"
#include "mozilla/glean/bindings/DistributionData.h"
#include "mozilla/glean/bindings/GleanMetric.h"
#include "mozilla/glean/bindings/HistogramGIFFTMap.h"
#include "mozilla/glean/bindings/MemoryDistribution.h"
#include "mozilla/glean/bindings/Quantity.h"
#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
#include "mozilla/glean/bindings/String.h"
#include "mozilla/glean/bindings/TimingDistribution.h"
#include "mozilla/glean/fog_ffi_generated.h"

enum class DynamicLabel : uint16_t;

namespace mozilla::glean {

namespace impl {

// Helper function to get the current process type string for telemetry labeling
nsCString GetProcessTypeForTelemetry();

static inline void UpdateLabeledMirror(Telemetry::ScalarID aMirrorId,
                                       uint32_t aSubmetricId,
                                       const nsACString& aLabel) {
  GetLabeledMirrorLock().apply([&](const auto& lock) {
    auto tuple = std::make_tuple<Telemetry::ScalarID, nsString>(
        std::move(aMirrorId), NS_ConvertUTF8toUTF16(aLabel));
    lock.ref()->InsertOrUpdate(aSubmetricId, std::move(tuple));
  });
}

static inline void UpdateLabeledDistributionMirror(
    Telemetry::HistogramID aMirrorId, uint32_t aSubmetricId,
    const nsACString& aLabel) {
  GetLabeledDistributionMirrorLock().apply([&](const auto& lock) {
    auto tuple = std::make_tuple<Telemetry::HistogramID, nsCString>(
        std::move(aMirrorId), nsPromiseFlatCString(aLabel));
    lock.ref()->InsertOrUpdate(aSubmetricId, std::move(tuple));
  });
}

template <typename T>
struct MetricOutputTypeTraits;

template <>
struct MetricOutputTypeTraits<BooleanMetric> {
  using Output = bool;
};

template <>
struct MetricOutputTypeTraits<QuantityMetric> {
  using Output = int64_t;
};

template <>
struct MetricOutputTypeTraits<CounterMetric<CounterType::eBaseOrLabeled>> {
  using Output = int32_t;
};

template <>
struct MetricOutputTypeTraits<StringMetric> {
  using Output = nsCString;
};

template <>
struct MetricOutputTypeTraits<CustomDistributionMetric> {
  using Output = DistributionData;
};

template <>
struct MetricOutputTypeTraits<MemoryDistributionMetric> {
  using Output = DistributionData;
};

template <>
struct MetricOutputTypeTraits<TimingDistributionMetric> {
  using Output = DistributionData;
};

template <typename T>
inline uint32_t fog_labeled_get(uint32_t aId, const nsACString* aLabel);

template <typename T>
inline uint32_t fog_labeled_enum_get(uint32_t aId, uint16_t aLabel);

template <typename T, typename O>
inline Maybe<nsTHashMap<nsCString, O>> fog_labeled_test_get_value(
    uint32_t aId, const nsACString* aPingName);

template <typename T>
inline Maybe<nsCString> fog_labeled_test_get_error(uint32_t aId);

#define FOG_LABEL_TYPE_MAP(_type, _name)                                       \
  template <>                                                                  \
  inline uint32_t fog_labeled_get<_type>(uint32_t aId,                         \
                                         const nsACString* aLabel) {           \
    return fog_labeled_##_name##_get(aId, aLabel);                             \
  }                                                                            \
  template <>                                                                  \
  inline uint32_t fog_labeled_enum_get<_type>(uint32_t aId, uint16_t aLabel) { \
    return fog_labeled_##_name##_enum_get(aId, aLabel);                        \
  }                                                                            \
  template <>                                                                  \
  inline Maybe<nsCString> fog_labeled_test_get_error<_type>(uint32_t aId) {    \
    nsCString error;                                                           \
    if (fog_labeled_##_name##_test_get_error(aId, &error)) {                   \
      return Some(error);                                                      \
    }                                                                          \
    return Nothing();                                                          \
  }

FOG_LABEL_TYPE_MAP(BooleanMetric, boolean)
FOG_LABEL_TYPE_MAP(QuantityMetric, quantity)
FOG_LABEL_TYPE_MAP(StringMetric, string)
FOG_LABEL_TYPE_MAP(CounterMetric<CounterType::eBaseOrLabeled>, counter)
FOG_LABEL_TYPE_MAP(CustomDistributionMetric, custom_distribution)
FOG_LABEL_TYPE_MAP(MemoryDistributionMetric, memory_distribution)
FOG_LABEL_TYPE_MAP(TimingDistributionMetric, timing_distribution)

#undef FOG_LABEL_TYPE_MAP

template <typename T, typename E>
class Labeled {
 public:
  constexpr explicit Labeled(uint32_t id) : mId(id) {}

  /**
   * Gets a specific metric for a given label.
   *
   * If a set of acceptable labels were specified in the `metrics.yaml` file,
   * and the given label is not in the set, it will be recorded under the
   * special `OTHER_LABEL` label.
   *
   * If a set of acceptable labels was not specified in the `metrics.yaml` file,
   * only the first 16 unique labels will be used.
   * After that, any additional labels will be recorded under the special
   * `OTHER_LABEL` label.
   *
   * @param aLabel - a snake_case string under 30 characters in length,
   *                 otherwise the metric will be recorded under the special
   *                 `OTHER_LABEL` label and an error will be recorded.
   */
  T Get(const nsACString& aLabel) const {
    uint32_t submetricId = fog_labeled_get<T>(mId, &aLabel);

    Maybe<ScalarID> mirrorId;
    if constexpr (MayBeScalarMirror()) {
      mirrorId = ScalarIdForMetric(mId);
      if (mirrorId) {
        UpdateLabeledMirror(mirrorId.extract(), submetricId, aLabel);
      }
    }
    if constexpr (MayBeDistributionMirror()) {
      if (!mirrorId) {
        if (Maybe<HistogramID> mirrorHgramId = HistogramIdForMetric(mId)) {
          UpdateLabeledDistributionMirror(mirrorHgramId.extract(), submetricId,
                                          aLabel);
        }
      }
    }
    return T(submetricId);
  }

  /**
   * Gets a specific metric for a given label, using the label's enum variant.
   *
   * @param aLabel - a variant of this label's label enum.
   */
  template <typename U = T>
  std::enable_if_t<!std::is_same_v<U, DynamicLabel>, T> EnumGet(
      E aLabel) const {
    uint32_t submetricId =
        fog_labeled_enum_get<T>(mId, static_cast<uint16_t>(aLabel));

    Maybe<ScalarID> mirrorId;
    if constexpr (MayBeScalarMirror()) {
      mirrorId = ScalarIdForMetric(mId);
      if (mirrorId) {
        // Telemetry's keyed scalars operate on (16-bit) strings,
        // so we're going to need the string for this enum.
        nsCString label;
        fog_labeled_enum_to_str(mId, static_cast<uint16_t>(aLabel), &label);
        UpdateLabeledMirror(mirrorId.extract(), submetricId, label);
      }
    }
    if constexpr (MayBeDistributionMirror()) {
      if (!mirrorId) {
        if (Maybe<HistogramID> mirrorHgramId = HistogramIdForMetric(mId)) {
          nsCString label;
          fog_labeled_enum_to_str(mId, static_cast<uint16_t>(aLabel), &label);
          UpdateLabeledDistributionMirror(mirrorHgramId.extract(), submetricId,
                                          label);
        }
      }
    }
    return T(submetricId);
  }

  template <typename M = T,
            typename V = typename MetricOutputTypeTraits<T>::Output>
  Result<Maybe<nsTHashMap<nsCString, V>>, nsCString> TestGetValue(
      const nsCString aPingName = nsCString()) const {
    Maybe<nsCString> err;
    err = fog_labeled_test_get_error<T>(mId);
    if (err.isSome()) {
      return Err(err.value());
    }
    uint64_t count = 0;
    nsTArray<nsCString> keys;
    nsTArray<V> values;
    if constexpr (std::is_same_v<M, BooleanMetric>) {
      fog_labeled_boolean_test_get_value(mId, &aPingName, &count, &keys,
                                         &values);
    } else if constexpr (std::is_same_v<
                             M, CounterMetric<CounterType::eBaseOrLabeled>>) {
      fog_labeled_counter_test_get_value(mId, &aPingName, &count, &keys,
                                         &values);
    } else if constexpr (std::is_same_v<M, StringMetric>) {
      fog_labeled_string_test_get_value(mId, &aPingName, &count, &keys,
                                        &values);
    } else if constexpr (std::is_same_v<M, QuantityMetric>) {
      fog_labeled_quantity_test_get_value(mId, &aPingName, &count, &keys,
                                          &values);
    } else if constexpr (std::is_same_v<M, CustomDistributionMetric>) {
      nsTArray<FfiDistributionData> ffi_values;
      fog_labeled_custom_distribution_test_get_value(mId, &aPingName, &count,
                                                     &keys, &ffi_values);
      DistributionData::fromFFIArray(ffi_values, values);
    } else if constexpr (std::is_same_v<M, MemoryDistributionMetric>) {
      nsTArray<FfiDistributionData> ffi_values;
      fog_labeled_memory_distribution_test_get_value(mId, &aPingName, &count,
                                                     &keys, &ffi_values);
      DistributionData::fromFFIArray(ffi_values, values);
    } else if constexpr (std::is_same_v<M, TimingDistributionMetric>) {
      nsTArray<FfiDistributionData> ffi_values;
      fog_labeled_timing_distribution_test_get_value(mId, &aPingName, &count,
                                                     &keys, &ffi_values);
      DistributionData::fromFFIArray(ffi_values, values);
    } else {
      static_assert(std::is_same_v<M, void>,
                    "There should be a block handling this type");
    }

    nsTHashMap<nsCStringHashKey, V> result;
    for (uint64_t i = 0; i < count; i++) {
      result.InsertOrUpdate(keys[i], std::move(values[i]));
    }

    if (result.Count() == 0) {
      return Maybe<nsTHashMap<nsCString, V>>();
    }
    return Some(std::move(result));
  }

  // This is a workaround only needed in a rare case, ensure it's only compiled
  // there.
  template <typename U = T, typename V = E>
  std::enable_if_t<std::is_same_v<U, TimingDistributionMetric> &&
                       std::is_same_v<V, DynamicLabel>,
                   T>
  MaybeTruncateAndGet(const nsACString& aLabel) const {
    // bug 1959765 is for incorporating this behaviour into the SDK.
    if (aLabel.Length() < 112) {
      return Get(aLabel);
    }
    nsAutoCStringN<111> truncated;
    truncated.Append(aLabel.BeginReading(), 108);
    truncated += "...";
    return Get(truncated);
  }

  /**
   * Gets a specific metric for the current process type.
   *
   * Automatically determines the process type and returns the appropriate
   * labeled metric instance. For content processes, distinguishes between
   * regular "tab" processes, "inference" processes, and "extension" processes.
   */
  T ProcessGet() const { return Get(GetProcessTypeForTelemetry()); }

 protected:
  uint32_t mId;

 private:
  static constexpr bool MayBeScalarMirror() {
    return std::is_same_v<T, BooleanMetric> ||
           std::is_same_v<T, CounterMetric<CounterType::eBaseOrLabeled>> ||
           std::is_same_v<T, QuantityMetric>;
  }

  static constexpr bool MayBeDistributionMirror() {
    return std::is_same_v<T, CounterMetric<CounterType::eBaseOrLabeled>> ||
           std::is_same_v<T, CustomDistributionMetric> ||
           std::is_same_v<T, MemoryDistributionMetric> ||
           std::is_same_v<T, TimingDistributionMetric>;
  }
};

}  // namespace impl

using GleanLabeledTestValue =
    dom::OwningBooleanOrUnsignedLongLongOrUTF8StringOrGleanDistributionData;

class GleanLabeled final : public GleanMetric {
 public:
  explicit GleanLabeled(uint32_t aId, uint32_t aTypeId, nsISupports* aParent)
      : GleanMetric(aParent), mId(aId), mTypeId(aTypeId) {}

  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override final;

  already_AddRefed<GleanMetric> NamedGetter(const nsAString& aName,
                                            bool& aFound);
  bool NameIsEnumerable(const nsAString& aName);
  void GetSupportedNames(nsTArray<nsString>& aNames);

  void TestGetValue(
      const nsACString& aPingName,
      dom::Nullable<dom::Record<nsCString, GleanLabeledTestValue>>& aResult,
      ErrorResult& aRv);

 private:
  virtual ~GleanLabeled() = default;

  const uint32_t mId;
  const uint32_t mTypeId;
};

}  // namespace mozilla::glean

#endif /* mozilla_glean_Labeled_h */