aboutsummaryrefslogtreecommitdiffstats
path: root/ydb/core/ymq/base/counters.h
blob: 3c2667b2410143d4f42227e71466c9b147162889 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
#pragma once

#include <ydb/core/protos/config.pb.h>

#include <ydb/core/ymq/base/action.h>
#include <ydb/core/ymq/base/cloud_enums.h>
#include <ydb/core/ymq/base/query_id.h>
#include <ydb/core/ymq/base/queue_path.h>

#include <ydb/library/yql/minikql/aligned_page_pool.h>

#include <library/cpp/monlib/dynamic_counters/counters.h>

#include <util/system/spinlock.h>
#include <util/generic/hash.h>

#include <atomic>
#include <utility>

namespace NKikimr::NSQS {

struct TUserCounters;
struct TQueueCounters;
struct THttpCounters;
struct TCloudAuthCounters;

#define INC_COUNTER(countersPack, counter)       \
    if (countersPack) {                          \
        ++*countersPack->counter;                \
    }

#define INC_COUNTER_COUPLE(countersPack, sqsCounter, ymqCounter)       \
    if (countersPack) {                                                \
        ++*countersPack->sqsCounter;                                   \
        ++*countersPack->ymqCounter;                                   \
    }

#define DEC_COUNTER(countersPack, counter)       \
    if (countersPack) {                          \
        --*countersPack->counter;                \
    }

#define ADD_COUNTER(countersPack, counter, count)   \
    if (countersPack) {                             \
        *countersPack->counter += (count);          \
    }

#define ADD_COUNTER_COUPLE(countersPack, sqsCounter, ymqCounter, count)   \
    if (countersPack) {                                                   \
        *countersPack->sqsCounter += (count);                             \
        *countersPack->ymqCounter += (count);                             \
    }

#define SET_COUNTER_COUPLE(countersPack, sqsCounter, ymqCounter, count)   \
    if (countersPack) {                                                   \
        *countersPack->sqsCounter = (count);                              \
        *countersPack->ymqCounter = (count);                              \
    }

#define COLLECT_HISTOGRAM_COUNTER(countersPack, counter, count) \
    if (countersPack) {                                         \
        countersPack->counter->Collect(count);                  \
    }

#define COLLECT_HISTOGRAM_COUNTER_COUPLE(countersCouple, counter, count)    \
    if (countersCouple.SqsCounters) {                                       \
        countersCouple.SqsCounters->counter->Collect(count);                \
    }                                                                       \
    if (countersCouple.YmqCounters) {                                       \
        countersCouple.YmqCounters->counter->Collect(count);                \
    }

// Enums for code readability and for static types guarantee that all parameters passed correctly.
enum class ELaziness {
    OnStart,
    OnDemand,
};

enum class ELifetime {
    Persistent,
    Expiring,
};

enum class EValueType {
    Absolute,
    Derivative,
};

extern const TString TOTAL_COUNTER_LABEL;
//constexpr static std::array<int, 10> RESPONSE_CODES = {200, 400, 403, 404, 500, 503, 504};

template<typename TCounterPtrType>
struct TCountersCouple {
    TCounterPtrType SqsCounters = nullptr;
    TCounterPtrType YmqCounters = nullptr;
    bool Defined() {
        return SqsCounters != nullptr;
    }
};

using TIntrusivePtrCntrCouple = TCountersCouple<TIntrusivePtr<NMonitoring::TDynamicCounters>>;

TIntrusivePtr<NMonitoring::TDynamicCounters> GetSqsServiceCounters(
        const TIntrusivePtr<NMonitoring::TDynamicCounters>& countersRoot, const TString& subgroup);
TIntrusivePtr<NMonitoring::TDynamicCounters> GetYmqPublicCounters(
        const TIntrusivePtr<NMonitoring::TDynamicCounters>& countersRoot);

TIntrusivePtrCntrCouple GetFolderCounters(const TIntrusivePtrCntrCouple& userCounters, const TString& folderId);
void RemoveFolderCounters(const TIntrusivePtrCntrCouple& userCounters, const TString& folderId);
std::pair<TIntrusivePtrCntrCouple, TIntrusivePtrCntrCouple> GetUserAndQueueCounters(
        const TIntrusivePtrCntrCouple& sqsRootCounters, const TQueuePath& queuePath);
TIntrusivePtr<NMonitoring::TDynamicCounters> GetAggregatedCountersFromSqsCoreCounters(
        const TIntrusivePtrCntrCouple& sqsCoreCounters, const NKikimrConfig::TSqsConfig& cfg);
TIntrusivePtr<NMonitoring::TDynamicCounters> GetAggregatedCountersFromUserCounters(
        const TIntrusivePtrCntrCouple& sqsCoreCounters, const NKikimrConfig::TSqsConfig& cfg);

extern const TString DEFAULT_COUNTER_NAME;
extern const TString DEFAULT_YMQ_COUNTER_NAME;
//extern const TString ACTION_CNTR_PREFIX;

namespace NDetails {

template <class TTargetCounterType, class TDerived>
struct TLazyCachedCounterBase {
    // Facade for counters aggregation.
    class TAggregatingCounterFacade {
        friend struct TLazyCachedCounterBase;

