simpleapi_use_in_class_example.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <prometheus/simpleapi.h>
  2. // use prometheus namespace
  3. using namespace prometheus::simpleapi;
  4. class MyClass {
  5. counter_family_t metric_family { "simple_family", "simple family example" };
  6. counter_metric_t metric1 { metric_family.Add({{"name", "counter1"}}) };
  7. counter_metric_t metric2 { metric_family.Add({{"name", "counter2"}}) };
  8. counter_metric_t metric3 { "simple_counter_1", "simple counter 1 without labels example" };
  9. counter_metric_t metric4 { "simple_counter_2", "simple counter 2 without labels example" };
  10. benchmark_family_t benchmark_family { "simple_benchmark_family", "simple benchmark family example" };
  11. benchmark_metric_t benchmark1 { benchmark_family.Add({{"benchmark", "1"}}) };
  12. benchmark_metric_t benchmark2 { benchmark_family.Add({{"benchmark", "2"}}) };
  13. public:
  14. MyClass() = default;
  15. void member_to_do_something() {
  16. benchmark1.start();
  17. const int random_value = std::rand();
  18. benchmark1.stop();
  19. benchmark2.start();
  20. if (random_value & 1) metric1++;
  21. if (random_value & 2) metric2++;
  22. if (random_value & 4) metric3++;
  23. if (random_value & 8) metric4++;
  24. benchmark2.stop();
  25. }
  26. };
  27. int main() {
  28. MyClass myClass;
  29. benchmark_metric_t benchmark { "simple_benchmark", "simple benchmark example" };
  30. for (;; ) {
  31. benchmark.start();
  32. std::this_thread::sleep_for(std::chrono::seconds(1));
  33. benchmark.stop();
  34. myClass.member_to_do_something();
  35. }
  36. }