httpssvc_metrics.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef NET_DNS_HTTPSSVC_METRICS_H_
  5. #define NET_DNS_HTTPSSVC_METRICS_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/containers/flat_set.h"
  9. #include "base/strings/string_piece.h"
  10. #include "base/time/time.h"
  11. #include "net/base/net_export.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace net {
  14. // These values are persisted to logs. Entries should not be renumbered and
  15. // numeric values should never be reused. (See HttpssvcDnsRcode in
  16. // tools/metrics/histograms/enums.xml.)
  17. enum HttpssvcDnsRcode {
  18. kTimedOut = 0,
  19. kUnrecognizedRcode,
  20. kMissingDnsResponse,
  21. kNoError,
  22. kFormErr,
  23. kServFail,
  24. kNxDomain,
  25. kNotImp,
  26. kRefused,
  27. kMaxValue = kRefused,
  28. };
  29. // Helper that classifies domains as experimental, control, or other. Queries
  30. // feature params and caches result to avoid repeated parsing.
  31. class NET_EXPORT_PRIVATE HttpssvcExperimentDomainCache {
  32. public:
  33. HttpssvcExperimentDomainCache();
  34. ~HttpssvcExperimentDomainCache();
  35. bool IsExperimental(base::StringPiece domain);
  36. bool IsControl(base::StringPiece domain);
  37. private:
  38. bool ListContainsDomain(
  39. const std::string& domain_list,
  40. base::StringPiece domain,
  41. absl::optional<base::flat_set<std::string>>& in_out_cached_list);
  42. absl::optional<base::flat_set<std::string>> experimental_list_;
  43. absl::optional<base::flat_set<std::string>> control_list_;
  44. };
  45. // Translate an RCODE value to the |HttpssvcDnsRcode| enum, which is used for
  46. // HTTPSSVC experimentation. The goal is to keep these values in a small,
  47. // contiguous range in order to satisfy the UMA enumeration function's
  48. // requirements. This function never returns |kTimedOut| |kUnrecognizedRcode|,
  49. // or |kMissingDnsResponse|.
  50. enum HttpssvcDnsRcode TranslateDnsRcodeForHttpssvcExperiment(uint8_t rcode);
  51. // Tool for aggregating HTTPSSVC and INTEGRITY metrics. Accumulates metrics via
  52. // the Save* methods. Records metrics to UMA on destruction.
  53. class NET_EXPORT_PRIVATE HttpssvcMetrics {
  54. public:
  55. HttpssvcMetrics(bool secure, bool expect_intact);
  56. ~HttpssvcMetrics();
  57. HttpssvcMetrics(HttpssvcMetrics&) = delete;
  58. HttpssvcMetrics(HttpssvcMetrics&&) = delete;
  59. // May be called many times.
  60. void SaveForAddressQuery(base::TimeDelta resolve_time,
  61. enum HttpssvcDnsRcode rcode);
  62. // Save the fact that the non-integrity queries failed. Prevents metrics from
  63. // being recorded.
  64. void SaveAddressQueryFailure();
  65. // Must only be called once.
  66. void SaveForIntegrity(enum HttpssvcDnsRcode rcode,
  67. const std::vector<bool>& condensed_records,
  68. base::TimeDelta integrity_resolve_time);
  69. void SaveForHttps(enum HttpssvcDnsRcode rcode,
  70. const std::vector<bool>& condensed_records,
  71. base::TimeDelta https_resolve_time);
  72. private:
  73. enum class RecordType { kIntegrity, kHttps };
  74. std::string BuildMetricName(RecordType type,
  75. base::StringPiece leaf_name) const;
  76. // Records all the aggregated metrics to UMA.
  77. void RecordMetrics();
  78. void RecordCommonMetrics();
  79. void RecordExpectIntactMetrics();
  80. void RecordExpectNoerrorMetrics();
  81. const bool secure_;
  82. const bool expect_intact_;
  83. // RecordIntegrityMetrics() will do nothing when |disqualified_| is true.
  84. bool disqualified_ = false;
  85. bool already_recorded_ = false;
  86. absl::optional<enum HttpssvcDnsRcode> rcode_integrity_;
  87. absl::optional<enum HttpssvcDnsRcode> rcode_https_;
  88. size_t num_integrity_records_ = 0;
  89. size_t num_https_records_ = 0;
  90. absl::optional<bool> is_integrity_intact_;
  91. absl::optional<bool> is_https_parsable_;
  92. // We never make multiple INTEGRITY or HTTPS queries per DnsTask, so we only
  93. // need one TimeDelta for each qtype.
  94. absl::optional<base::TimeDelta> integrity_resolve_time_;
  95. absl::optional<base::TimeDelta> https_resolve_time_;
  96. std::vector<base::TimeDelta> address_resolve_times_;
  97. };
  98. } // namespace net
  99. #endif // NET_DNS_HTTPSSVC_METRICS_H_