histogram_collector_explicit.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "histogram_collector.h"
  2. #include "atomics_array.h"
  3. #include <util/generic/algorithm.h>
  4. #include <util/generic/vector.h>
  5. #include <util/generic/yexception.h>
  6. #include <util/generic/ylimits.h>
  7. namespace NMonitoring {
  8. ///////////////////////////////////////////////////////////////////////////
  9. // TExplicitHistogramCollector
  10. ///////////////////////////////////////////////////////////////////////////
  11. class TExplicitHistogramCollector: public IHistogramCollector {
  12. public:
  13. TExplicitHistogramCollector(TBucketBounds bounds)
  14. : Values_(bounds.size() + 1)
  15. , Bounds_(std::move(bounds))
  16. {
  17. // add one bucket as +INF
  18. Bounds_.push_back(Max<TBucketBound>());
  19. }
  20. void Collect(double value, ui64 count) override {
  21. auto it = LowerBound(Bounds_.begin(), Bounds_.end(), value);
  22. auto index = std::distance(Bounds_.begin(), it);
  23. Values_.Add(index, count);
  24. }
  25. void Reset() override {
  26. Values_.Reset();
  27. }
  28. IHistogramSnapshotPtr Snapshot() const override {
  29. auto values = Values_.Copy();
  30. return ExplicitHistogramSnapshot(Bounds_, values);
  31. }
  32. private:
  33. TAtomicsArray Values_;
  34. TBucketBounds Bounds_;
  35. };
  36. IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds) {
  37. Y_ENSURE(bounds.size() >= 1,
  38. "explicit histogram must contain at least one bucket");
  39. Y_ENSURE(bounds.size() <= HISTOGRAM_MAX_BUCKETS_COUNT,
  40. "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT
  41. << ", but got: " << bounds.size());
  42. Y_ENSURE(IsSorted(bounds.begin(), bounds.end()),
  43. "bounds for explicit histogram must be sorted");
  44. return MakeHolder<TExplicitHistogramCollector>(std::move(bounds));
  45. }
  46. }