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
|
/* -*- 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/. */
#include "mozilla/glean/bindings/Labeled.h"
#include "mozilla/dom/GleanBinding.h"
#include "mozilla/dom/GleanMetricsBinding.h"
#include "mozilla/dom/Record.h"
#include "mozilla/glean/fog_ffi_generated.h"
#include "mozilla/glean/bindings/GleanJSMetricsLookup.h"
#include "mozilla/glean/bindings/MetricTypes.h"
#include "mozilla/glean/bindings/ScalarGIFFTMap.h"
#include "nsString.h"
#include "nsXULAppAPI.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/RemoteType.h"
namespace mozilla::glean {
JSObject* GleanLabeled::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::GleanLabeled_Binding::Wrap(aCx, this, aGivenProto);
}
already_AddRefed<GleanMetric> GleanLabeled::NamedGetter(const nsAString& aName,
bool& aFound) {
auto label = NS_ConvertUTF16toUTF8(aName);
// All strings will map to a label. Either a valid one or `__other__`.
aFound = true;
uint32_t submetricId = 0;
already_AddRefed<GleanMetric> submetric =
NewSubMetricFromIds(mTypeId, mId, label, &submetricId, mParent);
auto mirrorId = ScalarIdForMetric(mId);
if (mirrorId) {
GetLabeledMirrorLock().apply([&](const auto& lock) {
auto tuple = std::make_tuple<Telemetry::ScalarID, nsString>(
mirrorId.extract(), nsString(aName));
lock.ref()->InsertOrUpdate(submetricId, std::move(tuple));
});
} else if (auto mirrorHgramId = HistogramIdForMetric(mId)) {
GetLabeledDistributionMirrorLock().apply([&](const auto& lock) {
auto tuple = std::make_tuple<Telemetry::HistogramID, nsCString>(
mirrorHgramId.extract(), nsCString(label));
lock.ref()->InsertOrUpdate(submetricId, std::move(tuple));
});
}
return submetric;
}
bool GleanLabeled::NameIsEnumerable(const nsAString& aName) { return false; }
void GleanLabeled::GetSupportedNames(nsTArray<nsString>& aNames) {
// We really don't know, so don't do anything.
}
using GleanLabeledTestValue =
dom::OwningBooleanOrUnsignedLongLongOrUTF8StringOrGleanDistributionData;
void GleanLabeled::TestGetValue(
const nsACString& aPingName,
dom::Nullable<dom::Record<nsCString, GleanLabeledTestValue>>& aResult,
ErrorResult& aRv) {
auto type = static_cast<MetricTypeId>(mTypeId);
Maybe<nsCString> err;
switch (type) {
case MetricTypeId::LABELED_BOOLEAN:
err = impl::fog_labeled_test_get_error<impl::BooleanMetric>(mId);
break;
case MetricTypeId::LABELED_COUNTER:
err = impl::fog_labeled_test_get_error<
impl::CounterMetric<impl::CounterType::eBaseOrLabeled>>(mId);
break;
case MetricTypeId::LABELED_STRING:
err = impl::fog_labeled_test_get_error<impl::StringMetric>(mId);
break;
case MetricTypeId::LABELED_QUANTITY:
err = impl::fog_labeled_test_get_error<impl::QuantityMetric>(mId);
break;
case MetricTypeId::LABELED_CUSTOM_DISTRIBUTION:
err =
impl::fog_labeled_test_get_error<impl::CustomDistributionMetric>(mId);
break;
case MetricTypeId::LABELED_MEMORY_DISTRIBUTION:
err =
impl::fog_labeled_test_get_error<impl::MemoryDistributionMetric>(mId);
break;
case MetricTypeId::LABELED_TIMING_DISTRIBUTION:
err =
impl::fog_labeled_test_get_error<impl::TimingDistributionMetric>(mId);
break;
default:
err = Some(nsCString("type ID supplied is not a Labeled metric type"));
}
if (err.isSome()) {
aResult.SetNull();
aRv.ThrowDataError(err.value());
return;
}
dom::Record<nsCString, GleanLabeledTestValue> retVal;
uint64_t count = 0;
nsTArray<nsCString> keys;
if (type == MetricTypeId::LABELED_BOOLEAN) {
nsTArray<bool> values;
impl::fog_labeled_boolean_test_get_value(mId, &aPingName, &count, &keys,
&values);
for (uint64_t i = 0; i < count; i++) {
auto el = retVal.Entries().AppendElement();
el->mKey = keys[i];
el->mValue.SetAsBoolean() = values[i];
}
} else if (type == MetricTypeId::LABELED_COUNTER) {
nsTArray<int32_t> values;
impl::fog_labeled_counter_test_get_value(mId, &aPingName, &count, &keys,
&values);
for (uint64_t i = 0; i < count; i++) {
auto el = retVal.Entries().AppendElement();
el->mKey = keys[i];
el->mValue.SetAsUnsignedLongLong() = values[i];
}
} else if (type == MetricTypeId::LABELED_STRING) {
nsTArray<nsCString> values;
impl::fog_labeled_string_test_get_value(mId, &aPingName, &count, &keys,
&values);
for (uint64_t i = 0; i < count; i++) {
auto el = retVal.Entries().AppendElement();
el->mKey = keys[i];
el->mValue.SetAsUTF8String() = values[i];
}
} else if (type == MetricTypeId::LABELED_QUANTITY) {
nsTArray<int64_t> values;
impl::fog_labeled_quantity_test_get_value(mId, &aPingName, &count, &keys,
&values);
for (uint64_t i = 0; i < count; i++) {
auto el = retVal.Entries().AppendElement();
el->mKey = keys[i];
el->mValue.SetAsUnsignedLongLong() = values[i];
}
} else if (type == MetricTypeId::LABELED_CUSTOM_DISTRIBUTION ||
type == MetricTypeId::LABELED_MEMORY_DISTRIBUTION ||
type == MetricTypeId::LABELED_TIMING_DISTRIBUTION) {
nsTArray<impl::FfiDistributionData> values;
if (type == MetricTypeId::LABELED_CUSTOM_DISTRIBUTION) {
impl::fog_labeled_custom_distribution_test_get_value(
mId, &aPingName, &count, &keys, &values);
} else if (type == MetricTypeId::LABELED_MEMORY_DISTRIBUTION) {
impl::fog_labeled_memory_distribution_test_get_value(
mId, &aPingName, &count, &keys, &values);
} else if (type == MetricTypeId::LABELED_TIMING_DISTRIBUTION) {
impl::fog_labeled_timing_distribution_test_get_value(
mId, &aPingName, &count, &keys, &values);
}
for (size_t i = 0; i < keys.Length(); i++) {
auto el = retVal.Entries().AppendElement();
el->mKey = keys[i];
auto& distributionData = el->mValue.SetAsGleanDistributionData();
distributionData.mCount = values[i].count;
distributionData.mSum = values[i].sum;
for (size_t j = 0; j < values[i].keys.Length(); j++) {
auto elem = distributionData.mValues.Entries().AppendElement();
elem->mKey.AppendInt(values[i].keys[j]);
elem->mValue = values[i].values[j];
}
}
} else {
MOZ_ASSERT_UNREACHABLE(
"The function should have returned instead of hitting this.");
}
aResult.SetValue(std::move(retVal));
}
namespace impl {
// Helper function to get the current process type string for telemetry labeling
nsCString GetProcessTypeForTelemetry() {
nsCString processType(XRE_GetProcessTypeString());
// For content processes, check the specific remote type
if (processType.EqualsLiteral("tab")) {
auto* cc = mozilla::dom::ContentChild::GetSingleton();
if (cc) {
const nsACString& remoteType = cc->GetRemoteType();
if (remoteType == EXTENSION_REMOTE_TYPE) {
processType.AssignLiteral("extension");
} else if (remoteType == INFERENCE_REMOTE_TYPE) {
processType.AssignLiteral("inference");
}
// Otherwise keep "tab" for regular content processes
}
}
return processType;
}
} // namespace impl
} // namespace mozilla::glean
|