        TAggregatingCounterFacade(TLazyCachedCounterBase* parent)
            : Parent(parent)
        {
        }

        TAggregatingCounterFacade(const TAggregatingCounterFacade&) = delete;
        TAggregatingCounterFacade(TAggregatingCounterFacade&&) = delete;

    public:
        ~TAggregatingCounterFacade() = default;

#define FOR_EACH_AGGREGATED_COUNTER(action)                             \
        auto* lazyCounter = Parent;                                     \
        while (lazyCounter) {                                           \
            auto& counter = *lazyCounter->EnsureCreated();              \
            action;                                                     \
            lazyCounter = lazyCounter->AggregatedParent;                \
        }

        TAggregatingCounterFacade& operator++() {
            FOR_EACH_AGGREGATED_COUNTER(++counter);
            return *this;
        }

        TAggregatingCounterFacade& operator--() {
            FOR_EACH_AGGREGATED_COUNTER(--counter);
            return *this;
        }

        TAggregatingCounterFacade& operator+=(i64 value) {
            FOR_EACH_AGGREGATED_COUNTER(counter += value);
            return *this;
        }

        TAggregatingCounterFacade& operator=(i64 value) {
            auto* lazyCounter = Parent;
            i64 diff = 0;
            if (lazyCounter) {
                auto& counter = *lazyCounter->EnsureCreated();
                auto prevValue = AtomicSwap(&counter.GetAtomic(), value);
                diff = value - prevValue;
                lazyCounter = lazyCounter->AggregatedParent;
            }

            // Aggregation
            while (lazyCounter) {
                auto& counter = *lazyCounter->EnsureCreated();
                counter += diff;
                lazyCounter = lazyCounter->AggregatedParent;
            }
            return *this;
        }

        void Collect(i64 value) {
            FOR_EACH_AGGREGATED_COUNTER(counter.Collect(value));
        }

        void Add(i64 value) {
            FOR_EACH_AGGREGATED_COUNTER(counter += value);
        }

        void Inc() {
            FOR_EACH_AGGREGATED_COUNTER(++counter);
        }

#undef FOR_EACH_AGGREGATED_COUNTER

        TAggregatingCounterFacade* operator->() {
            return this;
        }

    private:
        TLazyCachedCounterBase* const Parent = nullptr;
    };

    TLazyCachedCounterBase()
        : Counter(nullptr)
    {
    }

    TLazyCachedCounterBase(const TLazyCachedCounterBase& other) {
        TTargetCounterType* counter = other.Counter;
        std::atomic_init(&Counter, counter);
        if (counter) {
            counter->Ref();
        } else {
            RootCounters = other.RootCounters;
            Value = other.Value;
            Lifetime = other.Lifetime;
        }
    }

    ~TLazyCachedCounterBase() {
        TTargetCounterType* const counter = Counter;
        if (counter) {
            counter->UnRef();
        }
    }

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, const TString& value, ELaziness laziness) {
        Init(rootCounters, lifetime, DEFAULT_COUNTER_NAME, value, laziness);
    }

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, const TString& name, const TString& value, ELaziness laziness) {
        RootCounters = rootCounters;
        Name = name;
        Value = value;
        Lifetime = lifetime;
        if (laziness == ELaziness::OnStart) {
            EnsureCreated();
        }
    }

    bool operator!() const {
        TTargetCounterType* const counter = Counter;
        return counter == nullptr;
    }

    TAggregatingCounterFacade operator*() {
        return this;
    }

