spack_v1.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include <library/cpp/monlib/encode/encoder.h>
  3. #include <library/cpp/monlib/encode/format.h>
  4. #include <library/cpp/monlib/metrics/metric.h>
  5. #include <util/generic/yexception.h>
  6. //
  7. // format specification available here:
  8. // https://wiki.yandex-team.ru/solomon/api/dataformat/spackv1/
  9. //
  10. class IInputStream;
  11. class IOutputStream;
  12. namespace NMonitoring {
  13. class TSpackDecodeError: public yexception {
  14. };
  15. constexpr auto EncodeMetricType(EMetricType mt) noexcept {
  16. return static_cast<std::underlying_type_t<EMetricType>>(mt);
  17. }
  18. EMetricType DecodeMetricType(ui8 byte);
  19. [[nodiscard]]
  20. bool TryDecodeMetricType(ui8 byte, EMetricType* result);
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // EValueType
  23. ///////////////////////////////////////////////////////////////////////////////
  24. enum class EValueType : ui8 {
  25. NONE = 0x00,
  26. ONE_WITHOUT_TS = 0x01,
  27. ONE_WITH_TS = 0x02,
  28. MANY_WITH_TS = 0x03,
  29. };
  30. constexpr auto EncodeValueType(EValueType vt) noexcept {
  31. return static_cast<std::underlying_type_t<EValueType>>(vt);
  32. }
  33. EValueType DecodeValueType(ui8 byte);
  34. [[nodiscard]]
  35. bool TryDecodeValueType(ui8 byte, EValueType* result);
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // ETimePrecision
  38. ///////////////////////////////////////////////////////////////////////////////
  39. enum class ETimePrecision : ui8 {
  40. SECONDS = 0x00,
  41. MILLIS = 0x01,
  42. };
  43. constexpr auto EncodeTimePrecision(ETimePrecision tp) noexcept {
  44. return static_cast<std::underlying_type_t<ETimePrecision>>(tp);
  45. }
  46. ETimePrecision DecodeTimePrecision(ui8 byte);
  47. [[nodiscard]]
  48. bool TryDecodeTimePrecision(ui8 byte, ETimePrecision* result);
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // ECompression
  51. ///////////////////////////////////////////////////////////////////////////////
  52. ui8 EncodeCompression(ECompression c) noexcept;
  53. ECompression DecodeCompression(ui8 byte);
  54. [[nodiscard]]
  55. bool TryDecodeCompression(ui8 byte, ECompression* result);
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // TSpackHeader
  58. ///////////////////////////////////////////////////////////////////////////////
  59. struct Y_PACKED TSpackHeader {
  60. ui16 Magic = 0x5053; // "SP"
  61. ui16 Version; // MSB - major version, LSB - minor version
  62. ui16 HeaderSize = sizeof(TSpackHeader);
  63. ui8 TimePrecision;
  64. ui8 Compression;
  65. ui32 LabelNamesSize;
  66. ui32 LabelValuesSize;
  67. ui32 MetricCount;
  68. ui32 PointsCount;
  69. // add new fields here
  70. };
  71. enum ESpackV1Version: ui16 {
  72. SV1_00 = 0x0100,
  73. SV1_01 = 0x0101,
  74. SV1_02 = 0x0102
  75. };
  76. IMetricEncoderPtr EncoderSpackV1(
  77. IOutputStream* out,
  78. ETimePrecision timePrecision,
  79. ECompression compression,
  80. EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT
  81. );
  82. IMetricEncoderPtr EncoderSpackV12(
  83. IOutputStream* out,
  84. ETimePrecision timePrecision,
  85. ECompression compression,
  86. EMetricsMergingMode mergingMode = EMetricsMergingMode::DEFAULT,
  87. TStringBuf metricNameLabel = "name"
  88. );
  89. void DecodeSpackV1(IInputStream* in, IMetricConsumer* c, TStringBuf metricNameLabel = "name");
  90. }