File: ip_protection_telemetry_uma.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (361 lines) | stat: -rw-r--r-- 12,825 bytes parent folder | download | duplicates (5)
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ip_protection/common/ip_protection_telemetry_uma.h"

#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>

#include "base/check.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/time/time.h"
#include "components/ip_protection/common/ip_protection_data_types.h"
#include "components/ip_protection/common/ip_protection_telemetry.h"
#include "net/base/proxy_chain.h"

namespace ip_protection {

namespace {

// An enumeration of the `chain_id` values supplied in the GetProxyInfo RPC
// response, for use with `UmaHistogramEnumeration`. These values are persisted
// to logs. Entries should not be renumbered and numeric values should never be
// reused.
enum class ProxyChainId {
  kUnknown = 0,
  kChain1 = 1,
  kChain2 = 2,
  kChain3 = 3,
  kMaxValue = kChain3,
};

// Convert a chain_id as given in `ProxyChain::ip_protection_chain_id()` into
// a value of the `ProxyChainId` enum.
ProxyChainId ChainIdToEnum(int chain_id) {
  static_assert(net::ProxyChain::kMaxIpProtectionChainId ==
                    static_cast<int>(ProxyChainId::kMaxValue),
                "maximum `chain_id` must match between `net::ProxyChain` and "
                "`ProxyChainId`");
  CHECK(chain_id >= static_cast<int>(ProxyChainId::kUnknown) &&
        chain_id <= static_cast<int>(ProxyChainId::kMaxValue));
  return static_cast<ProxyChainId>(chain_id);
}

std::string ProxyLayerToString(ProxyLayer proxy_layer) {
  switch (proxy_layer) {
    case ProxyLayer::kProxyA:
      return "ProxyA";
    case ProxyLayer::kProxyB:
      return "ProxyB";
  }
}

// Converts an IpProtectionTokenCountEvent enum value to its corresponding
// string representation for histogram naming.
std::string TokenCountEventToString(IpProtectionTokenCountEvent event) {
  switch (event) {
    case IpProtectionTokenCountEvent::kIssued:
      return "Issued";
    case IpProtectionTokenCountEvent::kSpent:
      return "Spent";
    case IpProtectionTokenCountEvent::kExpired:
      return "Expired";
  }
  NOTREACHED();
}

}  // namespace

IpProtectionTelemetry& Telemetry() {
  static IpProtectionTelemetryUma instance;
  return instance;
}

void IpProtectionTelemetryUma::OAuthTokenFetchComplete(
    base::TimeDelta duration) {
  base::UmaHistogramTimes("NetworkService.IpProtection.OAuthTokenFetchTime",
                          duration);
}

void IpProtectionTelemetryUma::TokenBatchFetchComplete(
    TryGetAuthTokensResult result,
    std::optional<base::TimeDelta> duration) {
  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.TryGetAuthTokensResult", result);
  if (duration.has_value()) {
    base::UmaHistogramTimes("NetworkService.IpProtection.TokenBatchRequestTime",
                            *duration);
  }
}

void IpProtectionTelemetryUma::AndroidTokenBatchFetchComplete(
    TryGetAuthTokensAndroidResult result,
    std::optional<base::TimeDelta> duration) {
  base::UmaHistogramEnumeration(
      "NetworkService.AwIpProtection.TryGetAuthTokensResult", result);
  if (duration.has_value()) {
    base::UmaHistogramTimes(
        "NetworkService.AwIpProtection.TokenBatchRequestTime", *duration);
  }
}

void IpProtectionTelemetryUma::ProxyChainFallback(int proxy_chain_id) {
  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.ProxyChainFallback",
      ChainIdToEnum(proxy_chain_id));
}

void IpProtectionTelemetryUma::EmptyTokenCache(ProxyLayer value) {
  base::UmaHistogramEnumeration("NetworkService.IpProtection.EmptyTokenCache2",
                                value);
}