    TAggregatingCounterFacade operator->() {
        return this;
    }

    void SetAggregatedParent(TLazyCachedCounterBase* parent) {
        AggregatedParent = parent;
        Y_ASSERT(!HasCycle());
    }

protected:
    TTargetCounterType* EnsureCreated() {
        TTargetCounterType* const counter = Counter;
        if (counter) {
            return counter;
        }
        TIntrusivePtr<TTargetCounterType> newCounter = static_cast<TDerived*>(this)->Create();
        if (!newCounter) {
            newCounter = TDerived::Default();
            Y_ASSERT(newCounter);
        }
        TTargetCounterType* expected = nullptr;
        if (Counter.compare_exchange_strong(expected, newCounter.Get())) {
            newCounter->Ref();
            return newCounter.Get();
        }
        Y_ASSERT(Counter.load() != nullptr);
        return Counter;
    }

    bool HasCycle() const {
        TLazyCachedCounterBase* item = AggregatedParent;
        while (item) {
            if (item == this) {
                return true;
            }
            item = item->AggregatedParent;
        }
        return false;
    }

protected:
    TLazyCachedCounterBase* AggregatedParent = nullptr;
    TIntrusivePtr<NMonitoring::TDynamicCounters> RootCounters;
    TString Name;
    TString Value;
    ELifetime Lifetime = ELifetime::Persistent;
    std::atomic<TTargetCounterType*> Counter;
};

} // namespace NDetails

struct TLazyCachedCounter : public NDetails::TLazyCachedCounterBase<NMonitoring::TCounterForPtr, TLazyCachedCounter> {
    TLazyCachedCounter() = default;
    TLazyCachedCounter(const TLazyCachedCounter&) = default;

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, EValueType valueType, const TString& value, ELaziness laziness) {
        Init(rootCounters, lifetime, valueType, DEFAULT_COUNTER_NAME, value, laziness);
    }

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, EValueType valueType, const TString& name, const TString& value, ELaziness laziness) {
        TLazyCachedCounterBase::Init(rootCounters, lifetime, name, value, ELaziness::OnDemand);
        ValueType = valueType;
        if (laziness == ELaziness::OnStart) {
            EnsureCreated();
        }
    }

    TIntrusivePtr<NMonitoring::TCounterForPtr> Create() {
        if (RootCounters && Name && Value) {
            const bool derivative = ValueType == EValueType::Derivative;
            return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedCounter(Name, Value, derivative) : RootCounters->GetNamedCounter(Name, Value, derivative);
        } else {
            return nullptr;
        }
    }

    static TIntrusivePtr<NMonitoring::TCounterForPtr> Default() {
        static TIntrusivePtr<NMonitoring::TCounterForPtr> counter = new NMonitoring::TCounterForPtr();
        return counter;
    }

private:
    EValueType ValueType = EValueType::Absolute;
};

struct TLazyCachedHistogram : public NDetails::TLazyCachedCounterBase<NMonitoring::THistogramCounter, TLazyCachedHistogram> {
    TLazyCachedHistogram() = default;
    TLazyCachedHistogram(const TLazyCachedHistogram&) = default;

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, const NMonitoring::TBucketBounds& buckets, const TString& value, ELaziness laziness) {
        Init(rootCounters, lifetime, buckets, DEFAULT_COUNTER_NAME, value, laziness);
    }

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, ELifetime lifetime, const NMonitoring::TBucketBounds& buckets, const TString& name, const TString& value, ELaziness laziness) {
        Buckets = &buckets;
        TLazyCachedCounterBase::Init(rootCounters, lifetime, name, value, ELaziness::OnDemand);
        if (laziness == ELaziness::OnStart) {
            EnsureCreated();
        }
    }

    TIntrusivePtr<NMonitoring::THistogramCounter> Create() {
        if (RootCounters && Name && Value && Buckets) {
            return Lifetime == ELifetime::Expiring ? RootCounters->GetExpiringNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*Buckets)) : RootCounters->GetNamedHistogram(Name, Value, NMonitoring::ExplicitHistogram(*Buckets));
        } else {
            return nullptr;
        }
    }

    static TIntrusivePtr<NMonitoring::THistogramCounter> Default() {
        static TIntrusivePtr<NMonitoring::THistogramCounter> counter = new NMonitoring::THistogramCounter(NMonitoring::ExplicitHistogram({1}));
        return counter;
    }

