Skip to content

Commit 178e735

Browse files
author
xenoxeno
committed
refactor counters page to better support ic-proxy
commit_hash:d6538967a151a3aba3c806d3d1ba3f4c7ef1ae47
1 parent a2a5a3c commit 178e735

File tree

1 file changed

+68
-23
lines changed
  • library/cpp/monlib/dynamic_counters

1 file changed

+68
-23
lines changed

library/cpp/monlib/dynamic_counters/page.cpp

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <library/cpp/monlib/service/pages/templates.h>
55
#include <library/cpp/string_utils/quote/quote.h>
66

7+
#include <util/string/builder.h>
78
#include <util/string/split.h>
89
#include <util/system/tls.h>
910

@@ -26,6 +27,19 @@ TMaybe<EFormat> ParseFormat(TStringBuf str) {
2627
}
2728
}
2829

30+
namespace {
31+
32+
TStringBuf GetParams(NMonitoring::IMonHttpRequest& request) {
33+
TStringBuf uri = request.GetUri();
34+
TStringBuf params = uri.After('?');
35+
if (params.Size() == uri.Size()) {
36+
params.Clear();
37+
}
38+
return params;
39+
}
40+
41+
}
42+
2943
void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
3044
if (OutputCallback) {
3145
OutputCallback();
@@ -37,28 +51,51 @@ void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
3751
};
3852

3953
TVector<TStringBuf> parts;
40-
StringSplitter(request.GetPathInfo())
41-
.Split('/')
42-
.SkipEmpty()
43-
.Collect(&parts);
44-
45-
TMaybe<EFormat> format = !parts.empty() ? ParseFormat(parts.back()) : Nothing();
46-
if (format) {
47-
parts.pop_back();
48-
}
54+
TMaybe<EFormat> format;
55+
TStringBuf params = GetParams(request);
56+
57+
if (request.GetPathInfo().empty() && !params.empty()) {
58+
StringSplitter(params).Split('&').SkipEmpty().Consume([&](TStringBuf part) {
59+
TStringBuf name;
60+
TStringBuf value;
61+
part.Split('=', name, value);
62+
if (name.StartsWith("@")) {
63+
if (name == "@format") {
64+
format = ParseFormat(value);
65+
} else if (name == "@name_label") {
66+
nameLabel = value;
67+
} else if (name == "@private") {
68+
visibility = TCountableBase::EVisibility::Private;
69+
}
70+
} else {
71+
parts.push_back(part);
72+
}
73+
return true;
74+
});
75+
} else {
76+
StringSplitter(request.GetPathInfo())
77+
.Split('/')
78+
.SkipEmpty()
79+
.Collect(&parts);
80+
81+
format = !parts.empty() ? ParseFormat(parts.back()) : Nothing();
82+
if (format) {
83+
parts.pop_back();
84+
}
4985

50-
if (!parts.empty() && parts.back().StartsWith(TStringBuf("name_label="))) {
51-
TVector<TString> labels;
52-
StringSplitter(parts.back()).Split('=').SkipEmpty().Collect(&labels);
53-
if (labels.size() == 2U) {
54-
nameLabel = labels.back();
86+
if (!parts.empty() && parts.back().StartsWith(TStringBuf("name_label="))) {
87+
TVector<TString> labels;
88+
StringSplitter(parts.back()).Split('=').SkipEmpty().Collect(&labels);
89+
if (labels.size() == 2U) {
90+
nameLabel = labels.back();
91+
}
92+
parts.pop_back();
5593
}
56-
parts.pop_back();
57-
}
5894

59-
if (!parts.empty() && parts.back() == TStringBuf("private")) {
60-
visibility = TCountableBase::EVisibility::Private;
61-
parts.pop_back();
95+
if (!parts.empty() && parts.back() == TStringBuf("private")) {
96+
visibility = TCountableBase::EVisibility::Private;
97+
parts.pop_back();
98+
}
6299
}
63100

64101
auto counters = Counters;
@@ -121,9 +158,15 @@ void TDynamicCountersPage::HandleAbsentSubgroup(IMonHttpRequest& request) {
121158

122159
void TDynamicCountersPage::BeforePre(IMonHttpRequest& request) {
123160
IOutputStream& out = request.Output();
161+
TStringBuf params = GetParams(request);
162+
TStringBuilder base;
163+
base << Path << '?';
164+
if (!params.empty()) {
165+
base << params << '&';
166+
}
124167
HTML(out) {
125168
DIV() {
126-
out << "<a href='" << request.GetPath() << "/json'>Counters as JSON</a>";
169+
out << "<a href='" << base << "@format=json'>Counters as JSON</a>";
127170
out << " for Solomon";
128171
}
129172

@@ -133,9 +176,11 @@ void TDynamicCountersPage::BeforePre(IMonHttpRequest& request) {
133176
UL() {
134177
currentCounters->EnumerateSubgroups([&](const TString& name, const TString& value) {
135178
LI() {
136-
TString pathPart = name + "=" + value;
137-
Quote(pathPart, "");
138-
out << "\n<a href='" << request.GetPath() << "/" << pathPart << "'>" << name << " " << value << "</a>";
179+
auto escName = name;
180+
auto escValue = value;
181+
Quote(escName);
182+
Quote(escValue);
183+
out << "\n<a href='" << base << escName << '=' << escValue << "'>" << name << " " << value << "</a>";
139184
}
140185
});
141186
}

0 commit comments

Comments
 (0)