AWSMetricsSystemComponent.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <AWSCoreBus.h>
  9. #include <AWSMetricsSystemComponent.h>
  10. #include <MetricsManager.h>
  11. #include <AzCore/IO/FileIO.h>
  12. #include <AzCore/RTTI/BehaviorContext.h>
  13. #include <AzCore/Serialization/EditContext.h>
  14. namespace AWSMetrics
  15. {
  16. class AWSMetricsNotificationBusHandler
  17. : public AWSMetricsNotificationBus::Handler
  18. , public AZ::BehaviorEBusHandler
  19. {
  20. public:
  21. AZ_EBUS_BEHAVIOR_BINDER(AWSMetricsNotificationBusHandler
  22. , "{5329566F-3E7E-4A04-9C43-DB11232D92CA}"
  23. , AZ::SystemAllocator
  24. , OnSendMetricsSuccess
  25. , OnSendMetricsFailure
  26. );
  27. void OnSendMetricsSuccess(int requestId) override
  28. {
  29. Call(FN_OnSendMetricsSuccess, requestId);
  30. }
  31. void OnSendMetricsFailure(int requestId, const AZStd::string& errorMessage) override
  32. {
  33. Call(FN_OnSendMetricsFailure, requestId, errorMessage);
  34. }
  35. };
  36. AWSMetricsSystemComponent::AWSMetricsSystemComponent()
  37. : m_metricsManager(AZStd::make_unique<MetricsManager>())
  38. {
  39. }
  40. AWSMetricsSystemComponent::~AWSMetricsSystemComponent() = default;
  41. void AWSMetricsSystemComponent::Reflect(AZ::ReflectContext* context)
  42. {
  43. ReflectMetricsAttribute(context);
  44. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  45. {
  46. serialize->Class<AWSMetricsSystemComponent, AZ::Component>()
  47. ->Version(0)
  48. ;
  49. if (AZ::EditContext* ec = serialize->GetEditContext())
  50. {
  51. ec->Class<AWSMetricsSystemComponent>("AWSMetrics", "Generate and submit metrics to the metrics analytics pipeline")
  52. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  53. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  54. ;
  55. }
  56. }
  57. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  58. {
  59. behaviorContext->EBus<AWSMetricsRequestBus>("AWSMetricsRequestBus", "Generate and submit metrics to the metrics analytics pipeline")
  60. ->Attribute(AZ::Script::Attributes::Category, "AWSMetrics")
  61. ->Event(
  62. "SubmitMetrics", &AWSMetricsRequestBus::Events::SubmitMetrics,
  63. { { { "Metrics Attributes list", "The list of metrics attributes to submit." },
  64. { "Event priority", "Priority of the event. Defaults to 0, which is highest priority." },
  65. { "Event source override", "Event source used to override the default, 'AWSMetricGem'." },
  66. { "Buffer metrics", "Whether to buffer metrics and send them in a batch." } } })
  67. ->Event("FlushMetrics", &AWSMetricsRequestBus::Events::FlushMetrics)
  68. ;
  69. behaviorContext->EBus<AWSMetricsNotificationBus>("AWSMetricsNotificationBus", "Notifications for sending metrics to the metrics analytics pipeline")
  70. ->Attribute(AZ::Script::Attributes::Category, "AWSMetrics")
  71. ->Handler<AWSMetricsNotificationBusHandler>()
  72. ;
  73. }
  74. }
  75. void AWSMetricsSystemComponent::ReflectMetricsAttribute(AZ::ReflectContext* reflection)
  76. {
  77. // Reflect MetricsAttribute
  78. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(reflection))
  79. {
  80. serialize->Class<MetricsAttribute>()
  81. ->Version(1)
  82. ;
  83. serialize->RegisterGenericType<Attributes>();
  84. }
  85. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(reflection))
  86. {
  87. behaviorContext->Class<MetricsAttribute>("AWSMetrics_MetricsAttribute")
  88. ->Attribute(AZ::Script::Attributes::Category, "AWSMetrics")
  89. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  90. ->Method("SetName", &MetricsAttribute::SetName,
  91. {{{"Metrics Attribute Name", "Name of the metrics attribute."}}})
  92. ->Method("SetStrValue", (void (MetricsAttribute::*)(const AZStd::string& val)) &MetricsAttribute::SetVal,
  93. {{{"Metrics Attribute Value", "String value of the metrics attribute."}}})
  94. ->Method("SetIntValue", (void (MetricsAttribute::*)(int)) &MetricsAttribute::SetVal,
  95. {{{"Metrics Attribute Value", "Integer value of the metrics attribute."}}})
  96. ->Method("SetDoubleValue", (void (MetricsAttribute::*)(double)) &MetricsAttribute::SetVal,
  97. {{{"Metrics Attribute Value", "Double value of the metrics attribute."}}})
  98. ;
  99. }
  100. AttributeSubmissionList::Reflect(reflection);
  101. }
  102. void AWSMetricsSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  103. {
  104. provided.push_back(AZ_CRC_CE("AWSMetricsService"));
  105. }
  106. void AWSMetricsSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  107. {
  108. incompatible.push_back(AZ_CRC_CE("AWSMetricsService"));
  109. }
  110. void AWSMetricsSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  111. {
  112. required.push_back(AZ_CRC_CE("AWSCoreService"));
  113. }
  114. void AWSMetricsSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  115. {
  116. AZ_UNUSED(dependent);
  117. }
  118. void AWSMetricsSystemComponent::DumpStats(const AZ::ConsoleCommandContainer& arguments)
  119. {
  120. AZ_UNUSED(arguments);
  121. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  122. AZ_Printf("AWSMetrics", "Total number of metrics events sent to the backend/local file: %u", stats.m_numEvents.load());
  123. AZ_Printf("AWSMetrics", "Total number of metrics events sent to the backend/local file successfully: %u", stats.m_numSuccesses.load());
  124. AZ_Printf("AWSMetrics", "Total size of metrics events sent to the backend/local file successfully: %u bytes", stats.m_sendSizeInBytes.load());
  125. AZ_Printf("AWSMetrics", "Total number of metrics events failed to be sent to the backend/local file: %u", stats.m_numErrors.load());
  126. AZ_Printf("AWSMetrics", "Total number of metrics events which failed the JSON schema validation or reached the maximum number of retries : %u", stats.m_numDropped.load());
  127. }
  128. void AWSMetricsSystemComponent::EnableOfflineRecording(const AZ::ConsoleCommandContainer& arguments)
  129. {
  130. if (arguments.size() == 0 || arguments.size() > 2)
  131. {
  132. AZ_Error("AWSMetrics", false, "Invalid number of console command arguments. Please provide a boolean value to enable/disable the feature. "
  133. "To submit metrics recorded in the local file to the backend and delete the file, set the first argument to false and use \"submit\" as the second argument."
  134. "For example, AWSMetricsSystemComponent.EnableOfflineRecording false submit");
  135. return;
  136. }
  137. if (arguments[0] != "true" && arguments[0] != "false")
  138. {
  139. AZ_Error("AWSMetrics", false, "The first argument needs to be either true or false.");
  140. return;
  141. }
  142. if (arguments.size() == 2 && arguments[1] != "submit")
  143. {
  144. AZ_Error("AWSMetrics", false, "The second argument needs to be \"submit\" if exists.");
  145. return;
  146. }
  147. bool enable = arguments[0] == "true" ? true : false;
  148. bool submitLocalMetrics = arguments.size() == 2;
  149. m_metricsManager->UpdateOfflineRecordingStatus(enable, submitLocalMetrics);
  150. }
  151. bool AWSMetricsSystemComponent::SubmitMetrics(const AZStd::vector<MetricsAttribute>& metricsAttributes, int eventPriority,
  152. const AZStd::string& metricSource, bool bufferMetrics)
  153. {
  154. if (bufferMetrics)
  155. {
  156. return m_metricsManager->SubmitMetrics(metricsAttributes, eventPriority, metricSource);
  157. }
  158. return m_metricsManager->SendMetricsAsync(metricsAttributes, eventPriority, metricSource);
  159. }
  160. void AWSMetricsSystemComponent::FlushMetrics()
  161. {
  162. m_metricsManager->FlushMetricsAsync();
  163. }
  164. void AWSMetricsSystemComponent::Init()
  165. {
  166. m_metricsManager->Init();
  167. }
  168. void AWSMetricsSystemComponent::Activate()
  169. {
  170. AWSMetricsRequestBus::Handler::BusConnect();
  171. m_metricsManager->StartMetrics();
  172. }
  173. void AWSMetricsSystemComponent::Deactivate()
  174. {
  175. m_metricsManager->ShutdownMetrics();
  176. AWSMetricsRequestBus::Handler::BusDisconnect();
  177. }
  178. void AWSMetricsSystemComponent::AttributeSubmissionList::Reflect(AZ::ReflectContext* reflection)
  179. {
  180. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(reflection))
  181. {
  182. serialize->Class<AttributeSubmissionList>()
  183. ->Version(1)
  184. ;
  185. }
  186. if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(reflection))
  187. {
  188. behaviorContext->Class<AttributeSubmissionList>("AWSMetrics_AttributesSubmissionList")
  189. ->Attribute(AZ::Script::Attributes::Category, "AWSMetrics")
  190. ->Attribute(AZ::Script::Attributes::Storage, AZ::Script::Attributes::StorageType::Value)
  191. ->Property("attributes", BehaviorValueProperty(&AttributeSubmissionList::attributes))
  192. ;
  193. }
  194. }
  195. } // namespace AWSMetrics