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
|
#include "producer.h"
#include <util/system/compiler.h>
#include <library/cpp/yt/memory/new.h>
namespace NYT::NProfiling {
////////////////////////////////////////////////////////////////////////////////
TWithTagGuard::TWithTagGuard(ISensorWriter* writer)
: Writer_(writer)
{
YT_VERIFY(Writer_);
}
TWithTagGuard::TWithTagGuard(ISensorWriter* writer, const std::string& tagKey, const std::string& tagValue)
: TWithTagGuard(writer)
{
AddTag(tagKey, tagValue);
}
void TWithTagGuard::AddTag(TTag tag)
{
Writer_->PushTag(std::move(tag));
++AddedTagCount_;
}
void TWithTagGuard::AddTag(const std::string& tagKey, const std::string& tagValue)
{
AddTag({tagKey, tagValue});
}
TWithTagGuard::~TWithTagGuard()
{
for (int i = 0; i < AddedTagCount_; ++i) {
Writer_->PopTag();
}
}
////////////////////////////////////////////////////////////////////////////////
void TSensorBuffer::PushTag(TTag tag)
{
Tags_.push_back(std::move(tag));
}
void TSensorBuffer::PopTag()
{
Tags_.pop_back();
}
void TSensorBuffer::AddGauge(const std::string& name, double value)
{
Gauges_.emplace_back(name, Tags_, value);
}
void TSensorBuffer::AddCounter(const std::string& name, i64 value)
{
Counters_.emplace_back(name, Tags_, value);
}
const std::vector<std::tuple<std::string, TTagList, i64>>& TSensorBuffer::GetCounters() const
{
return Counters_;
}
const std::vector<std::tuple<std::string, TTagList, double>>& TSensorBuffer::GetGauges() const
{
return Gauges_;
}
void TSensorBuffer::WriteTo(ISensorWriter* writer)
{
for (const auto& [name, tags, value] : Counters_) {
for (const auto& tag : tags) {
writer->PushTag(tag);
}
writer->AddCounter(name, value);
for (size_t i = 0; i < tags.size(); i++) {
writer->PopTag();
}
}
for (const auto& [name, tags, value] : Gauges_) {
for (const auto& tag : tags) {
writer->PushTag(tag);
}
writer->AddGauge(name, value);
for (size_t i = 0; i < tags.size(); i++) {
writer->PopTag();
}
}
}
////////////////////////////////////////////////////////////////////////////////
TIntrusivePtr<TSensorBuffer> ISensorProducer::GetBuffer()
{
auto buffer = New<TSensorBuffer>();
CollectSensors(buffer.Get());
return buffer;
}
////////////////////////////////////////////////////////////////////////////////
void TBufferedProducer::CollectSensors(ISensorWriter* )
{
YT_ABORT();
}
TIntrusivePtr<TSensorBuffer> TBufferedProducer::GetBuffer()
{
auto guard = Guard(Lock_);
if (Enabled_) {
if (Buffer_) {
return Buffer_;
} else {
return New<TSensorBuffer>();
}
}
return nullptr;
}
void TBufferedProducer::SetEnabled(bool enabled)
{
auto guard = Guard(Lock_);
Enabled_ = enabled;
}
void TBufferedProducer::Update(TSensorBuffer buffer)
{
auto ptr = New<TSensorBuffer>(std::move(buffer));
auto guard = Guard(Lock_);
Buffer_ = ptr;
}
void TBufferedProducer::Update(const std::function<void(ISensorWriter*)>& callback)
{
TSensorBuffer buffer;
callback(&buffer);
Update(std::move(buffer));
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT::NProfiling
|