private:
    const NMonitoring::TBucketBounds* Buckets = nullptr;
};

// Counters for actions (like SendMessage, CreateQueue or GetQueueUrl).
struct TActionCounters {
public:
    TLazyCachedCounter Success;
    TLazyCachedCounter Errors; // User metric for cloud console (SendMessage/ReceiveMessage/DeleteMessage).
    TLazyCachedCounter Infly;

    TLazyCachedHistogram Duration; // Histogram with buckets for durations (== 18 counters). // User metric for cloud console (SendMessage/DeleteMessage).

    // only for receive message action
    TLazyCachedHistogram WorkingDuration; // Special duration except wait time for ReceiveMessage action (== 18 counters). // User metric for cloud console (ReceiveMessage).

public:
    void Init(const NKikimrConfig::TSqsConfig& cfg, const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters,
                      EAction action, ELifetime lifetime = ELifetime::Persistent);

    virtual void SetAggregatedParent(TActionCounters* parent);
    virtual ~TActionCounters() {}
};

struct TYmqActionCounters : public TActionCounters {
    void Init(const NKikimrConfig::TSqsConfig& cfg, const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters,
              EAction action, const TString& labelName, const TString& namePrefix,
              ELifetime lifetime = ELifetime::Persistent);
public:
    virtual ~TYmqActionCounters() {}
    void SetAggregatedParent(TActionCounters*) override {
        return;
    }

private:
    NMonitoring::TDynamicCounterPtr SubGroup;
};

// Counters for typed queries (WRITE_MESSAGE_ID, PURGE_QUEUE_ID and etc).
struct TQueryTypeCounters {
    TLazyCachedCounter TransactionsCount;
    TLazyCachedCounter TransactionsFailed;
    TLazyCachedHistogram TransactionDuration; // Histogram with buckets for durations (== 18 counters).

    void SetAggregatedParent(TQueryTypeCounters* parent);
};

// Counters for transactions processing.
// These counters are present in queue counters and in user counters.
struct TTransactionCounters : public TAtomicRefCount<TTransactionCounters> {
    // Query types are declared in ydb/core/ymq/base/query_id.h.
    TQueryTypeCounters QueryTypeCounters[EQueryId::QUERY_VECTOR_SIZE]; // 23 types at all. 15 for std. 16 for fifo. 2 for user.

    std::shared_ptr<TAlignedPagePoolCounters> AllocPoolCounters; // counters for kikimr core.

    TLazyCachedCounter CompileQueryCount; // Compiles count.
    TLazyCachedCounter TransactionsCount; // Transactions processed.
    TLazyCachedCounter TransactionsInfly; // Current transactions count inflight.
    TLazyCachedCounter TransactionRetryTimeouts; // Count of times when we got temporary error from transaction, but can't retry, because there is no time left.
    TLazyCachedCounter TransactionRetries; // Transactions retries due to temporary errors.
    TLazyCachedCounter TransactionsFailed; // Transactions that failed.

    TIntrusivePtr<TTransactionCounters> AggregatedParent; // Less detailed transaction counters aggregated by queue/user.

    void SetAggregatedParent(const TIntrusivePtr<TTransactionCounters>& parent);

public:
    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters,
        std::shared_ptr<TAlignedPagePoolCounters> poolCounters, bool forQueue);
};

// Amazon status codes.
struct TAPIStatusesCounters {
public:
    void AddError(const TString& errorCode, size_t count = 1);
    void AddOk(size_t count = 1);

    void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& root);

    void SetAggregatedParent(TAPIStatusesCounters* parent);

private:
    THashMap<TString, TLazyCachedCounter> ErrorToCounter; // Map with different status codes. See ydb/core/ymq/base/error.cpp.
    TLazyCachedCounter OkCounter; // Special status for successful requests.
    TLazyCachedCounter UnknownCounter; // Special status for statuses that are not in map.
};

// User counters in SQS core subsystem.
struct TUserCounters : public TAtomicRefCount<TUserCounters> {
    // Action types are declared in ydb/core/ymq/base/action.h.
    TActionCounters SqsActionCounters[EAction::ActionsArraySize]; // 11 actions. See IsActionForUser() function in ydb/core/ymq/base/action.cpp.
    TYmqActionCounters YmqActionCounters[EAction::ActionsArraySize]; // see IsActionForUserYMQ() function.

