2
0

push_to_server_example.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <prometheus/registry.h>
  2. #include <prometheus/counter.h>
  3. #include <prometheus/push_to_server.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. PushToServer pusher(registry_ptr, std::chrono::seconds(5),
  20. std::string("http://127.0.0.1:9091/metrics/job/samples/instance/test") );
  21. // add a new counter family to the registry (families combine values with the
  22. // same name, but distinct label dimensions)
  23. //
  24. // @note please follow the metric-naming best-practices:
  25. // https://prometheus.io/docs/practices/naming/
  26. Family& family { Family::Build(*registry_ptr, "our_metric", "some metric") };
  27. // add and remember dimensional data, incrementing those is very cheap
  28. Metric& metric { family.Add({}) };
  29. for (;; ) {
  30. std::this_thread::sleep_for(std::chrono::seconds(1));
  31. const int random_value = std::rand();
  32. metric += random_value % 10;
  33. }
  34. }