save_to_file_example.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <prometheus/registry.h>
  2. #include <prometheus/counter.h>
  3. #include <prometheus/save_to_file.h>
  4. #include <array>
  5. #include <chrono>
  6. #include <cstdlib>
  7. #include <memory>
  8. #include <string>
  9. #include <thread>
  10. #include <iostream>
  11. int main() {
  12. using namespace prometheus;
  13. // for clarity, we deduce the required types
  14. using Metric = Counter<uint64_t>;
  15. using Family = Metric::Family;
  16. // create a metrics registry
  17. // @note it's the users responsibility to keep the object alive
  18. std::shared_ptr<Registry> registry_ptr = std::make_shared<Registry>();
  19. SaveToFile saver( registry_ptr, std::chrono::seconds(5), "./metrics.prom" );
  20. // add a new counter family to the registry (families combine values with the
  21. // same name, but distinct label dimensions)
  22. //
  23. // @note please follow the metric-naming best-practices:
  24. // https://prometheus.io/docs/practices/naming/
  25. Family& family { Family::Build(*registry_ptr, "our_metric", "some metric") };
  26. // add and remember dimensional data, incrementing those is very cheap
  27. Metric& metric { family.Add({}) };
  28. for (;; ) {
  29. std::this_thread::sleep_for(std::chrono::seconds(1));
  30. const int random_value = std::rand();
  31. metric += random_value % 10;
  32. }
  33. }