3
0

MetricsManagerTest.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 <AWSMetricsBus.h>
  9. #include <AWSMetricsGemMock.h>
  10. #include <AWSMetricsConstant.h>
  11. #include <ClientConfiguration.h>
  12. #include <MetricsEvent.h>
  13. #include <MetricsManager.h>
  14. #include <AzCore/Settings/SettingsRegistry.h>
  15. #include <AzCore/std/string/conversions.h>
  16. #include <AzFramework/StringFunc/StringFunc.h>
  17. #include <AzCore/std/smart_ptr/unique_ptr.h>
  18. namespace AZ
  19. {
  20. namespace IO
  21. {
  22. class fileIOMock : public LocalFileIO
  23. {
  24. public:
  25. AZ_RTTI(fileIOMock, "{9F23EB93-917B-401F-AC91-63D85BADB102}", FileIOBase);
  26. AZ_CLASS_ALLOCATOR(fileIOMock, OSAllocator);
  27. fileIOMock() = default;
  28. ~fileIOMock() = default;
  29. Result Open(const char* filePath, OpenMode mode, HandleType& fileHandle) override
  30. {
  31. AZ_UNUSED(filePath);
  32. AZ_UNUSED(mode);
  33. AZ_UNUSED(fileHandle);
  34. return Result(ResultCode::Success);
  35. }
  36. Result Close(HandleType fileHandle) override
  37. {
  38. AZ_UNUSED(fileHandle);
  39. return Result(ResultCode::Success);
  40. }
  41. Result Read(
  42. HandleType fileHandle, void* buffer, AZ::u64 size, bool failOnFewerThanSizeBytesRead = false,
  43. AZ::u64* bytesRead = nullptr) override
  44. {
  45. AZ_UNUSED(fileHandle);
  46. AZ_UNUSED(buffer);
  47. AZ_UNUSED(size);
  48. AZ_UNUSED(failOnFewerThanSizeBytesRead);
  49. AZ_UNUSED(bytesRead);
  50. return Result(ResultCode::Success);
  51. }
  52. Result Write(HandleType fileHandle, const void* buffer, AZ::u64 size, AZ::u64* bytesWritten = nullptr) override
  53. {
  54. AZ_UNUSED(fileHandle);
  55. AZ_UNUSED(buffer);
  56. AZ_UNUSED(size);
  57. AZ_UNUSED(bytesWritten);
  58. return Result(ResultCode::Success);
  59. }
  60. };
  61. } // namespace IO
  62. }; // namespace AZ
  63. namespace AWSMetrics
  64. {
  65. class MetricsManagerMock
  66. : public MetricsManager
  67. {
  68. private:
  69. AZ::Outcome<void, AZStd::string> SendMetricsToFile(AZStd::shared_ptr<MetricsQueue> metricsQueue) override
  70. {
  71. if (AZ::IO::FileIOBase::GetInstance())
  72. {
  73. return AZ::Success();
  74. }
  75. else
  76. {
  77. return AZ::Failure(AZStd::string{ "Invalid File IO" });
  78. }
  79. }
  80. };
  81. class AWSMetricsNotificationBusMock
  82. : protected AWSMetricsNotificationBus::Handler
  83. {
  84. public:
  85. AZ_CLASS_ALLOCATOR(AWSMetricsNotificationBusMock, AZ::SystemAllocator);
  86. AWSMetricsNotificationBusMock()
  87. : m_numSuccessNotification(0)
  88. , m_numFailureNotification(0)
  89. {
  90. AWSMetricsNotificationBus::Handler::BusConnect();
  91. }
  92. ~AWSMetricsNotificationBusMock()
  93. {
  94. AWSMetricsNotificationBus::Handler::BusDisconnect();
  95. }
  96. void OnSendMetricsSuccess(int requestId) override
  97. {
  98. AZ_UNUSED(requestId);
  99. ++m_numSuccessNotification;
  100. }
  101. void OnSendMetricsFailure(int requestId, const AZStd::string& errorMessage) override
  102. {
  103. AZ_UNUSED(requestId);
  104. AZ_UNUSED(errorMessage);
  105. ++m_numFailureNotification;
  106. }
  107. int m_numSuccessNotification;
  108. int m_numFailureNotification;
  109. };
  110. class MetricsManagerTest
  111. : public AWSMetricsGemAllocatorFixture
  112. , protected AWSMetricsRequestBus::Handler
  113. {
  114. public:
  115. // We calculate the event size by adding the size of all the key value pairs in MetricsEvent.m_attributes
  116. // TestMetricsEventSizeInBytes value is based on a simple event with default attributes + one test attribute
  117. // See MetricEvents::GetSizeInBytes and MetricsManager::SubmitMetrics for details
  118. static constexpr int TestMetricsEventSizeInBytes = 178;
  119. static constexpr int MbToBytes = 1000000;
  120. static constexpr int DefaultFlushPeriodInSeconds = 1;
  121. static constexpr int MaxNumMetricsEvents = 10;
  122. static constexpr int SleepTimeForProcessingInMs = 100;
  123. // Timeout for metrics events processing in milliseconds.
  124. static constexpr int TimeoutForProcessingInMs = DefaultFlushPeriodInSeconds * MaxNumMetricsEvents * 1000;
  125. static const char* const AttrValue;
  126. void SetUp() override
  127. {
  128. AWSMetricsGemAllocatorFixture::SetUp();
  129. AWSMetricsRequestBus::Handler::BusConnect();
  130. m_metricsManager = AZStd::make_unique<MetricsManagerMock>();
  131. AZStd::string configFilePath = CreateClientConfigFile(true, (double) TestMetricsEventSizeInBytes / MbToBytes * 2, DefaultFlushPeriodInSeconds, 0);
  132. m_settingsRegistry->MergeSettingsFile(configFilePath, AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  133. m_metricsManager->Init();
  134. ReplaceLocalFileIOWithMockIO();
  135. }
  136. void TearDown() override
  137. {
  138. RevertMockIOToLocalFileIO();
  139. RemoveFile(GetDefaultTestFilePath());
  140. m_metricsManager.reset();
  141. AWSMetricsRequestBus::Handler::BusDisconnect();
  142. AWSMetricsGemAllocatorFixture::TearDown();
  143. }
  144. void ResetClientConfig(bool offlineRecordingEnabled, double maxQueueSizeInMb, int queueFlushPeriodInSeconds, int MaxNumRetries)
  145. {
  146. RevertMockIOToLocalFileIO();
  147. AZStd::string configFilePath = CreateClientConfigFile(offlineRecordingEnabled, maxQueueSizeInMb, queueFlushPeriodInSeconds, MaxNumRetries);
  148. m_settingsRegistry->MergeSettingsFile(configFilePath, AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  149. m_metricsManager->Init();
  150. ReplaceLocalFileIOWithMockIO();
  151. }
  152. void ReplaceLocalFileIOWithMockIO()
  153. {
  154. m_fileIOMock = aznew AZ::IO::fileIOMock();
  155. AZ::IO::FileIOBase::SetInstance(nullptr);
  156. AZ::IO::FileIOBase::SetInstance(m_fileIOMock);
  157. }
  158. void RevertMockIOToLocalFileIO()
  159. {
  160. AZ::IO::FileIOBase::SetInstance(nullptr);
  161. delete m_fileIOMock;
  162. AZ::IO::FileIOBase::SetInstance(m_localFileIO);
  163. }
  164. // AWSMetricsRequestBus interface implementation
  165. bool SubmitMetrics(const AZStd::vector<MetricsAttribute>& metricsAttributes, int eventPriority = 0,
  166. const AZStd::string& eventSourceOverride = "", bool bufferMetrics = true) override
  167. {
  168. if (bufferMetrics)
  169. {
  170. return m_metricsManager->SubmitMetrics(metricsAttributes, eventPriority, eventSourceOverride);
  171. }
  172. else
  173. {
  174. return m_metricsManager->SendMetricsAsync(metricsAttributes, eventPriority, eventSourceOverride);
  175. }
  176. };
  177. //! Flush all metrics buffered in memory.
  178. virtual void FlushMetrics() override
  179. {
  180. m_metricsManager->FlushMetricsAsync();
  181. };
  182. //! Wait for either timeout or all the expected metrics events are processed.
  183. void WaitForProcessing(int expectedNumProcessedEvents)
  184. {
  185. const GlobalStatistics& originalStats = m_metricsManager->GetGlobalStatistics();
  186. int currentNumProcessedEvents = originalStats.m_numEvents;
  187. int processingTime = 0;
  188. int numTotalRequests = 0;
  189. while (processingTime < TimeoutForProcessingInMs)
  190. {
  191. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(SleepTimeForProcessingInMs));
  192. processingTime += SleepTimeForProcessingInMs;
  193. const GlobalStatistics& currentStats = m_metricsManager->GetGlobalStatistics();
  194. currentNumProcessedEvents = currentStats.m_numEvents;
  195. numTotalRequests = m_metricsManager->GetNumTotalRequests();
  196. if (currentNumProcessedEvents == expectedNumProcessedEvents)
  197. {
  198. // All the expectd metrics events has been sent. Flush the tick bus queue until we get all the notifications.
  199. AZ::TickBus::ExecuteQueuedEvents();
  200. if (numTotalRequests ==
  201. m_notifications.m_numSuccessNotification + m_notifications.m_numFailureNotification)
  202. {
  203. break;
  204. }
  205. }
  206. }
  207. }
  208. AZStd::unique_ptr<MetricsManagerMock> m_metricsManager;
  209. AWSMetricsNotificationBusMock m_notifications;
  210. AZ::IO::FileIOBase* m_fileIOMock;
  211. };
  212. const char* const MetricsManagerTest::AttrValue = "value";
  213. TEST_F(MetricsManagerTest, SubmitMetrics_MaxFlushPeriod_SendToLocalFile)
  214. {
  215. m_metricsManager->StartMetrics();
  216. AZStd::vector<MetricsAttribute> metricsAttributes;
  217. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  218. bool result = false;
  219. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", true);
  220. ASSERT_TRUE(result);
  221. WaitForProcessing(1);
  222. ASSERT_EQ(m_notifications.m_numSuccessNotification, 1);
  223. ASSERT_EQ(m_notifications.m_numFailureNotification, 0);
  224. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  225. m_metricsManager->ShutdownMetrics();
  226. }
  227. TEST_F(MetricsManagerTest, SubmitMetrics_MaxQueueSize_SendToLocalFile)
  228. {
  229. // Reset the config file to change the max queue size and set a flush period larger than the timeout.
  230. ResetClientConfig(true, 0.0, (TimeoutForProcessingInMs + 1), 0);
  231. m_metricsManager->StartMetrics();
  232. AZStd::vector<MetricsAttribute> metricsAttributes;
  233. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  234. bool result = false;
  235. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", true);
  236. ASSERT_TRUE(result);
  237. WaitForProcessing(1);
  238. ASSERT_EQ(m_notifications.m_numSuccessNotification, 1);
  239. ASSERT_EQ(m_notifications.m_numFailureNotification, 0);
  240. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  241. m_metricsManager->ShutdownMetrics();
  242. }
  243. TEST_F(MetricsManagerTest, SubmitMetricsFromMultipleThreads_WithAndWithoutBuffer_SendToLocalFile)
  244. {
  245. m_metricsManager->StartMetrics();
  246. AZStd::vector<AZStd::thread> producers;
  247. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  248. {
  249. producers.emplace_back(AZStd::thread([index]()
  250. {
  251. AZStd::vector<MetricsAttribute> metricsAttributes;
  252. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  253. bool result = false;
  254. // Submit metrics with or without buffer
  255. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", index % 2 == 0);
  256. ASSERT_TRUE(result);
  257. }));
  258. }
  259. for (AZStd::thread& producer : producers) {
  260. producer.join();
  261. }
  262. // Flush the metrics queue to send all the remaining buffered metrics
  263. AWSMetricsRequestBus::Broadcast(&AWSMetricsRequests::FlushMetrics);
  264. WaitForProcessing(MaxNumMetricsEvents);
  265. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  266. EXPECT_EQ(stats.m_numEvents, MaxNumMetricsEvents);
  267. EXPECT_EQ(stats.m_numSuccesses, MaxNumMetricsEvents);
  268. EXPECT_EQ(stats.m_numErrors, 0);
  269. EXPECT_EQ(stats.m_sendSizeInBytes, TestMetricsEventSizeInBytes * MaxNumMetricsEvents);
  270. m_metricsManager->ShutdownMetrics();
  271. }
  272. TEST_F(MetricsManagerTest, SubmitMetrics_NoMetircsAttributes_Fail)
  273. {
  274. bool result = false;
  275. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, AZStd::vector<MetricsAttribute>(), 0, "", true);
  276. ASSERT_FALSE(result);
  277. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  278. }
  279. TEST_F(MetricsManagerTest, SendMetricsAsync_NoMetircsAttributes_Fail)
  280. {
  281. bool result = false;
  282. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, AZStd::vector<MetricsAttribute>(), 0, "", false);
  283. ASSERT_FALSE(result);
  284. }
  285. TEST_F(MetricsManagerTest, SendMetricsAsync_InvalidFileIO_Fail)
  286. {
  287. AZ::IO::FileIOBase::SetInstance(nullptr);
  288. AZStd::vector<MetricsAttribute> metricsAttributes;
  289. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  290. bool result = false;
  291. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", false);
  292. ASSERT_TRUE(result);
  293. WaitForProcessing(1);
  294. ASSERT_EQ(m_notifications.m_numSuccessNotification, 0);
  295. ASSERT_EQ(m_notifications.m_numFailureNotification, 1);
  296. }
  297. TEST_F(MetricsManagerTest, FlushMetrics_NonEmptyQueue_Success)
  298. {
  299. ResetClientConfig(true, (double)TestMetricsEventSizeInBytes * (MaxNumMetricsEvents + 1) / MbToBytes,
  300. DefaultFlushPeriodInSeconds, 1);
  301. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  302. {
  303. AZStd::vector<MetricsAttribute> metricsAttributes;
  304. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  305. bool result = false;
  306. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", true);
  307. ASSERT_TRUE(result);
  308. }
  309. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), MaxNumMetricsEvents);
  310. AWSMetricsRequestBus::Broadcast(&AWSMetricsRequests::FlushMetrics);
  311. WaitForProcessing(MaxNumMetricsEvents);
  312. ASSERT_EQ(m_notifications.m_numSuccessNotification, 1);
  313. ASSERT_EQ(m_notifications.m_numFailureNotification, 0);
  314. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  315. }
  316. TEST_F(MetricsManagerTest, ResetOfflineRecordingStatus_ResubmitLocalMetrics_Success)
  317. {
  318. // Disable offline recording in the config file.
  319. ResetClientConfig(false, (double)TestMetricsEventSizeInBytes * 2 / MbToBytes, 0, 0);
  320. // Enable offline recording after initialize the metric manager.
  321. m_metricsManager->UpdateOfflineRecordingStatus(true);
  322. AZStd::vector<MetricsAttribute> metricsAttributes;
  323. metricsAttributes.emplace_back(AZStd::move(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue)));
  324. bool result = false;
  325. AWSMetricsRequestBus::BroadcastResult(result, &AWSMetricsRequests::SubmitMetrics, metricsAttributes, 0, "", false);
  326. ASSERT_TRUE(result);
  327. WaitForProcessing(1);
  328. ASSERT_EQ(m_notifications.m_numSuccessNotification, 1);
  329. ASSERT_EQ(m_notifications.m_numFailureNotification, 0);
  330. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  331. RevertMockIOToLocalFileIO();
  332. AZStd::string localmetrics = "[{\"event_name\": \"test\"}]";
  333. ASSERT_TRUE(m_localFileIO->Exists(m_metricsManager->GetMetricsFileDirectory()) || m_localFileIO->CreatePath(m_metricsManager->GetMetricsFileDirectory()));
  334. ASSERT_TRUE(CreateFile(m_metricsManager->GetMetricsFilePath(), localmetrics));
  335. // Disable offline recording and resubmitted metrics stored in the local file.
  336. m_metricsManager->UpdateOfflineRecordingStatus(false, true);
  337. //! Wait for either timeout or the local metrics events are re-added to the buffer.
  338. int processingTime = 0;
  339. while (processingTime < TimeoutForProcessingInMs && m_localFileIO->Exists(m_metricsManager->GetMetricsFilePath()))
  340. {
  341. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(SleepTimeForProcessingInMs));
  342. processingTime += SleepTimeForProcessingInMs;
  343. }
  344. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 1);
  345. ASSERT_FALSE(m_localFileIO->Exists(m_metricsManager->GetMetricsFilePath()));
  346. ReplaceLocalFileIOWithMockIO();
  347. }
  348. TEST_F(MetricsManagerTest, OnResponseReceived_WithResponseEntries_RetryFailedMetrics)
  349. {
  350. // Reset the config file to change the max queue size setting.
  351. ResetClientConfig(false, (double)TestMetricsEventSizeInBytes * (MaxNumMetricsEvents + 1) / MbToBytes,
  352. DefaultFlushPeriodInSeconds, 1);
  353. MetricsQueue metricsEvents;
  354. ServiceAPI::PostMetricsEventsResponseEntries responseEntries;
  355. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  356. {
  357. MetricsEvent newEvent;
  358. newEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
  359. metricsEvents.AddMetrics(newEvent);
  360. ServiceAPI::PostMetricsEventsResponseEntry responseEntry;
  361. if (index % 2 == 0)
  362. {
  363. responseEntry.m_errorCode = "Error";
  364. }
  365. else
  366. {
  367. responseEntry.m_result = "Ok";
  368. }
  369. responseEntries.emplace_back(responseEntry);
  370. }
  371. m_metricsManager->OnResponseReceived(metricsEvents, responseEntries);
  372. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  373. EXPECT_EQ(stats.m_numEvents, MaxNumMetricsEvents);
  374. EXPECT_EQ(stats.m_numSuccesses, MaxNumMetricsEvents / 2);
  375. EXPECT_EQ(stats.m_numErrors, MaxNumMetricsEvents / 2);
  376. EXPECT_EQ(stats.m_numDropped, 0);
  377. int metricsEventSize = static_cast<int>(sizeof(AwsMetricsAttributeKeyEventName) - 1 + strlen(AttrValue));
  378. EXPECT_EQ(stats.m_sendSizeInBytes, metricsEventSize * MaxNumMetricsEvents / 2);
  379. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), MaxNumMetricsEvents / 2);
  380. }
  381. TEST_F(MetricsManagerTest, OnResponseReceived_NoResponseEntries_RetryAllMetrics)
  382. {
  383. // Reset the config file to change the max queue size setting.
  384. ResetClientConfig(false, (double)TestMetricsEventSizeInBytes * (MaxNumMetricsEvents + 1) / MbToBytes,
  385. DefaultFlushPeriodInSeconds, 1);
  386. MetricsQueue metricsEvents;
  387. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  388. {
  389. MetricsEvent newEvent;
  390. newEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
  391. metricsEvents.AddMetrics(newEvent);
  392. }
  393. m_metricsManager->OnResponseReceived(metricsEvents);
  394. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  395. EXPECT_EQ(stats.m_numEvents, MaxNumMetricsEvents);
  396. EXPECT_EQ(stats.m_numSuccesses, 0);
  397. EXPECT_EQ(stats.m_sendSizeInBytes, 0);
  398. EXPECT_EQ(stats.m_numDropped, 0);
  399. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), MaxNumMetricsEvents);
  400. }
  401. TEST_F(MetricsManagerTest, OnResponseReceived_MaxNumRetires_DropMetrics)
  402. {
  403. // Reset the config file to change the max queue size setting.
  404. ResetClientConfig(false, (double)TestMetricsEventSizeInBytes * (MaxNumMetricsEvents + 1) / MbToBytes,
  405. DefaultFlushPeriodInSeconds, 1);
  406. MetricsQueue metricsEvents;
  407. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  408. {
  409. MetricsEvent newMetricsEvent;
  410. // Number of failures exceeds the maximum number of retries setting.
  411. newMetricsEvent.MarkFailedSubmission();
  412. newMetricsEvent.MarkFailedSubmission();
  413. metricsEvents.AddMetrics(newMetricsEvent);
  414. }
  415. m_metricsManager->OnResponseReceived(metricsEvents);
  416. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  417. EXPECT_EQ(stats.m_numEvents, 0);
  418. EXPECT_EQ(stats.m_numSuccesses, 0);
  419. // Number of errors is expected to be 0 here since the test metrics events
  420. // have been retried for multiple times and we do not increase the total number of errors in this case.
  421. EXPECT_EQ(stats.m_numErrors, 0);
  422. EXPECT_EQ(stats.m_sendSizeInBytes, 0);
  423. EXPECT_EQ(stats.m_numDropped, MaxNumMetricsEvents);
  424. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  425. }
  426. TEST_F(MetricsManagerTest, PushMetricsForRetries_NoRetry_DropMetrics)
  427. {
  428. // Reset the config file to change the max queue size setting.
  429. ResetClientConfig(false, (double)TestMetricsEventSizeInBytes * (MaxNumMetricsEvents + 1) / MbToBytes,
  430. DefaultFlushPeriodInSeconds, 0);
  431. MetricsQueue metricsEvents;
  432. for (int index = 0; index < MaxNumMetricsEvents; ++index)
  433. {
  434. metricsEvents.AddMetrics(MetricsEvent());
  435. }
  436. m_metricsManager->OnResponseReceived(metricsEvents);
  437. const GlobalStatistics& stats = m_metricsManager->GetGlobalStatistics();
  438. EXPECT_EQ(stats.m_numEvents, MaxNumMetricsEvents);
  439. EXPECT_EQ(stats.m_numSuccesses, 0);
  440. EXPECT_EQ(stats.m_numErrors, MaxNumMetricsEvents);
  441. EXPECT_EQ(stats.m_sendSizeInBytes, 0);
  442. EXPECT_EQ(stats.m_numDropped, MaxNumMetricsEvents);
  443. ASSERT_EQ(m_metricsManager->GetNumBufferedMetrics(), 0);
  444. }
  445. class ClientConfigurationTest
  446. : public AWSMetricsGemAllocatorFixture
  447. {
  448. public:
  449. const double DEFAULT_MAX_QUEUE_SIZE_IN_MB = 0.0004;
  450. const int DefaultFlushPeriodInSeconds = 1;
  451. const int DEFAULT_MAX_NUM_RETRIES = 1;
  452. void SetUp() override
  453. {
  454. AWSMetricsGemAllocatorFixture::SetUp();
  455. m_clientConfiguration = AZStd::make_unique<ClientConfiguration>();
  456. }
  457. AZStd::unique_ptr<ClientConfiguration> m_clientConfiguration;
  458. };
  459. TEST_F(ClientConfigurationTest, ResetClientConfiguration_ValidClientConfiguration_Success)
  460. {
  461. AZStd::string configFilePath = CreateClientConfigFile(true, DEFAULT_MAX_QUEUE_SIZE_IN_MB, DefaultFlushPeriodInSeconds, DEFAULT_MAX_NUM_RETRIES);
  462. m_settingsRegistry->MergeSettingsFile(configFilePath, AZ::SettingsRegistryInterface::Format::JsonMergePatch, {});
  463. ASSERT_TRUE(m_clientConfiguration->InitClientConfiguration());
  464. ASSERT_TRUE(m_clientConfiguration->OfflineRecordingEnabled());
  465. ASSERT_EQ(m_clientConfiguration->GetMaxQueueSizeInBytes(), DEFAULT_MAX_QUEUE_SIZE_IN_MB * 1000000);
  466. ASSERT_EQ(m_clientConfiguration->GetQueueFlushPeriodInSeconds(), DefaultFlushPeriodInSeconds);
  467. ASSERT_EQ(m_clientConfiguration->GetMaxNumRetries(), DEFAULT_MAX_NUM_RETRIES);
  468. char resolvedPath[AZ_MAX_PATH_LEN] = { 0 };
  469. ASSERT_TRUE(m_localFileIO->ResolvePath(AwsMetricsLocalFileDir, resolvedPath, AZ_MAX_PATH_LEN));
  470. AZStd::string expectedMetricsFilePath;
  471. ASSERT_TRUE(AzFramework::StringFunc::Path::Join(resolvedPath, AwsMetricsLocalFileName, expectedMetricsFilePath));
  472. ASSERT_EQ(strcmp(m_clientConfiguration->GetMetricsFileDir(), resolvedPath), 0);
  473. ASSERT_EQ(m_clientConfiguration->GetMetricsFileFullPath(), expectedMetricsFilePath);
  474. }
  475. }