aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/monlib/dynamic_counters/golovan_page.cpp
blob: 0654519a7e435b5170f6510a65414b3912d19cd0 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "golovan_page.h"

#include <library/cpp/monlib/service/pages/templates.h>

#include <util/string/split.h>
#include <util/system/tls.h>

using namespace NMonitoring;

class TGolovanCountableConsumer: public ICountableConsumer {
public:
    using TOutputCallback = std::function<void()>;

    TGolovanCountableConsumer(IOutputStream& out, TOutputCallback& OutputCallback)
        : out(out)
    {
        if (OutputCallback) {
            OutputCallback();
        }

        out << HTTPOKJSON << "[";
        FirstCounter = true;
    }

    void OnCounter(const TString&, const TString& value, const TCounterForPtr* counter) override {
        if (FirstCounter) {
            FirstCounter = false;
        } else {
            out << ",";
        }

        out << "[\"" << prefix + value;
        if (counter->ForDerivative()) {
            out << "_dmmm";
        } else {
            out << "_ahhh";
        }

        out << "\"," << counter->Val() << "]";
    }

    void OnHistogram(const TString&, const TString&, IHistogramSnapshotPtr, bool) override {
    }

    void OnGroupBegin(const TString&, const TString& value, const TDynamicCounters*) override {
        prefix += value;
        if (!value.empty()) {
            prefix += "_";
        }
    }

    void OnGroupEnd(const TString&, const TString&, const TDynamicCounters*) override {
        prefix = "";
    }

    void Flush() {
        out << "]";
        out.Flush();
    }

private:
    IOutputStream& out; 
    bool FirstCounter;
    TString prefix;
};

TGolovanCountersPage::TGolovanCountersPage(const TString& path, TIntrusivePtr<NMonitoring::TDynamicCounters> counters,
                                           TOutputCallback outputCallback)
    : IMonPage(path)
    , Counters(counters)
    , OutputCallback(outputCallback)
{
}

void TGolovanCountersPage::Output(IMonHttpRequest& request) {
    TGolovanCountableConsumer consumer(request.Output(), OutputCallback);
    Counters->Accept("", "", consumer);
    consumer.Flush();
}