    TLazyCachedCounter RequestTimeouts; // Requests that weren't processed in 10 minutes. They are almost sure hanged.

    TLazyCachedCounter UnauthenticatedAccess; // For users in exception list and total (aggregated) - unauthenticated accesses count.

    struct TDetailedCounters {
        TIntrusivePtr<TTransactionCounters> TransactionCounters;

        TAPIStatusesCounters APIStatuses;

        TLazyCachedHistogram GetConfiguration_Duration; // Part of request initialization. Histogram with buckets for durations (== 18 counters).
        TLazyCachedHistogram GetQuota_Duration; // Histogram with buckets for quota durations (== 16 counters).

        TLazyCachedCounter CreateAccountOnTheFly_Success; // Account created on the fly (Yandex Cloud mode).
        TLazyCachedCounter CreateAccountOnTheFly_Errors; // Account that were failed to create on the fly (Yandex Cloud mode).

        void Init(const TIntrusivePtrCntrCouple& userCounters,
                  const std::shared_ptr<TAlignedPagePoolCounters>& allocPoolCounters,
                  const NKikimrConfig::TSqsConfig& cfg);
        void SetAggregatedParent(TDetailedCounters* parent);
    };

    TDetailedCounters* GetDetailedCounters() {
        if (NeedToShowDetailedCounters()) {
            return &DetailedCounters;
        } else if (AggregatedParent) {
            return AggregatedParent->GetDetailedCounters();
        } else {
            return nullptr;
        }
    }

    // Raw counters interface
    // Don't use counters by name!
    TIntrusivePtrCntrCouple SqsCoreCounters; // Sqs core subsystem
    TIntrusivePtrCntrCouple UserCounters; // User tree in core subsystem

    TUserCounters(
            const NKikimrConfig::TSqsConfig& cfg, const TIntrusivePtr<NMonitoring::TDynamicCounters>& sqsCoreCounters,
            const TIntrusivePtr<NMonitoring::TDynamicCounters>& ymqRootCounters,
            const std::shared_ptr<TAlignedPagePoolCounters>& allocPoolCounters, const TString& userName,
            const TIntrusivePtr<TUserCounters>& aggregatedParent,
            bool isAggregatedCounters = false
    )
        : SqsCoreCounters{sqsCoreCounters, ymqRootCounters}
        , Cfg(&cfg)
        , UserName(userName)
        , AggregatedParent(aggregatedParent)
        , IsAggregatedCounters(isAggregatedCounters)
    {
        InitCounters(userName, allocPoolCounters);
    }

    TIntrusivePtr<TQueueCounters> CreateQueueCounters(const TString& queueName, const TString& folderId, bool insertCounters);

    void RemoveCounters();

    void ShowDetailedCounters(TInstant deadline) {
        Y_ASSERT(ShowDetailedCountersDeadline);
        *ShowDetailedCountersDeadline = deadline.GetValue();
    }

    bool NeedToShowDetailedCounters() const {
        Y_ASSERT(ShowDetailedCountersDeadline);
        return TInstant::Now().GetValue() < *ShowDetailedCountersDeadline;
    }

    void DisableCounters(bool disable);

    void ExportTransactionCounters(bool needExport) {
        NeedExportTransactionCounters = needExport;
    }

    TIntrusivePtr<TTransactionCounters> GetTransactionCounters() const {
        if (NeedExportTransactionCounters || NeedToShowDetailedCounters()) {
            return DetailedCounters.TransactionCounters;
        } else if (AggregatedParent) {
            return AggregatedParent->GetTransactionCounters();
        } else {
            return nullptr;
        }
    }

private:
    void InitCounters(const TString& userName, const std::shared_ptr<TAlignedPagePoolCounters>& allocPoolCounters);
    TIntrusivePtr<TQueueCounters> CreateQueueCountersImpl(const TString& queueName, const TString& folderId, bool insertCounters, bool aggregated = false);

    friend struct TQueueCounters;

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
    const TString UserName;
    std::shared_ptr<std::atomic<ui64>> ShowDetailedCountersDeadline = std::make_shared<std::atomic<ui64>>(0ul); // TInstant value
    std::atomic<bool> NeedExportTransactionCounters = false;
    TDetailedCounters DetailedCounters;
    TIntrusivePtr<TUserCounters> AggregatedParent;
    TIntrusivePtr<TQueueCounters> AggregatedQueueCounters;
    bool IsAggregatedCounters;
};

