@@ -10,138 +10,48 @@ namespace NYT::NProfiling {
10
10
11
11
// //////////////////////////////////////////////////////////////////////////////
12
12
13
- namespace {
14
-
15
- static constexpr int MaxLabelSize = 200 ;
16
- static constexpr int HalfMaxLabelSize = MaxLabelSize / 2 ;
17
-
18
- struct TSanitizeParameters
19
- {
20
- int ForbiddenCharCount;
21
- int ResultingLength;
22
-
23
- bool IsSanitizationRequired () const
24
- {
25
- return ForbiddenCharCount > 0 || ResultingLength > MaxLabelSize;
26
- }
27
- };
28
-
29
- bool IsAllowedMonitoringTagValueChar (unsigned char c)
30
- {
31
- return 31 < c &&
32
- c < 127 &&
33
- c != ' |' &&
34
- c != ' *' &&
35
- c != ' ?' &&
36
- c != ' "' &&
37
- c != ' \' ' &&
38
- c != ' \\ ' &&
39
- c != ' `' ;
40
- }
41
-
42
- TSanitizeParameters ScanForSanitize (const std::string& value)
43
- {
44
- int forbiddenCharCount = 0 ;
45
- for (unsigned char c : value) {
46
- forbiddenCharCount += static_cast <int >(!IsAllowedMonitoringTagValueChar (c));
47
- }
48
-
49
- return {
50
- .ForbiddenCharCount = forbiddenCharCount,
51
- .ResultingLength = static_cast <int >(value.size () + forbiddenCharCount * 2 ),
52
- };
53
- }
54
-
55
- std::string SanitizeMonitoringTagValue (const std::string& value, int resultingLength)
13
+ TTagIdList TTagRegistry::Encode (const TTagList& tags)
56
14
{
57
- bool needTrim = resultingLength > MaxLabelSize;
58
-
59
- std::string result;
60
- result.resize (std::min (resultingLength, MaxLabelSize));
61
-
62
- int resultIndex = 0 ;
63
- for (int index = 0 ; resultIndex < (needTrim ? HalfMaxLabelSize : resultingLength); ++index) {
64
- unsigned char c = value[index];
65
-
66
- if (IsAllowedMonitoringTagValueChar (value[index])) {
67
- result[resultIndex++] = c;
68
- } else {
69
- result[resultIndex++] = ' %' ;
70
- result[resultIndex++] = IntToHexLowercase[c >> 4 ];
71
- result[resultIndex++] = IntToHexLowercase[c & 0x0f ];
72
- }
73
- }
74
-
75
- if (!needTrim) {
76
- return result;
77
- }
78
-
79
- resultIndex = MaxLabelSize - 1 ;
80
- for (int index = ssize (value) - 1 ; resultIndex > HalfMaxLabelSize + 2 ; --index) {
81
- unsigned char c = value[index];
15
+ TTagIdList ids;
82
16
83
- if (IsAllowedMonitoringTagValueChar (value[index])) {
84
- result[resultIndex--] = c;
17
+ for (const auto & tag : tags) {
18
+ if (auto it = TagByName_.find (tag); it != TagByName_.end ()) {
19
+ ids.push_back (it->second );
85
20
} else {
86
- result[resultIndex--] = IntToHexLowercase[c & 0x0f ] ;
87
- result[resultIndex-- ] = IntToHexLowercase[c >> 4 ] ;
88
- result[resultIndex--] = ' % ' ;
21
+ TagById_. push_back (tag) ;
22
+ TagByName_[tag ] = TagById_. size () ;
23
+ ids. push_back (TagById_. size ()) ;
89
24
}
90
25
}
91
26
92
- result[HalfMaxLabelSize] = ' .' ;
93
- result[HalfMaxLabelSize + 1 ] = ' .' ;
94
- result[HalfMaxLabelSize + 2 ] = ' .' ;
95
-
96
- return result;
27
+ return ids;
97
28
}
98
29
99
- TTag SanitizeMonitoringTag (const TTag& tag, int resultingLength )
30
+ TTagId TTagRegistry::Encode (const TTag& tag)
100
31
{
101
- return {tag.first , SanitizeMonitoringTagValue (tag.second , resultingLength)};
32
+ if (auto it = TagByName_.find (tag); it != TagByName_.end ()) {
33
+ return it->second ;
34
+ } else {
35
+ TagById_.push_back (tag);
36
+ TagByName_[tag] = TagById_.size ();
37
+ return TagById_.size ();
38
+ }
102
39
}
103
40
104
- } // namespace
105
-
106
- // //////////////////////////////////////////////////////////////////////////////
107
-
108
41
TTagIdList TTagRegistry::Encode (const TTagSet& tags)
109
42
{
110
43
return Encode (tags.Tags ());
111
44
}
112
45
113
- TTagIdList TTagRegistry::Encode (const TTagList& tags)
114
- {
115
- TTagIdList ids;
116
- for (const auto & tag : tags) {
117
- ids.push_back (Encode (tag));
118
- }
119
-
120
- return ids;
121
- }
122
-
123
- TTagId TTagRegistry::Encode (const TTag& tag)
124
- {
125
- if (auto sanitizeParameters = ScanForSanitize (tag.second );
126
- sanitizeParameters.IsSanitizationRequired ())
127
- {
128
- return EncodeSanitized (SanitizeMonitoringTag (tag, sanitizeParameters.ResultingLength ));
129
- } else {
130
- return EncodeSanitized (tag);
131
- }
132
- }
133
-
134
46
TCompactVector<std::optional<TTagId>, TypicalTagCount> TTagRegistry::TryEncode (const TTagList& tags) const
135
47
{
136
48
TCompactVector<std::optional<TTagId>, TypicalTagCount> ids;
137
49
138
50
for (const auto & tag : tags) {
139
- if (auto sanitizeParameters = ScanForSanitize (tag.second );
140
- sanitizeParameters.IsSanitizationRequired ())
141
- {
142
- ids.push_back (TryEncodeSanitized (SanitizeMonitoringTag (tag, sanitizeParameters.ResultingLength )));
51
+ if (auto it = TagByName_.find (tag); it != TagByName_.end ()) {
52
+ ids.push_back (it->second );
143
53
} else {
144
- ids.push_back (TryEncodeSanitized (tag) );
54
+ ids.push_back ({} );
145
55
}
146
56
}
147
57
@@ -183,15 +93,6 @@ void TTagRegistry::DumpTags(NProto::TSensorDump* dump)
183
93
}
184
94
}
185
95
186
- std::optional<TTagId> TTagRegistry::TryEncodeSanitized (const TTag& tag) const
187
- {
188
- if (auto it = TagByName_.find (tag); it != TagByName_.end ()) {
189
- return it->second ;
190
- } else {
191
- return std::nullopt;
192
- }
193
- }
194
-
195
96
// //////////////////////////////////////////////////////////////////////////////
196
97
197
98
void TTagWriter::WriteLabel (TTagId tag)
0 commit comments