aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/metrics/histogram_collector_explicit.cpp
blob: a567573a022b7b0ee57a2e2df488f4b6a91a01e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "histogram_collector.h"
#include "atomics_array.h"

#include <util/generic/algorithm.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/generic/ylimits.h>

namespace NMonitoring {

    ///////////////////////////////////////////////////////////////////////////
    // TExplicitHistogramCollector
    ///////////////////////////////////////////////////////////////////////////
    class TExplicitHistogramCollector: public IHistogramCollector {
    public:
        TExplicitHistogramCollector(TBucketBounds bounds)
            : Values_(bounds.size() + 1)
            , Bounds_(std::move(bounds))
        {
            // add one bucket as +INF
            Bounds_.push_back(Max<TBucketBound>());
        }

        void Collect(double value, ui64 count) override {
            auto it = LowerBound(Bounds_.begin(), Bounds_.end(), value);
            auto index = std::distance(Bounds_.begin(), it);
            Values_.Add(index, count);
        }

        void Reset() override {
            Values_.Reset();
        }

        IHistogramSnapshotPtr Snapshot() const override {
            auto values = Values_.Copy();
            return ExplicitHistogramSnapshot(Bounds_, values);
        }

    private:
        TAtomicsArray Values_;
        TBucketBounds Bounds_;
    };

    IHistogramCollectorPtr ExplicitHistogram(TBucketBounds bounds) {
        Y_ENSURE(bounds.size() >= 1,
                 "explicit histogram must contain at least one bucket");
        Y_ENSURE(bounds.size() <= HISTOGRAM_MAX_BUCKETS_COUNT,
                 "buckets count must be <=" << HISTOGRAM_MAX_BUCKETS_COUNT
                                            << ", but got: " << bounds.size());
        Y_ENSURE(IsSorted(bounds.begin(), bounds.end()),
                 "bounds for explicit histogram must be sorted");

        return MakeHolder<TExplicitHistogramCollector>(std::move(bounds));
    }
}