aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/duration_counter.h
blob: dbdfc22ed4dc3138b586119e83d9ebb353b85b2c (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
#pragma once

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

#include <util/generic/deque.h>
#include <util/system/hp_timer.h>
#include <util/system/mutex.h>

namespace NUnifiedAgent {
    class TDurationUsCounter {
    public:
        class TScope {
        public:
            TScope(TDurationUsCounter& counter)
                : Counter(counter)
                , StartTime(Counter.Begin())
            {
            }

            ~TScope() {
                Counter.End(StartTime);
            }

        private:
            TDurationUsCounter& Counter;
            NHPTimer::STime* StartTime;
        };

    public:
        TDurationUsCounter(const TString& name, NMonitoring::TDynamicCounters& owner);

        NHPTimer::STime* Begin();

        void End(NHPTimer::STime* startTime);

        void Update();

    private:
        NMonitoring::TDeprecatedCounter& Counter;
        TDeque<NHPTimer::STime> ActiveTimers;
        TAdaptiveLock Lock;
    };
}