MetricsEventBuilder.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <AWSMetricsConstant.h>
  9. #include <MetricsEventBuilder.h>
  10. #include <MetricsEvent.h>
  11. #include <AzCore/Math/Uuid.h>
  12. #include <ctime>
  13. namespace AWSMetrics
  14. {
  15. MetricsEventBuilder::MetricsEventBuilder()
  16. : m_currentMetricsEvent(MetricsEvent())
  17. {
  18. AddTimestampAttribute();
  19. }
  20. MetricsEventBuilder& MetricsEventBuilder::AddDefaultMetricsAttributes(const AZStd::string& clientId, const AZStd::string& metricSourceOverride)
  21. {
  22. AddClientIdAttribute(clientId);
  23. AddEventIdAttribute();
  24. AddSourceAttribute(metricSourceOverride);
  25. return *this;
  26. }
  27. void MetricsEventBuilder::AddClientIdAttribute(const AZStd::string& clientId)
  28. {
  29. m_currentMetricsEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyClientId, clientId));
  30. }
  31. void MetricsEventBuilder::AddEventIdAttribute()
  32. {
  33. AZ::Uuid eventId = AZ::Uuid::Create();
  34. m_currentMetricsEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventId, eventId.ToString<AZStd::string>()));
  35. }
  36. void MetricsEventBuilder::AddSourceAttribute(const AZStd::string& eventSourceOverride)
  37. {
  38. AZStd::string eventSource = eventSourceOverride.empty() ? DefaultMetricsSource : eventSourceOverride;
  39. m_currentMetricsEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventSource, eventSource));
  40. }
  41. void MetricsEventBuilder::AddTimestampAttribute()
  42. {
  43. // Timestamp format is using the UTC ISO8601 format
  44. time_t now;
  45. time(&now);
  46. char buffer[50];
  47. tm time;
  48. #if AZ_TRAIT_USE_SECURE_CRT_FUNCTIONS
  49. gmtime_s(&time, &now);
  50. #else
  51. time = *gmtime(&now);
  52. #endif
  53. strftime(buffer, sizeof(buffer), "%FT%TZ", &time);
  54. m_currentMetricsEvent.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventTimestamp, AZStd::string(buffer)));
  55. }
  56. MetricsEventBuilder& MetricsEventBuilder::AddMetricsAttributes(const AZStd::vector<MetricsAttribute>& attributes)
  57. {
  58. m_currentMetricsEvent.AddAttributes(attributes);
  59. return *this;
  60. }
  61. MetricsEventBuilder& MetricsEventBuilder::SetMetricsPriority(int priority)
  62. {
  63. m_currentMetricsEvent.SetEventPriority(priority);
  64. return *this;
  65. }
  66. MetricsEvent MetricsEventBuilder::Build()
  67. {
  68. MetricsEvent result = AZStd::move(m_currentMetricsEvent);
  69. // Set m_currentMetricsEvent to a new metrics event instance after the first call to this function.
  70. m_currentMetricsEvent = MetricsEvent();
  71. return AZStd::move(result);
  72. }
  73. }