simpleapi.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include <prometheus/family.h>
  3. #include <prometheus/registry.h>
  4. #include <prometheus/counter.h>
  5. #include <prometheus/gauge.h>
  6. #include <prometheus/benchmark.h>
  7. #include <prometheus/registry.h>
  8. #include <prometheus/save_to_file.h>
  9. #include <thread>
  10. #include <iostream>
  11. #include <memory>
  12. #include <functional>
  13. #include <stdexcept>
  14. #include <stdint.h>
  15. #if UINTPTR_MAX == 0xffFFffFF
  16. // 32-bit
  17. typedef uint32_t metric_size;
  18. #elif UINTPTR_MAX == 0xffFFffFFffFFffFF
  19. // 64-bit
  20. typedef uint64_t metric_size;
  21. #else
  22. #error Unknown platform - does not look either like 32-bit or 64-bit
  23. #endif
  24. namespace prometheus {
  25. namespace simpleapi {
  26. extern Registry& registry;
  27. extern SaveToFile saver;
  28. template <typename CustomWrapper>
  29. class family_wrapper_t {
  30. typename CustomWrapper::Family* family_{ nullptr };
  31. public:
  32. // make new family: family_t family {"family_name", "Family description"}
  33. family_wrapper_t(const std::string& name, const std::string& description)
  34. : family_(&CustomWrapper::Family::Build(registry, name, description)) {}
  35. // make new metric into existing family: metric_t metric {family.Add({{"tag_name", "tag_value"}})}
  36. CustomWrapper Add(const typename CustomWrapper::Family::Labels& labels) {
  37. return CustomWrapper(family_, family_->Add(labels));
  38. }
  39. };
  40. class counter_metric_t {
  41. public:
  42. using Metric = Counter<metric_size>;
  43. using Family = Metric::Family;
  44. private:
  45. Family* family_ { nullptr };
  46. Metric* metric_ { nullptr };
  47. friend family_wrapper_t<counter_metric_t>;
  48. counter_metric_t(typename Metric::Family* family, Metric& metric)
  49. : family_(family), metric_(&metric) {}
  50. public:
  51. // fake empty metric
  52. counter_metric_t() = default;
  53. // make new counter as simple metric without tags and with hidden family included: metric_t metric {"counter_name", "Counter description"}
  54. counter_metric_t(const std::string& name, const std::string& description)
  55. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  56. void operator++ () { metric_->Increment(); }
  57. void operator++ (int) { metric_->Increment(); }
  58. void operator+= (typename Metric::Value val) { metric_->Increment(val); }
  59. uint64_t value() const { return metric_->Get(); }
  60. };
  61. using counter_family_t = family_wrapper_t<counter_metric_t>;
  62. class gauge_metric_t {
  63. public:
  64. using Metric = Gauge<metric_size>;
  65. using Family = Metric::Family;
  66. private:
  67. Family* family_ { nullptr };
  68. Metric* metric_ { nullptr };
  69. friend family_wrapper_t<gauge_metric_t>;
  70. gauge_metric_t(typename Metric::Family* family, Metric& metric)
  71. : family_(family), metric_(&metric) {}
  72. public:
  73. // fake empty metric
  74. gauge_metric_t() = default;
  75. // make new gauge as simple metric without tags and with hidden family included: metric {"counter_name", "Counter description"}
  76. gauge_metric_t(const std::string& name, const std::string& description)
  77. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  78. void operator++ () { metric_->Increment(); }
  79. void operator++ (int) { metric_->Increment(); }
  80. void operator+= (typename Metric::Value val) { metric_->Increment(val); }
  81. void operator-- () { metric_->Decrement(); }
  82. void operator-- (int) { metric_->Decrement(); }
  83. void operator-= (typename Metric::Value val) { metric_->Decrement(val); }
  84. void operator= (typename Metric::Value val) { metric_->Set(val); }
  85. int64_t value() const { return metric_->Get(); }
  86. };
  87. using gauge_family_t = family_wrapper_t<gauge_metric_t>;
  88. class benchmark_metric_t {
  89. public:
  90. using Metric = Benchmark;
  91. using Family = Metric::Family;
  92. private:
  93. Family* family_ { nullptr };
  94. Metric* metric_ { nullptr };
  95. friend family_wrapper_t<benchmark_metric_t>;
  96. benchmark_metric_t(typename Metric::Family* family, Metric& metric)
  97. : family_(family), metric_(&metric) {}
  98. public:
  99. // fake empty metric
  100. benchmark_metric_t() = default;
  101. // make new benchmark as simple metric without tags and with hidden family included: metric {"counter_name", "Counter description"}
  102. benchmark_metric_t(const std::string& name, const std::string& description)
  103. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  104. void start() { metric_->start(); }
  105. void stop() { metric_->stop(); }
  106. double value() const { return metric_->Get(); }
  107. };
  108. using benchmark_family_t = family_wrapper_t<benchmark_metric_t>;
  109. }
  110. }