void IpProtectionTelemetryUma::ProxyResolution(ProxyResolutionResult result) {
  base::UmaHistogramEnumeration("NetworkService.IpProtection.ProxyResolution",
                                result);

  // Translate the result into eligibility and availability values.
  ProtectionEligibility eligibility;
  auto record_availability = [](bool are_auth_tokens_available,
                                bool is_proxy_list_available) {
    base::UmaHistogramBoolean(
        "NetworkService.IpProtection.AreAuthTokensAvailable",
        are_auth_tokens_available);
    base::UmaHistogramBoolean(
        "NetworkService.IpProtection.IsProxyListAvailable",
        is_proxy_list_available);
    base::UmaHistogramBoolean(
        "NetworkService.IpProtection.ProtectionIsAvailableForRequest",
        are_auth_tokens_available && is_proxy_list_available);
  };

  switch (result) {
    case ProxyResolutionResult::kMdlNotPopulated:
      eligibility = ProtectionEligibility::kUnknown;
      break;
    case ProxyResolutionResult::kNoMdlMatch:
      eligibility = ProtectionEligibility::kIneligible;
      break;
    case ProxyResolutionResult::kSettingDisabled:
      eligibility = ProtectionEligibility::kEligible;
      break;
    case ProxyResolutionResult::kProxyListNotAvailable:
      eligibility = ProtectionEligibility::kEligible;
      record_availability(
          /*are_auth_tokens_available=*/false,
          /*is_proxy_list_available=*/false);
      break;
    case ProxyResolutionResult::kTokensNeverAvailable:
      // fall through to the same as exhausted tokens for the purpose of this
      // metric.
    case ProxyResolutionResult::kTokensExhausted:
      eligibility = ProtectionEligibility::kEligible;
      record_availability(
          /*are_auth_tokens_available=*/false,
          /*is_proxy_list_available=*/true);
      break;
    case ProxyResolutionResult::kHasSiteException:
      eligibility = ProtectionEligibility::kEligible;
      record_availability(
          /*are_auth_tokens_available=*/true,
          /*is_proxy_list_available=*/true);
      break;
    case ProxyResolutionResult::kAttemptProxy:
      eligibility = ProtectionEligibility::kEligible;
      record_availability(
          /*are_auth_tokens_available=*/true,
          /*is_proxy_list_available=*/true);
      break;
  }

  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.RequestIsEligibleForProtection",
      eligibility);
}

void IpProtectionTelemetryUma::GetAuthTokenResultForGeo(
    bool is_token_available,
    bool is_cache_empty,
    bool does_requested_geo_match_current) {
  base::UmaHistogramBoolean("NetworkService.IpProtection.GetAuthTokenResult",
                            is_token_available);
  AuthTokenResultForGeo result;
  if (is_token_available) {
    if (does_requested_geo_match_current) {
      result = AuthTokenResultForGeo::kAvailableForCurrentGeo;
    } else {
      result = AuthTokenResultForGeo::kAvailableForOtherCachedGeo;
    }
  } else if (!is_cache_empty) {
    result = AuthTokenResultForGeo::kUnavailableButCacheContainsTokens;
  } else {
    result = AuthTokenResultForGeo::kUnavailableCacheEmpty;
  }
  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.GetAuthTokenResultForGeo", result);
}

void IpProtectionTelemetryUma::TokenBatchGenerationComplete(
    base::TimeDelta duration) {
  base::UmaHistogramMediumTimes(
      "NetworkService.IpProtection.TokenBatchGenerationTime", duration);
}

void IpProtectionTelemetryUma::TryGetAuthTokensError(uint32_t hash) {
  base::UmaHistogramSparse("NetworkService.IpProtection.TryGetAuthTokensErrors",
                           hash);
}

void IpProtectionTelemetryUma::GeoChangeTokenPresence(bool tokens_present) {
  base::UmaHistogramBoolean(
      "NetworkService.IpProtection.GeoChangeTokenPresence", tokens_present);
}

void IpProtectionTelemetryUma::ProxyListRefreshComplete(
    GetProxyListResult result,
    std::optional<base::TimeDelta> duration) {
  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.GetProxyListResult", result);
  if (duration.has_value()) {
    base::UmaHistogramMediumTimes(
        "NetworkService.IpProtection.ProxyListRefreshTime", *duration);
  }
}

void IpProtectionTelemetryUma::TokenSpendRate(ProxyLayer proxy_layer,
                                              int value) {
  base::UmaHistogramCounts1000(
      base::StrCat({"NetworkService.IpProtection.",
                    ProxyLayerToString(proxy_layer), ".TokenSpendRate"}),
      value);
}

void IpProtectionTelemetryUma::TokenExpirationRate(ProxyLayer proxy_layer,
                                                   int value) {
  base::UmaHistogramCounts100000(
      base::StrCat({"NetworkService.IpProtection.",
                    ProxyLayerToString(proxy_layer), ".TokenExpirationRate"}),
      value);
}

