MetricsEvent.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 <MetricsAttribute.h>
  10. #include <AzCore/std/containers/vector.h>
  11. namespace AWSMetrics
  12. {
  13. static constexpr const char* DefaultMetricsSource = "AWSMetricGem";
  14. //! Metrics event is used to represent one event which contains a collection of metrics attributes.
  15. class MetricsEvent
  16. {
  17. public:
  18. MetricsEvent() = default;
  19. virtual ~MetricsEvent() = default;
  20. //! Add a new attribute to the metrics.
  21. //! @param attribute Attribute to add.
  22. void AddAttribute(const MetricsAttribute& attribute);
  23. //! Add attributes to the metrics event.
  24. //! @param attributes List of attributes to append.
  25. void AddAttributes(const AZStd::vector<MetricsAttribute>& attributes);
  26. int GetNumAttributes() const;
  27. //! Get the metrics event size serialized to json.
  28. //! @return metrics event size in bytes.
  29. size_t GetSizeInBytes() const;
  30. //! Serialize the metrics event to JSON for the sending requests.
  31. //! @param writer JSON writer for the serialization.
  32. //! @return Whether the metrics event is serialized successfully.
  33. bool SerializeToJson(AWSCore::JsonWriter& writer) const;
  34. //! Read from a JSON value to the metrics event.
  35. //! @param metricsObjVal JSON value to read from.
  36. //! @return Whether the metrics event is created successfully.
  37. bool ReadFromJson(rapidjson::Value& metricsObjVal);
  38. //! Validate the metrics event with the predefined JSON schema.
  39. //! @return whether the metrics event match the JSON schema.
  40. bool ValidateAgainstSchema();
  41. //! Add the count of failures for sending the metrics event.
  42. void MarkFailedSubmission();
  43. //! Get the count of failures for sending the metrics event.
  44. //! @return Count of failures for sending the metrics event.
  45. int GetNumFailures() const;
  46. //! Set the priority of a metrics event.
  47. //! @param priority Priority to set.
  48. void SetEventPriority(int priority);
  49. //! Get the priority of the metrics event.
  50. //! @return Priority of the metrics event.
  51. int GetEventPriority() const;
  52. private:
  53. //! Check whether the attribute exists in the metrics event.
  54. //! @param attributeName Attribute name to check.
  55. //! @return whether the attribute exists.
  56. bool AttributeExists(const AZStd::string& attributeName) const;
  57. AZStd::vector<MetricsAttribute> m_attributes; //!< Attributes included in the metrics.
  58. size_t m_sizeSerializedToJson = 0; //! < Metrics event size serialized to json.
  59. int m_numFailures = 0; //! < Count of failures for sending the metrics event.
  60. int m_eventPriority = 0; //! < Priority of the metrics event.
  61. };
  62. } // namespace AWSMetrics