-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathPrometheusMetricsWriter.cpp
188 lines (156 loc) · 6.71 KB
/
PrometheusMetricsWriter.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "PrometheusMetricsWriter.h"
#include <algorithm>
#include <IO/WriteHelpers.h>
#include <Common/StatusInfo.h>
#include <Storages/ExternalStream/StorageExternalStream.h>
#include <regex>
namespace
{
template <typename T>
void writeOutLine(DB::WriteBuffer & wb, T && val)
{
DB::writeText(std::forward<T>(val), wb);
DB::writeChar('\n', wb);
}
template <typename T, typename... TArgs>
void writeOutLine(DB::WriteBuffer & wb, T && val, TArgs &&... args)
{
DB::writeText(std::forward<T>(val), wb);
DB::writeChar(' ', wb);
writeOutLine(wb, std::forward<TArgs>(args)...);
}
/// Returns false if name is not valid
bool replaceInvalidChars(std::string & metric_name)
{
/// dirty solution
metric_name = std::regex_replace(metric_name, std::regex("[^a-zA-Z0-9_:]"), "_");
metric_name = std::regex_replace(metric_name, std::regex("^[^a-zA-Z]*"), "");
return !metric_name.empty();
}
}
namespace DB
{
PrometheusMetricsWriter::PrometheusMetricsWriter(
const Poco::Util::AbstractConfiguration & config, const std::string & config_name,
const AsynchronousMetrics & async_metrics_, ContextPtr context_)
: async_metrics(async_metrics_)
, context(context_)
, send_events(config.getBool(config_name + ".events", true))
, send_metrics(config.getBool(config_name + ".metrics", true))
, send_asynchronous_metrics(config.getBool(config_name + ".asynchronous_metrics", true))
, send_status_info(config.getBool(config_name + ".status_info", true))
, send_external_stream(config.getBool(config_name + ".external_stream", true))
{
}
void PrometheusMetricsWriter::write(WriteBuffer & wb) const
{
if (send_events)
{
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
{
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
std::string metric_name{ProfileEvents::getName(static_cast<ProfileEvents::Event>(i))};
std::string metric_doc{ProfileEvents::getDocumentation(static_cast<ProfileEvents::Event>(i))};
if (!replaceInvalidChars(metric_name))
continue;
std::string key{profile_events_prefix + metric_name};
writeOutLine(wb, "# HELP", key, metric_doc);
writeOutLine(wb, "# TYPE", key, "counter");
writeOutLine(wb, key, counter);
}
}
if (send_metrics)
{
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
{
const auto value = CurrentMetrics::values[i].load(std::memory_order_relaxed);
std::string metric_name{CurrentMetrics::getName(static_cast<CurrentMetrics::Metric>(i))};
std::string metric_doc{CurrentMetrics::getDocumentation(static_cast<CurrentMetrics::Metric>(i))};
if (!replaceInvalidChars(metric_name))
continue;
std::string key{current_metrics_prefix + metric_name};
writeOutLine(wb, "# HELP", key, metric_doc);
writeOutLine(wb, "# TYPE", key, "gauge");
writeOutLine(wb, key, value);
}
}
if (send_asynchronous_metrics)
{
auto async_metrics_values = async_metrics.getValues();
for (const auto & name_value : async_metrics_values)
{
std::string key{asynchronous_metrics_prefix + name_value.first};
if (!replaceInvalidChars(key))
continue;
auto value = name_value.second;
// TODO: add HELP section? asynchronous_metrics contains only key and value
writeOutLine(wb, "# TYPE", key, "gauge");
writeOutLine(wb, key, value);
}
}
if (send_status_info)
{
for (size_t i = 0, end = CurrentStatusInfo::end(); i < end; ++i)
{
std::lock_guard<std::mutex> lock(CurrentStatusInfo::locks[static_cast<CurrentStatusInfo::Status>(i)]);
std::string metric_name{CurrentStatusInfo::getName(static_cast<CurrentStatusInfo::Status>(i))};
std::string metric_doc{CurrentStatusInfo::getDocumentation(static_cast<CurrentStatusInfo::Status>(i))};
if (!replaceInvalidChars(metric_name))
continue;
std::string key{current_status_prefix + metric_name};
writeOutLine(wb, "# HELP", key, metric_doc);
writeOutLine(wb, "# TYPE", key, "gauge");
for (const auto & value: CurrentStatusInfo::values[i])
{
for (const auto & enum_value: CurrentStatusInfo::getAllPossibleValues(static_cast<CurrentStatusInfo::Status>(i)))
{
DB::writeText(key, wb);
DB::writeChar('{', wb);
DB::writeText(key, wb);
DB::writeChar('=', wb);
writeDoubleQuotedString(enum_value.first, wb);
DB::writeText(",name=", wb);
writeDoubleQuotedString(value.first, wb);
DB::writeText("} ", wb);
DB::writeText(value.second == enum_value.second, wb);
DB::writeChar('\n', wb);
}
}
}
}
if (send_external_stream)
{
auto databases = DatabaseCatalog::instance().getDatabases();
for (const auto & [_, database] : databases)
{
for (auto table_it = database->getTablesIterator(context); table_it->isValid(); table_it->next())
{
StoragePtr table_ptr = table_it->table();
if(auto * external_stream = table_ptr->as<StorageExternalStream>())
{
const auto & storage_id = external_stream->getStorageID();
String database_name = storage_id.getDatabaseName();
String table_name = storage_id.getTableName();
auto external_stream_counter = external_stream->getExternalStreamCounter();
if (!external_stream_counter)
continue;
const auto & counters = external_stream_counter->getCounters();
for (const auto & [metric_name, value] : counters)
{
std::string key{external_stream_prefix + metric_name};
writeOutLine(wb, "# TYPE", key, "counter");
DB::writeText(key, wb);
DB::writeText("{database=\"", wb);
DB::writeText(database_name, wb);
DB::writeText("\", name=\"", wb);
DB::writeText(table_name, wb);
DB::writeText("\"} ", wb);
DB::writeIntText(value, wb);
DB::writeChar('\n', wb);
}
}
}
}
}
}
}