15
15
#include < util/string/builder.h>
16
16
#include < util/generic/deque.h>
17
17
18
+ #include < optional>
19
+
18
20
LWTRACE_USING (KESUS_QUOTER_PROVIDER);
19
21
20
22
namespace NKikimr {
@@ -200,6 +202,58 @@ class TBillingMetric {
200
202
}
201
203
};
202
204
205
+ // //////////////////////////////////////////////////////////////////////////////
206
+ class TPublicCounters {
207
+ static std::optional<TString> GetCategory (const NKikimrKesus::TAccountingConfig::TMetric& cfg) {
208
+ for (const auto & [label, value] : cfg.GetLabels ()) {
209
+ if (to_lower (label) == " category" ) {
210
+ return value;
211
+ }
212
+ }
213
+
214
+ return std::nullopt ;
215
+ }
216
+
217
+ public:
218
+ void Configure (const NKikimrKesus::TAccountingConfig::TMetric& cfg, double limit, ::NMonitoring::TDynamicCounterPtr counters) {
219
+ std::optional<TString> category;
220
+ if (!cfg.GetEnabled ()
221
+ || !cfg.GetCloudId ()
222
+ || !cfg.GetFolderId ()
223
+ || !cfg.GetResourceId ()
224
+ || !(category = GetCategory (cfg))
225
+ ) {
226
+ Limit.Reset ();
227
+ Consumed.Reset ();
228
+ Counters.Reset ();
229
+ return ;
230
+ }
231
+
232
+ Y_ABORT_UNLESS (category.has_value ());
233
+ Counters = GetServiceCounters (counters, " ydb_serverless" , false )
234
+ ->GetSubgroup (" host" , " " )
235
+ ->GetSubgroup (" cloud_id" , cfg.GetCloudId ())
236
+ ->GetSubgroup (" folder_id" , cfg.GetFolderId ())
237
+ ->GetSubgroup (" database_id" , cfg.GetResourceId ())
238
+ ->GetSubgroup (" category" , *category);
239
+ Limit = Counters->GetExpiringNamedCounter (" name" , " resources.request_units.limit" , false );
240
+ Consumed = Counters->GetExpiringNamedCounter (" name" , " resources.request_units.consumed" , true );
241
+
242
+ *Limit = limit;
243
+ }
244
+
245
+ void Consume (double consumed) {
246
+ if (Consumed) {
247
+ *Consumed += consumed;
248
+ }
249
+ }
250
+
251
+ private:
252
+ ::NMonitoring::TDynamicCounterPtr Counters;
253
+ ::NMonitoring::TDynamicCounters::TCounterPtr Limit;
254
+ ::NMonitoring::TDynamicCounters::TCounterPtr Consumed;
255
+ };
256
+
203
257
// //////////////////////////////////////////////////////////////////////////////
204
258
class TAccountingActor final : public TActor<TAccountingActor> {
205
259
private:
@@ -219,9 +273,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
219
273
220
274
// Monitoring
221
275
TRateAccountingCounters Counters;
222
- ::NMonitoring::TDynamicCounterPtr PublicCounters;
223
- ::NMonitoring::TDynamicCounters::TCounterPtr Limit;
224
- ::NMonitoring::TDynamicCounters::TCounterPtr Consumed;
276
+ TPublicCounters PublicCounters;
225
277
226
278
public:
227
279
explicit TAccountingActor (const IBillSink::TPtr& billSink, const NKikimrKesus::TStreamingQuoterResource& props, const TString& quoterPath)
@@ -278,30 +330,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
278
330
OnDemand.Configure (accCfg.GetOnDemand (), QuoterPath, props.GetResourcePath (), " ondemand" , BillSink);
279
331
Overshoot.Configure (accCfg.GetOvershoot (), QuoterPath, props.GetResourcePath (), " overshoot" , BillSink);
280
332
281
- if (const auto & cfg = accCfg.GetOnDemand (); cfg.GetEnabled () && cfg.GetCloudId () && cfg.GetFolderId () && cfg.GetResourceId ()) {
282
- TString category = " Generic" ;
283
- for (const auto & [label, value] : cfg.GetLabels ()) {
284
- if (to_lower (label) == " category" ) {
285
- category = value;
286
- break ;
287
- }
288
- }
289
-
290
- PublicCounters = GetServiceCounters (AppData ()->Counters , " ydb_serverless" , false )
291
- ->GetSubgroup (" host" , " " )
292
- ->GetSubgroup (" cloud_id" , cfg.GetCloudId ())
293
- ->GetSubgroup (" folder_id" , cfg.GetFolderId ())
294
- ->GetSubgroup (" database_id" , cfg.GetResourceId ())
295
- ->GetSubgroup (" category" , category);
296
- Limit = PublicCounters->GetExpiringNamedCounter (" name" , " resources.request_units.limit" , false );
297
- Consumed = PublicCounters->GetExpiringNamedCounter (" name" , " resources.request_units.consumed" , true );
298
-
299
- *Limit = resCfg.GetMaxUnitsPerSecond ();
300
- } else {
301
- Limit.Reset ();
302
- Consumed.Reset ();
303
- PublicCounters.Reset ();
304
- }
333
+ PublicCounters.Configure (accCfg.GetOnDemand (), resCfg.GetMaxUnitsPerSecond (), AppData ()->Counters );
305
334
306
335
LWPROBE (ResourceAccountConfigure,
307
336
QuoterPath,
@@ -349,9 +378,7 @@ class TAccountingActor final: public TActor<TAccountingActor> {
349
378
OnDemand.Add (onDemand, t, ctx);
350
379
Overshoot.Add (overshoot, t, ctx);
351
380
352
- if (Consumed) {
353
- *Consumed += consumed;
354
- }
381
+ PublicCounters.Consume (consumed);
355
382
}
356
383
};
357
384
0 commit comments