// Queue counters in SQS core subsystem.
struct TQueueCounters : public TAtomicRefCount<TQueueCounters> {
    // Action types are declared in ydb/core/ymq/base/action.h.
    TActionCounters SqsActionCounters[EAction::ActionsArraySize]; // 12 actions. See IsActionForQueue() function in ydb/core/ymq/base/action.cpp.
    TYmqActionCounters YmqActionCounters[EAction::ActionsArraySize]; // See IsActionForQueueYMQ() function in ydb/core/ymq/sqs/base/action.cpp.

    TLazyCachedCounter RequestTimeouts; // Requests that weren't processed in 10 minutes. They are almost sure hanged.
    TLazyCachedCounter request_timeouts_count_per_second; // Requests that weren't processed in 10 minutes. They are almost sure hanged.
    TLazyCachedCounter RequestsThrottled; // Request that ended with ThrottlingException
    TLazyCachedCounter QueueMasterStartProblems; // TODO: remove after migration
    TLazyCachedCounter QueueLeaderStartProblems; // Critical problems during leader start.

    TLazyCachedCounter MessagesPurged;
    TLazyCachedCounter purged_count_per_second;

    TLazyCachedHistogram MessageReceiveAttempts; // User attempts for receive messages. Histogram with buckets for receive attempts (== 4 counters). // User metric for cloud console.
    TLazyCachedHistogram receive_attempts_count_rate;
    TLazyCachedHistogram ClientMessageProcessing_Duration; // Time between receive and delete for deleted message. Histogram with buckets for client processing (== 21 counters). // User metric for cloud console.
    TLazyCachedHistogram client_processing_duration_milliseconds;
    TLazyCachedHistogram MessageReside_Duration; // Time between send and receive for received messages. Histogram with buckets for client processing (== 21 counters). // User metric for cloud console.
    TLazyCachedHistogram reside_duration_milliseconds;

    TLazyCachedCounter DeleteMessage_Count; // Messages count that were deleted. // User metric for cloud console.
    TLazyCachedCounter deleted_count_per_second;

    TLazyCachedCounter ReceiveMessage_EmptyCount; // Receive message requests count that returned empty results.
    TLazyCachedCounter empty_receive_attempts_count_per_second;
    TLazyCachedCounter ReceiveMessage_Count; // Messages count that were received. // User metric for cloud console.
    TLazyCachedCounter received_count_per_second;
    TLazyCachedCounter ReceiveMessage_BytesRead; // Bytes of message bodies that were received. // User metric for cloud console.
    TLazyCachedCounter received_bytes_per_second;

    TLazyCachedCounter MessagesMovedToDLQ; // Count of messages that were moved to DLQ.

    TLazyCachedCounter SendMessage_DeduplicationCount; // Count of messages that were deduplicated (for fifo only).
    TLazyCachedCounter deduplicated_count_per_second;
    TLazyCachedCounter SendMessage_Count; // Count of messages that were sent. // User metric for cloud console.
    TLazyCachedCounter sent_count_per_second;
    TLazyCachedCounter SendMessage_BytesWritten; // Bytes of message bodies that were sent.
    TLazyCachedCounter sent_bytes_per_second;

    TLazyCachedCounter MessagesCount; // Messages count in queue. // User metric for cloud console.
    TLazyCachedCounter stored_count;
    TLazyCachedCounter InflyMessagesCount; // Messages count in queue that are inflight. // User metric for cloud console.
    TLazyCachedCounter inflight_count;
    TLazyCachedCounter OldestMessageAgeSeconds; // Age of the oldest message in queue. // User metric for cloud console.
    TLazyCachedCounter oldest_age_milliseconds;

    struct TDetailedCounters {
        TIntrusivePtr<TTransactionCounters> TransactionCounters;

        TLazyCachedHistogram GetConfiguration_Duration; // Part of request initialization. Histogram with buckets for durations (== 18 counters).

        TLazyCachedCounter ReceiveMessage_KeysInvalidated; // Count of attempts to receive a message, but race occured.

        TLazyCachedHistogram ReceiveMessageImmediate_Duration; // Time for receive message request that was processed with only one attempt (without wait or try many shards). Histogram with buckets for durations (== 18 counters).

