|
1 | 1 | #include "grpc_server.h" |
2 | 2 |
|
| 3 | +#include <library/cpp/monlib/dynamic_counters/counters.h> |
| 4 | +#include <library/cpp/time_provider/monotonic.h> |
| 5 | + |
3 | 6 | #include <util/string/join.h> |
4 | 7 | #include <util/generic/yexception.h> |
5 | 8 | #include <util/system/thread.h> |
|
18 | 21 |
|
19 | 22 | namespace NYdbGrpc { |
20 | 23 |
|
21 | | -using NThreading::TFuture; |
22 | | - |
23 | | -static void PullEvents(grpc::ServerCompletionQueue* cq) { |
| 24 | +static void PullEvents(grpc::ServerCompletionQueue* cq, TIntrusivePtr<::NMonitoring::TDynamicCounters> counters) { |
24 | 25 | TThread::SetCurrentThreadName("grpc_server"); |
| 26 | + auto okCounter = counters->GetCounter("RequestExecuted", true); |
| 27 | + auto errorCounter = counters->GetCounter("RequestDestroyed", true); |
| 28 | + auto cpuTime = counters->GetCounter("ThreadCPU", true); |
| 29 | + |
| 30 | + NMonotonic::TMonotonic lastCpuTimeTs = {}; |
25 | 31 | while (true) { |
26 | 32 | void* tag; // uniquely identifies a request. |
27 | 33 | bool ok; |
28 | 34 |
|
| 35 | + auto now = NMonotonic::TMonotonic::Now(); |
| 36 | + if (now - lastCpuTimeTs >= TDuration::Seconds(1)) { |
| 37 | + lastCpuTimeTs = now; |
| 38 | + *cpuTime = ThreadCPUTime(); |
| 39 | + } |
| 40 | + |
29 | 41 | if (cq->Next(&tag, &ok)) { |
30 | 42 | IQueueEvent* const ev(static_cast<IQueueEvent*>(tag)); |
31 | 43 |
|
32 | | - if (!ev->Execute(ok)) { |
| 44 | + if (ev->Execute(ok)) { |
| 45 | + okCounter->Inc(); |
| 46 | + } else { |
33 | 47 | ev->DestroyRequest(); |
| 48 | + errorCounter->Inc(); |
34 | 49 | } |
35 | 50 | } else { |
36 | 51 | break; |
@@ -103,10 +118,16 @@ void TGrpcServiceProtectiable::DecRequest() { |
103 | 118 | } |
104 | 119 | } |
105 | 120 |
|
106 | | -TGRpcServer::TGRpcServer(const TServerOptions& opts) |
| 121 | +TGRpcServer::TGRpcServer(const TServerOptions& opts, TIntrusivePtr<::NMonitoring::TDynamicCounters> counters) |
107 | 122 | : Options_(opts) |
| 123 | + , Counters_(std::move(counters)) |
108 | 124 | , Limiter_(Options_.MaxGlobalRequestInFlight) |
109 | | - {} |
| 125 | +{ |
| 126 | + if (!Counters_) { |
| 127 | + // make a stub to simplify code |
| 128 | + Counters_.Reset(new ::NMonitoring::TDynamicCounters()); |
| 129 | + } |
| 130 | +} |
110 | 131 |
|
111 | 132 | TGRpcServer::~TGRpcServer() { |
112 | 133 | Y_ABORT_UNLESS(Ts.empty()); |
@@ -237,10 +258,12 @@ void TGRpcServer::Start() { |
237 | 258 | } |
238 | 259 |
|
239 | 260 | Ts.reserve(Options_.WorkerThreads); |
| 261 | + auto grpcCounters = Counters_->GetSubgroup("counters", "grpc"); |
240 | 262 | for (size_t i = 0; i < Options_.WorkerThreads; ++i) { |
241 | 263 | auto* cq = &CQS_[i % CQS_.size()]; |
242 | | - Ts.push_back(SystemThreadFactory()->Run([cq] { |
243 | | - PullEvents(cq->get()); |
| 264 | + auto workerCounters = grpcCounters->GetSubgroup("worker", ToString(i)); |
| 265 | + Ts.push_back(SystemThreadFactory()->Run([cq, workerCounters] { |
| 266 | + PullEvents(cq->get(), std::move(workerCounters)); |
244 | 267 | })); |
245 | 268 | } |
246 | 269 |
|
|
0 commit comments