blob: 223a4448a896d0643e9647b4aca029842cc2236c (
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
|
#include "page.h"
#include "encode.h"
#include <library/cpp/monlib/service/pages/templates.h>
#include <library/cpp/string_utils/quote/quote.h>
#include <util/string/split.h>
#include <util/system/tls.h>
using namespace NMonitoring;
namespace {
Y_POD_STATIC_THREAD(TDynamicCounters*)
currentCounters(nullptr);
}
TMaybe<EFormat> ParseFormat(TStringBuf str) {
if (str == TStringBuf("json")) {
return EFormat::JSON;
} else if (str == TStringBuf("spack")) {
return EFormat::SPACK;
} else if (str == TStringBuf("prometheus")) {
return EFormat::PROMETHEUS;
} else {
return Nothing();
}
}
void TDynamicCountersPage::Output(NMonitoring::IMonHttpRequest& request) {
if (OutputCallback) {
OutputCallback();
}
TString nameLabel("sensor");
TCountableBase::EVisibility visibility{
TCountableBase::EVisibility::Public
};
TVector<TStringBuf> parts;
StringSplitter(request.GetPathInfo())
.Split('/')
.SkipEmpty()
.Collect(&parts);
TMaybe<EFormat> format = !parts.empty() ? ParseFormat(parts.back()) : Nothing();
if (format) {
parts.pop_back();
}
if (!parts.empty() && parts.back().StartsWith(TStringBuf("name_label="))) {
TVector<TString> labels;
StringSplitter(parts.back()).Split('=').SkipEmpty().Collect(&labels);
if (labels.size() == 2U) {
nameLabel = labels.back();
}
parts.pop_back();
}
if (!parts.empty() && parts.back() == TStringBuf("private")) {
visibility = TCountableBase::EVisibility::Private;
parts.pop_back();
}
auto counters = Counters;
for (const auto& escaped : parts) {
const auto part = CGIUnescapeRet(escaped);
TVector<TString> labels;
StringSplitter(part).Split('=').SkipEmpty().Collect(&labels);
if (labels.size() != 2U)
return NotFound(request);
if (const auto child = counters->FindSubgroup(
labels.front(),
labels.back())) {
counters = child;
} else {
return HandleAbsentSubgroup(request);
}
}
if (!format) {
currentCounters = counters.Get();
THtmlMonPage::Output(request);
currentCounters = nullptr;
return;
}
IOutputStream& out = request.Output();
if (*format == EFormat::JSON) {
out << HTTPOKJSON;
} else if (*format == EFormat::SPACK) {
out << HTTPOKSPACK;
} else if (*format == EFormat::PROMETHEUS) {
out << HTTPOKPROMETHEUS;
} else {
ythrow yexception() << "unsupported metric encoding format: " << *format;
}
auto encoder = CreateEncoder(&out, *format, nameLabel, visibility);
counters->Accept(TString(), TString(), *encoder);
out.Flush();
}
void TDynamicCountersPage::HandleAbsentSubgroup(IMonHttpRequest& request) {
if (UnknownGroupPolicy == EUnknownGroupPolicy::Error) {
NotFound(request);
} else if (UnknownGroupPolicy == EUnknownGroupPolicy::Ignore) {
NoContent(request);
} else {
Y_FAIL("Unsupported policy set");
}
}
void TDynamicCountersPage::BeforePre(IMonHttpRequest& request) {
IOutputStream& out = request.Output();
HTML(out) {
DIV() {
out << "<a href='" << request.GetPath() << "/json'>Counters as JSON</a>";
out << " for <a href='https://wiki.yandex-team.ru/solomon/'>Solomon</a>";
}
TAG(TH5) {
out << "Counters subgroups";
}
UL() {
currentCounters->EnumerateSubgroups([&](const TString& name, const TString& value) {
LI() {
TString pathPart = name + "=" + value;
Quote(pathPart, "");
out << "\n<a href='" << request.GetPath() << "/" << pathPart << "'>" << name << " " << value << "</a>";
}
});
}
TAG(TH4) {
out << "Counters as text";
}
}
}
void TDynamicCountersPage::OutputText(IOutputStream& out, IMonHttpRequest&) {
currentCounters->OutputPlainText(out);
}
void TDynamicCountersPage::SetUnknownGroupPolicy(EUnknownGroupPolicy value) {
UnknownGroupPolicy = value;
}
|