aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/grpc/server/grpc_counters.h
blob: 0b6c36c84ccc366bd6e65bad9e0749308ff692c6 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#pragma once

#include <library/cpp/monlib/dynamic_counters/percentile/percentile.h>
#include <library/cpp/monlib/dynamic_counters/counters.h>
#include <util/generic/ptr.h>

namespace NGrpc {

struct ICounterBlock : public TThrRefBase {
    virtual void CountNotOkRequest() = 0;
    virtual void CountNotOkResponse() = 0;
    virtual void CountNotAuthenticated() = 0;
    virtual void CountResourceExhausted() = 0;
    virtual void CountRequestBytes(ui32 requestSize) = 0;
    virtual void CountResponseBytes(ui32 responseSize) = 0;
    virtual void StartProcessing(ui32 requestSize) = 0;
    virtual void FinishProcessing(ui32 requestSize, ui32 responseSize, bool ok, ui32 status, TDuration requestDuration) = 0;
    virtual void CountRequestsWithoutDatabase() {}
    virtual void CountRequestsWithoutToken() {}
    virtual void CountRequestWithoutTls() {}

    virtual TIntrusivePtr<ICounterBlock> Clone() { return this; }
    virtual void UseDatabase(const TString& database) { Y_UNUSED(database); }
};

using ICounterBlockPtr = TIntrusivePtr<ICounterBlock>;

class TCounterBlock final : public ICounterBlock {
    NMonitoring::TDynamicCounters::TCounterPtr TotalCounter;
    NMonitoring::TDynamicCounters::TCounterPtr InflyCounter;
    NMonitoring::TDynamicCounters::TCounterPtr NotOkRequestCounter;
    NMonitoring::TDynamicCounters::TCounterPtr NotOkResponseCounter;
    NMonitoring::TDynamicCounters::TCounterPtr RequestBytes;
    NMonitoring::TDynamicCounters::TCounterPtr InflyRequestBytes;
    NMonitoring::TDynamicCounters::TCounterPtr ResponseBytes;
    NMonitoring::TDynamicCounters::TCounterPtr NotAuthenticated;
    NMonitoring::TDynamicCounters::TCounterPtr ResourceExhausted;
    bool Percentile = false;
    NMonitoring::TPercentileTracker<4, 512, 15> RequestHistMs;
    std::array<NMonitoring::TDynamicCounters::TCounterPtr, 2>  GRpcStatusCounters;

public:
    TCounterBlock(NMonitoring::TDynamicCounters::TCounterPtr totalCounter,
            NMonitoring::TDynamicCounters::TCounterPtr inflyCounter,
            NMonitoring::TDynamicCounters::TCounterPtr notOkRequestCounter,
            NMonitoring::TDynamicCounters::TCounterPtr notOkResponseCounter,
            NMonitoring::TDynamicCounters::TCounterPtr requestBytes,
            NMonitoring::TDynamicCounters::TCounterPtr inflyRequestBytes,
            NMonitoring::TDynamicCounters::TCounterPtr responseBytes,
            NMonitoring::TDynamicCounters::TCounterPtr notAuthenticated,
            NMonitoring::TDynamicCounters::TCounterPtr resourceExhausted,
            TIntrusivePtr<NMonitoring::TDynamicCounters> group)
        : TotalCounter(std::move(totalCounter))
        , InflyCounter(std::move(inflyCounter))
        , NotOkRequestCounter(std::move(notOkRequestCounter))
        , NotOkResponseCounter(std::move(notOkResponseCounter))
        , RequestBytes(std::move(requestBytes))
        , InflyRequestBytes(std::move(inflyRequestBytes))
        , ResponseBytes(std::move(responseBytes))
        , NotAuthenticated(std::move(notAuthenticated))
        , ResourceExhausted(std::move(resourceExhausted))
    {
        if (group) {
            RequestHistMs.Initialize(group, "event", "request", "ms", {0.5f, 0.9f, 0.99f, 0.999f, 1.0f});
            Percentile = true;
        }
    }

    void CountNotOkRequest() override {
        NotOkRequestCounter->Inc();
    }

    void CountNotOkResponse() override {
        NotOkResponseCounter->Inc();
    }

    void CountNotAuthenticated() override {
        NotAuthenticated->Inc();
    }

    void CountResourceExhausted() override {
        ResourceExhausted->Inc();
    }

    void CountRequestBytes(ui32 requestSize) override {
        *RequestBytes += requestSize;
    }

    void CountResponseBytes(ui32 responseSize) override {
        *ResponseBytes += responseSize;
    }

    void StartProcessing(ui32 requestSize) override {
        TotalCounter->Inc();
        InflyCounter->Inc();
        *RequestBytes += requestSize;
        *InflyRequestBytes += requestSize;
    }

    void FinishProcessing(ui32 requestSize, ui32 responseSize, bool ok, ui32 status,
        TDuration requestDuration) override
    {
        Y_UNUSED(status);

        InflyCounter->Dec();
        *InflyRequestBytes -= requestSize;
        *ResponseBytes += responseSize;
        if (!ok) {
            NotOkResponseCounter->Inc();
        }
        if (Percentile) {
            RequestHistMs.Increment(requestDuration.MilliSeconds());
        }
    }

    ICounterBlockPtr Clone() override {
        return this;
    }

    void Update() {
        if (Percentile) {
            RequestHistMs.Update();
        }
    }
};

using TCounterBlockPtr = TIntrusivePtr<TCounterBlock>;

/**
 * Creates new instance of ICounterBlock implementation which does nothing.
 *
 * @return new instance
 */
ICounterBlockPtr FakeCounterBlock();

} // namespace NGrpc