MetricsManager.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <GlobalStatistics.h>
  10. #include <MetricsQueue.h>
  11. #include <IdentityProvider.h>
  12. #include <AWSMetricsServiceApi.h>
  13. #include <AzCore/Jobs/JobContext.h>
  14. #include <AzCore/Jobs/JobManager.h>
  15. #include <AzCore/Outcome/Outcome.h>
  16. #include <AzCore/Settings/SettingsRegistry.h>
  17. #include <AzCore/std/smart_ptr/unique_ptr.h>
  18. #include <AzCore/std/parallel/mutex.h>
  19. namespace AWSMetrics
  20. {
  21. class ClientConfiguration;
  22. //! Metrics manager handles direct or batch sending metrics to backend
  23. class MetricsManager
  24. {
  25. public:
  26. static const unsigned int DesiredMaxWorkers = 2;
  27. MetricsManager();
  28. virtual ~MetricsManager();
  29. //! Initializing the metrics manager
  30. //! @return Whether the operation is successful.
  31. bool Init();
  32. //! Start sending metircs to the backend or a local file.
  33. void StartMetrics();
  34. //! Stop sending metircs to the backend or a local file.
  35. void ShutdownMetrics();
  36. //! Implementation for submitting metrics.
  37. //! Metrics will be buffered before sending in batch.
  38. //! @param metricsAttributes Attributes of the metrics.
  39. //! @param eventPriority Priority of the event. Default to 0 which is considered as the highest priority.
  40. //! @param eventSourceOverride Event source used to override the default value.
  41. //! @return Whether the submission is successful.
  42. bool SubmitMetrics(const AZStd::vector<MetricsAttribute>& metricsAttributes = AZStd::vector<MetricsAttribute>(),
  43. int eventPriority = 0, const AZStd::string& eventSourceOverride = "");
  44. //! Implementation for sending metrics asynchronously.
  45. //! @param metricsAttributes Attributes of the metrics.
  46. //! @param eventSourceOverride Event source used to override the default value.
  47. //! @param eventPriority Priority of the event. Default to 0 which is considered as the highest priority.
  48. //! @return Whether the request is sent successfully.
  49. bool SendMetricsAsync(const AZStd::vector<MetricsAttribute>& metricsAttributes = AZStd::vector<MetricsAttribute>(),
  50. int eventPriority = 0, const AZStd::string& eventSourceOverride = "");
  51. //! Update the global stats and add qualified failed metrics events back to the buffer for retry.
  52. //! @param metricsEventsInRequest Metrics events in the original request.
  53. //! @param responseEntries Response list for all the processed metrics events.
  54. void OnResponseReceived(const MetricsQueue& metricsEventsInRequest, const ServiceAPI::PostMetricsEventsResponseEntries& responseEntries = ServiceAPI::PostMetricsEventsResponseEntries());
  55. //! Implementation for flush all metrics buffered in memory.
  56. void FlushMetricsAsync();
  57. //! Get the total number of metrics buffered in the metrics queue.
  58. //! @return the number of buffered metrics
  59. AZ::s64 GetNumBufferedMetrics();
  60. //! Retrieve the global statistics for sending metrics.
  61. //! @return Global statistics for sending metrics
  62. const GlobalStatistics& GetGlobalStatistics() const;
  63. //! Enable/Disable the offline recording and resubmit metrics stored in the local metrics file if the client switches to the online mode.
  64. //! @param enable Whether to enable the offline recording.
  65. //! @param submitLocalMetrics Whether to resubmit local metrics if exist.
  66. void UpdateOfflineRecordingStatus(bool enable, bool submitLocalMetrics = false);
  67. //! Get the directory of the local metrics file.
  68. //! @return Directory of the local metrics file.
  69. const char* GetMetricsFileDirectory() const;
  70. //! Get the path to the local metrics file.
  71. //! @return Path to the local metrics file.
  72. const char* GetMetricsFilePath() const;
  73. //! Get the total number of requests for sending metrics events.
  74. //! This value could be different to the number of submitted metrics events since metrics events could be sent in batch.
  75. //! @return Total number of requests for sending metrics events.
  76. int GetNumTotalRequests() const;
  77. protected:
  78. //! Send metrics to a local file.
  79. //! @param metricsQueue metricsQueue Metrics queue that stores the metrics.
  80. //! @return Outcome of the operation.
  81. virtual AZ::Outcome<void, AZStd::string> SendMetricsToFile(AZStd::shared_ptr<MetricsQueue> metricsQueue);
  82. private:
  83. //! Job management
  84. void SetupJobContext();
  85. //! Monitor the buffered metrics queue and consume metrics when the size or elapsed time limit is hit.
  86. void MonitorMetricsQueue();
  87. //! Send metrics to a local file or the backend service asynchronously.
  88. //! @param metricsQueue Metrics queue that stores the metrics.
  89. void SendMetricsAsync(AZStd::shared_ptr<MetricsQueue> metricsQueue);
  90. //! Send the batched metrics events to the local metrics file asynchronously.
  91. //! @param metricsQueue Metrics events to send.
  92. void SendMetricsToLocalFileAsync(AZStd::shared_ptr<MetricsQueue> metricsQueue);
  93. //! Send the batched metrics events to the Service API asynchronously.
  94. //! @param metricsQueue Metrics events to send.
  95. void SendMetricsToServiceApiAsync(const MetricsQueue& metricsQueue);
  96. //! Push metrics events to the front of the queue for retry.
  97. //! @param metricsEventsForRetry Metrics events for retry.
  98. void PushMetricsForRetry(MetricsQueue& metricsEventsForRetry);
  99. void SubmitLocalMetricsAsync();
  100. AZStd::mutex m_metricsMutex; //!< Mutex to protect the metrics queue
  101. MetricsQueue m_metricsQueue; //!< Queue fo buffering the metrics events
  102. AZStd::mutex m_metricsFileMutex; //!< Mutex to protect the local metrics file
  103. AZStd::atomic<int> m_sendMetricsId;//!< Request ID for sending metrics
  104. AZStd::thread m_monitorThread; //!< Thread to monitor and consume the metrics queue
  105. AZStd::atomic<bool> m_monitorTerminated;
  106. AZStd::binary_semaphore m_waitEvent;
  107. // Client Configurations.
  108. AZStd::unique_ptr<ClientConfiguration> m_clientConfiguration;
  109. AZStd::unique_ptr<AZ::JobManager> m_jobManager{ nullptr };
  110. AZStd::unique_ptr <AZ::JobContext> m_jobContext{ nullptr };
  111. AZStd::unique_ptr<AZ::SettingsRegistryInterface> m_settingsRegistry;
  112. AZStd::unique_ptr<IdentityProvider> m_clientIdProvider;
  113. GlobalStatistics m_globalStats;
  114. };
  115. }