simpleapi.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. namespace prometheus {
  15. namespace simpleapi {
  16. extern Registry& registry;
  17. extern SaveToFile saver;
  18. template <typename CustomWrapper>
  19. class family_wrapper_t {
  20. typename CustomWrapper::Family* family_{ nullptr };
  21. public:
  22. // make new family: family_t family {"family_name", "Family description"}
  23. family_wrapper_t(const std::string& name, const std::string& description)
  24. : family_(&CustomWrapper::Family::Build(registry, name, description)) {}
  25. // make new metric into existing family: metric_t metric {family.Add({{"tag_name", "tag_value"}})}
  26. CustomWrapper Add(const typename CustomWrapper::Family::Labels& labels) {
  27. return CustomWrapper(family_, family_->Add(labels));
  28. }
  29. };
  30. class counter_metric_t {
  31. public:
  32. using Metric = Counter<uint64_t>;
  33. using Family = Metric::Family;
  34. private:
  35. Family* family_ { nullptr };
  36. Metric* metric_ { nullptr };
  37. friend family_wrapper_t<counter_metric_t>;
  38. counter_metric_t(typename Metric::Family* family, Metric& metric)
  39. : family_(family), metric_(&metric) {}
  40. public:
  41. // fake empty metric
  42. counter_metric_t() = default;
  43. // make new counter as simple metric without tags and with hidden family included: metric_t metric {"counter_name", "Counter description"}
  44. counter_metric_t(const std::string& name, const std::string& description)
  45. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  46. void operator++ () { metric_->Increment(); }
  47. void operator++ (int) { metric_->Increment(); }
  48. void operator+= (typename Metric::Value val) { metric_->Increment(val); }
  49. uint64_t value() const { return metric_->Get(); }
  50. };
  51. using counter_family_t = family_wrapper_t<counter_metric_t>;
  52. class gauge_metric_t {
  53. public:
  54. using Metric = Gauge<int64_t>;
  55. using Family = Metric::Family;
  56. private:
  57. Family* family_ { nullptr };
  58. Metric* metric_ { nullptr };
  59. friend family_wrapper_t<gauge_metric_t>;
  60. gauge_metric_t(typename Metric::Family* family, Metric& metric)
  61. : family_(family), metric_(&metric) {}
  62. public:
  63. // fake empty metric
  64. gauge_metric_t() = default;
  65. // make new gauge as simple metric without tags and with hidden family included: metric {"counter_name", "Counter description"}
  66. gauge_metric_t(const std::string& name, const std::string& description)
  67. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  68. void operator++ () { metric_->Increment(); }
  69. void operator++ (int) { metric_->Increment(); }
  70. void operator+= (typename Metric::Value val) { metric_->Increment(val); }
  71. void operator-- () { metric_->Decrement(); }
  72. void operator-- (int) { metric_->Decrement(); }
  73. void operator-= (typename Metric::Value val) { metric_->Decrement(val); }
  74. void operator= (typename Metric::Value val) { metric_->Set(val); }
  75. int64_t value() const { return metric_->Get(); }
  76. };
  77. using gauge_family_t = family_wrapper_t<gauge_metric_t>;
  78. class benchmark_metric_t {
  79. public:
  80. using Metric = Benchmark;
  81. using Family = Metric::Family;
  82. private:
  83. Family* family_ { nullptr };
  84. Metric* metric_ { nullptr };
  85. friend family_wrapper_t<benchmark_metric_t>;
  86. benchmark_metric_t(typename Metric::Family* family, Metric& metric)
  87. : family_(family), metric_(&metric) {}
  88. public:
  89. // fake empty metric
  90. benchmark_metric_t() = default;
  91. // make new benchmark as simple metric without tags and with hidden family included: metric {"counter_name", "Counter description"}
  92. benchmark_metric_t(const std::string& name, const std::string& description)
  93. : family_(&Metric::Family::Build(registry, name, description)), metric_(&family_->Add({})) {}
  94. void start() { metric_->start(); }
  95. void stop() { metric_->stop(); }
  96. double value() const { return metric_->Get(); }
  97. };
  98. using benchmark_family_t = family_wrapper_t<benchmark_metric_t>;
  99. }
  100. }