        void Init(const TIntrusivePtr<NMonitoring::TDynamicCounters>& queueCounters,
            const std::shared_ptr<TAlignedPagePoolCounters>& allocPoolCounters, bool forLeaderNode);
        void SetAggregatedParent(TDetailedCounters* parent);
    };

    TDetailedCounters* GetDetailedCounters() {
        if (NeedToShowDetailedCounters()) {
            return &DetailedCounters;
        } else if (AggregatedParent) {
            return AggregatedParent->GetDetailedCounters();
        } else {
            return nullptr;
        }
    }

    // Raw counters interface.RootCounters
    // Don't use counters by name!
    TIntrusivePtrCntrCouple RootCounters; // Sqs core subsystem.
    TIntrusivePtrCntrCouple UserCounters; // User tree in core subsystem
    TIntrusivePtrCntrCouple FolderCounters; // Folder subtree in user tree (only for Yandex Cloud).
    TIntrusivePtrCntrCouple QueueCounters; // Queue subtree in user (or folder) tree.

    // Creates counters for not leader node.
    TQueueCounters(const NKikimrConfig::TSqsConfig& cfg,
                   const TIntrusivePtrCntrCouple& sqsCoreCounters,
                   const TUserCounters* userCounters,
                   const TString& queueName,
                   const TString& folderId,
                   bool insertCounters,
                   bool aggregated);

    TQueueCounters(const TQueueCounters&) = default;

    void InsertCounters();
    void RemoveCounters();

    TIntrusivePtr<TQueueCounters> GetCountersForLeaderNode();
    TIntrusivePtr<TQueueCounters> GetCountersForNotLeaderNode();

    void ShowDetailedCounters(TInstant deadline) {
        Y_ASSERT(ShowDetailedCountersDeadline);
        *ShowDetailedCountersDeadline = deadline.GetValue();
    }

    bool NeedToShowDetailedCounters() const {
        Y_ASSERT(ShowDetailedCountersDeadline);
        Y_ASSERT(UserShowDetailedCountersDeadline);
        const TInstant now = TInstant::Now();
        return now.GetValue() < *ShowDetailedCountersDeadline || now.GetValue() < *UserShowDetailedCountersDeadline;
    }

    TIntrusivePtr<TTransactionCounters> GetTransactionCounters() const {
        if (NeedToShowDetailedCounters()) {
            return DetailedCounters.TransactionCounters;
        } else if (AggregatedParent) {
            return AggregatedParent->GetTransactionCounters();
        } else {
            return nullptr;
        }
    }

    void SetAggregatedParent(const TIntrusivePtr<TQueueCounters>& parent);

private:
    void InitCounters(bool forLeaderNode = false);

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
    const TString QueueName;
    bool AggregatedCounters = false;
    TIntrusivePtr<TQueueCounters> NotLeaderNodeCounters;
    std::shared_ptr<std::atomic<ui64>> ShowDetailedCountersDeadline = std::make_shared<std::atomic<ui64>>(0ul); // TInstant value
    std::shared_ptr<std::atomic<ui64>> UserShowDetailedCountersDeadline;
    TDetailedCounters DetailedCounters;
    std::shared_ptr<TAlignedPagePoolCounters> AllocPoolCounters; // Transaction counters for kikimr core.
    TIntrusivePtr<TQueueCounters> AggregatedParent;
};

//
// Http subsystem counters
//

// Http counters for actions (like SendMessage, CreateQueue or GetQueueUrl).
struct THttpActionCounters {
    TLazyCachedCounter Requests; // Requests count of given type.

    void Init(const NKikimrConfig::TSqsConfig& cfg, const TIntrusivePtr<NMonitoring::TDynamicCounters>& rootCounters, EAction action);
    void SetAggregatedParent(THttpActionCounters* parent);

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
};

struct THttpUserCounters : public TAtomicRefCount<THttpUserCounters> {
    // Action types are declared in ydb/core/ymq/base/action.h.
    THttpActionCounters ActionCounters[EAction::ActionsArraySize]; // 23 actions.

    TLazyCachedCounter RequestExceptions; // Exceptions count during http request processing.

    // Raw counters interface
    // Don't use counters by name!
    TIntrusivePtr<NMonitoring::TDynamicCounters> SqsHttpCounters; // Sqs http subsystem
    TIntrusivePtr<NMonitoring::TDynamicCounters> UserCounters; // User tree in core subsystem