void IpProtectionTelemetryUma::MdlEstimatedMemoryUsage(size_t usage) {
  base::UmaHistogramCustomCounts(
      "NetworkService.MaskedDomainList.EstimatedMemoryUsage",
      usage / 1024,  // Convert to KB
      /*min=*/1,
      /*exclusive_max=*/5000,  // Maximum of 5MB
      /*buckets=*/50);
}

void IpProtectionTelemetryUma::MdlEstimatedDiskUsage(int64_t usage) {
  base::UmaHistogramCustomCounts("NetworkService.MaskedDomainList.DiskUsage",
                                 usage / 1024,  // Convert to KB
                                 /*min=*/1,
                                 /*exclusive_max=*/5000,  // Maximum of 5MB
                                 /*buckets=*/50);
}

void IpProtectionTelemetryUma::MdlSize(int64_t size) {
  base::UmaHistogramCustomCounts("NetworkService.MaskedDomainList.Size2",
                                 size / 1024,  // Convert to KB
                                 /*min=*/1,
                                 /*exclusive_max=*/5000,  // Maximum of 5MB
                                 /*buckets=*/50);
}

void IpProtectionTelemetryUma::AndroidAuthClientCreationTime(
    base::TimeDelta duration) {
  base::UmaHistogramMediumTimes(
      "NetworkService.IpProtection.AndroidAuthClient.CreationTime", duration);
}

void IpProtectionTelemetryUma::AndroidAuthClientGetInitialDataTime(
    base::TimeDelta duration) {
  base::UmaHistogramMediumTimes(
      "NetworkService.IpProtection.AndroidAuthClient.GetInitialDataTime",
      duration);
}

void IpProtectionTelemetryUma::AndroidAuthClientAuthAndSignTime(
    base::TimeDelta duration) {
  base::UmaHistogramMediumTimes(
      "NetworkService.IpProtection.AndroidAuthClient.AuthAndSignTime",
      duration);
}

void IpProtectionTelemetryUma::MdlFirstUpdateTime(base::TimeDelta duration) {
  base::UmaHistogramTimes("NetworkService.MaskedDomainList.FirstUpdateTime",
                          duration);
}

void IpProtectionTelemetryUma::MdlMatchesTime(base::TimeDelta duration) {
  base::UmaHistogramMicrosecondsTimes(
      "NetworkService.MaskedDomainList.MatchesTime", duration);
}

void IpProtectionTelemetryUma::GetProbabilisticRevealTokensComplete(
    TryGetProbabilisticRevealTokensStatus status,
    base::TimeDelta duration) {
  base::UmaHistogramEnumeration(
      "NetworkService.IpProtection.GetProbabilisticRevealTokensResult", status);

  if (status == TryGetProbabilisticRevealTokensStatus::kSuccess) {
    base::UmaHistogramTimes(
        "NetworkService.IpProtection.ProbabilisticRevealTokensRequestTime",
        duration);
  }
}

void IpProtectionTelemetryUma::IsProbabilisticRevealTokenAvailable(
    bool is_initial_request,
    bool is_token_available) {
  if (is_initial_request) {
    base::UmaHistogramBoolean(
        "NetworkService.IpProtection."
        "IsProbabilisticRevealTokenAvailableOnInitialRequest",
        is_token_available);
  } else {
    base::UmaHistogramBoolean(
        "NetworkService.IpProtection."
        "IsProbabilisticRevealTokenAvailableOnSubsequentRequest",
        is_token_available);
  }
}

void IpProtectionTelemetryUma::ProbabilisticRevealTokenRandomizationTime(
    base::TimeDelta duration) {
  base::UmaHistogramTimes(
      "NetworkService.IpProtection.ProbabilisticRevealTokenRandomizationTime",
      duration);
}

void IpProtectionTelemetryUma::QuicProxiesFailed(int after_requests) {
  base::UmaHistogramCounts1000("NetworkService.IpProtection.QuicProxiesFailed",
                               after_requests);
}

void IpProtectionTelemetryUma::RecordTokenCountEvent(
    ProxyLayer layer,
    IpProtectionTokenCountEvent event,
    int count) {
  // Construct the histogram name dynamically based on the layer and event type.
  // Example: "NetworkService.IpProtection.ProxyA.TokenCount.Issued"
  std::string histogram_name = base::StrCat({
      "NetworkService.IpProtection.",
      ProxyLayerToString(layer),
      ".TokenCount.",
      TokenCountEventToString(event),
  });

  // Using a maximum of 1000 counts, since the maximum number of tokens per
  // event would generally be around the batch or cache size which is typically
  // much less than 1000.
  base::UmaHistogramCounts1000(histogram_name, count);
}

}  // namespace ip_protection