bluetooth_metrics_helper.cc 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2021 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. #include "device/bluetooth/dbus/bluetooth_metrics_helper.h"
  5. #include "base/metrics/histogram_functions.h"
  6. #include "base/strings/stringprintf.h"
  7. #include "dbus/message.h"
  8. namespace bluez {
  9. namespace {
  10. // This enum is tied directly to a UMA enum defined in
  11. // tools/metrics/histograms/enums.xml, existing entries should not be modified.
  12. enum class DBusResult {
  13. kSuccess = 0,
  14. // The following three are all different types of timeouts reported by the
  15. // DBus service:
  16. // "No reply to a message expecting one, usually means a timeout occurred."
  17. kErrorNoReply = 1,
  18. // "Certain timeout errors, possibly ETIMEDOUT on a socket."
  19. kErrorTimeout = 2,
  20. // "Certain timeout errors, e.g. while starting a service"
  21. kErrorTimedOut = 3,
  22. kErrorNotSupported = 4,
  23. kErrorAccessDenied = 5,
  24. kErrorDisconnected = 6,
  25. kErrorResponseMissing = 7,
  26. kErrorUnknown = 8,
  27. kMaxValue = kErrorUnknown
  28. };
  29. DBusResult GetResult(dbus::ErrorResponse* response) {
  30. if (!response) {
  31. return DBusResult::kErrorResponseMissing;
  32. }
  33. const std::string& error_name = response->GetErrorName();
  34. if (error_name == DBUS_ERROR_NO_REPLY) {
  35. return DBusResult::kErrorNoReply;
  36. }
  37. if (error_name == DBUS_ERROR_TIMEOUT) {
  38. return DBusResult::kErrorTimeout;
  39. }
  40. if (error_name == DBUS_ERROR_TIMED_OUT) {
  41. return DBusResult::kErrorTimedOut;
  42. }
  43. if (error_name == DBUS_ERROR_NOT_SUPPORTED) {
  44. return DBusResult::kErrorNotSupported;
  45. }
  46. if (error_name == DBUS_ERROR_ACCESS_DENIED) {
  47. return DBusResult::kErrorAccessDenied;
  48. }
  49. if (error_name == DBUS_ERROR_DISCONNECTED) {
  50. return DBusResult::kErrorDisconnected;
  51. }
  52. return DBusResult::kErrorUnknown;
  53. }
  54. } // namespace
  55. void RecordSuccess(const std::string& method_name, base::Time start_time) {
  56. base::TimeDelta latency = base::Time::Now() - start_time;
  57. std::string result_metric =
  58. base::StringPrintf(kResultMetric, method_name.c_str());
  59. std::string latency_metric =
  60. base::StringPrintf(kLatencyMetric, method_name.c_str());
  61. base::UmaHistogramMediumTimes(latency_metric, latency);
  62. base::UmaHistogramEnumeration(result_metric, DBusResult::kSuccess);
  63. }
  64. void RecordFailure(const std::string& method_name,
  65. dbus::ErrorResponse* response) {
  66. std::string result_metric =
  67. base::StringPrintf(kResultMetric, method_name.c_str());
  68. DBusResult result = GetResult(response);
  69. base::UmaHistogramEnumeration(result_metric, result);
  70. }
  71. } // namespace bluez