    THttpUserCounters(const NKikimrConfig::TSqsConfig& cfg, const TIntrusivePtr<NMonitoring::TDynamicCounters>& sqsHttpCounters, const TString& userName)
        : SqsHttpCounters(sqsHttpCounters)
        , Cfg(&cfg)
    {
        InitCounters(userName);
    }

    void SetAggregatedParent(const TIntrusivePtr<THttpUserCounters>& parent);

private:
    void InitCounters(const TString& userName);

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
    TIntrusivePtr<THttpUserCounters> AggregatedParent;
};

struct THttpCounters : public TAtomicRefCount<THttpCounters> {
    TLazyCachedCounter RequestExceptions; // Exceptions count during http request processing.
    TLazyCachedCounter InternalExceptions; // Internal exceptions count.
    TLazyCachedCounter ConnectionsCount; // Connections count that kikimr http server sees.

    // Raw counters interface
    // Don't use counters by name!
    TIntrusivePtr<NMonitoring::TDynamicCounters> SqsHttpCounters; // Sqs http subsystem

    THttpCounters(const NKikimrConfig::TSqsConfig& cfg, TIntrusivePtr<NMonitoring::TDynamicCounters> sqsHttpCounters)
        : SqsHttpCounters(std::move(sqsHttpCounters))
        , Cfg(&cfg)
    {
        InitCounters();
    }

    TIntrusivePtr<THttpUserCounters> GetUserCounters(const TString& userName);

private:
    void InitCounters();
    TIntrusivePtr<THttpUserCounters> GetUserCountersImpl(const TString& userName, const TIntrusivePtr<THttpUserCounters>& aggregatedUserCounters);

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
    TAdaptiveLock Lock;
    THashMap<TString, TIntrusivePtr<THttpUserCounters>> UserCounters;
    TIntrusivePtr<THttpUserCounters> AggregatedUserCounters;
};

// Cloud specific counters.
struct TCloudAuthCounters {
    // Durations for different security actions types.
    TLazyCachedHistogram AuthenticateDuration; // Histogram with buckets for durations (== 18 counters).
    TLazyCachedHistogram AuthorizeDuration; // Histogram with buckets for durations (== 18 counters).
    TLazyCachedHistogram GetFolderIdDuration; // Histogram with buckets for durations (== 18 counters).

    explicit TCloudAuthCounters(const NKikimrConfig::TSqsConfig& cfg, TIntrusivePtr<NMonitoring::TDynamicCounters> cloudAuthCountersRoot)
        : Cfg(&cfg)
    {
        InitCounters(std::move(cloudAuthCountersRoot));
    }

    void IncCounter(const NCloudAuth::EActionType actionType, const NCloudAuth::ECredentialType credentialType, int grpcStatus);

    static constexpr int GRPC_STATUSES_COUNT = 18;

private:
    void InitCounters(TIntrusivePtr<NMonitoring::TDynamicCounters> cloudAuthCounters);

private:
    const NKikimrConfig::TSqsConfig* Cfg = nullptr;
    TLazyCachedCounter CloudAuthCounters[NCloudAuth::EActionType::ActionTypesCount] // 3 types.
                                        [NCloudAuth::ECredentialType::CredentialTypesCount] // 2 types.
                                        [GRPC_STATUSES_COUNT]; // 18 types.
};

// Metering counters in SQS core subsystem.
struct TMeteringCounters : public TAtomicRefCount<TMeteringCounters> {
    THashMap<TString, TLazyCachedCounter> ClassifierRequestsResults;
    THashMap<TString, TLazyCachedCounter> IdleClassifierRequestsResults;

    TMeteringCounters(const NKikimrConfig::TSqsConfig& config, const TIntrusivePtr<NMonitoring::TDynamicCounters>& sqsMeteringCounters, const TVector<TString>& classifierLabels)
        : SqsMeteringCounters(sqsMeteringCounters)
        , Config(config)
    {
        InitCounters(classifierLabels);
    }

private:
    void InitCounters(const TVector<TString>& classifierLabels);

private:
    TIntrusivePtr<NMonitoring::TDynamicCounters> SqsMeteringCounters;
    const NKikimrConfig::TSqsConfig& Config;
};

} // namespace NKikimr::NSQS