simpleapi.h 4.6 KB

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