forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.hpp
225 lines (181 loc) · 7.6 KB
/
metrics.hpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#pragma once
#ifdef FEATURE_METRICS
#include "game/scheduling/dispatcher.hpp"
#include <opentelemetry/exporters/ostream/metric_exporter_factory.h>
#include <opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_factory.h>
#include <opentelemetry/exporters/prometheus/exporter_options.h>
#include <opentelemetry/metrics/provider.h>
#include <opentelemetry/sdk/metrics/aggregation/default_aggregation.h>
#include <opentelemetry/sdk/metrics/aggregation/histogram_aggregation.h>
#include <opentelemetry/sdk/metrics/push_metric_exporter.h>
#include <opentelemetry/sdk/metrics/aggregation/base2_exponential_histogram_indexer.h>
#include <opentelemetry/sdk/metrics/meter.h>
#include <opentelemetry/sdk/metrics/meter_provider.h>
#include <opentelemetry/sdk/metrics/meter_provider_factory.h>
#include <opentelemetry/sdk/metrics/view/instrument_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/meter_selector_factory.h>
#include <opentelemetry/sdk/metrics/view/view_factory.h>
namespace metrics_sdk = opentelemetry::sdk::metrics;
namespace common = opentelemetry::common;
namespace metrics_exporter = opentelemetry::exporter::metrics;
namespace metrics_api = opentelemetry::metrics;
namespace metrics {
using Meter = opentelemetry::nostd::shared_ptr<metrics_api::Meter>;
template <typename T>
using Histogram = opentelemetry::nostd::unique_ptr<metrics_api::Histogram<T>>;
template <typename T>
using Counter = opentelemetry::nostd::unique_ptr<metrics_api::Counter<T>>;
template <typename T>
using UpDownCounter = opentelemetry::nostd::unique_ptr<metrics_api::UpDownCounter<T>>;
struct Options {
bool enablePrometheusExporter;
bool enableOStreamExporter;
metrics_sdk::PeriodicExportingMetricReaderOptions ostreamOptions;
metrics_exporter::PrometheusExporterOptions prometheusOptions;
};
class ScopedLatency {
public:
explicit ScopedLatency(const std::string_view &name, const std::string &histogramName, const std::string &scopeKey);
explicit ScopedLatency([[maybe_unused]] const std::string_view &name, Histogram<double> &histogram, std::map<std::string, std::string> attrs = {}, opentelemetry::context::Context context = opentelemetry::context::Context()) :
begin(std::chrono::steady_clock::now()), histogram(histogram), attrs(std::move(attrs)), context(std::move(context)) {
}
void stop();
~ScopedLatency();
private:
std::chrono::steady_clock::time_point begin;
Histogram<double> &histogram;
std::map<std::string, std::string> attrs;
opentelemetry::context::Context context;
bool stopped { false };
};
#define DEFINE_LATENCY_CLASS(class_name, histogram_name, category) \
class class_name##_latency final : public ScopedLatency { \
public: \
class_name##_latency(std::string_view name) : \
ScopedLatency(name, histogram_name "_latency", category) { } \
}
DEFINE_LATENCY_CLASS(method, "method", "method");
DEFINE_LATENCY_CLASS(lua, "lua", "scope");
DEFINE_LATENCY_CLASS(query, "query", "truncated_query");
DEFINE_LATENCY_CLASS(task, "task", "task");
DEFINE_LATENCY_CLASS(lock, "lock", "scope");
const std::vector<std::string> latencyNames {
"method_latency",
"lua_latency",
"query_latency",
"task_latency",
"lock_latency",
};
class Metrics final {
public:
Metrics() = default;
~Metrics() = default;
void init(Options opts);
void initHistograms();
void shutdown();
static Metrics &getInstance();
void addCounter(std::string_view name, double value, std::map<std::string, std::string> attrs = {}) {
std::scoped_lock lock(mutex_);
if (!getMeter()) {
return;
}
if (counters.find(name) == counters.end()) {
std::string nameStr(name);
counters[name] = getMeter()->CreateDoubleCounter(nameStr);
}
auto attrskv = opentelemetry::common::KeyValueIterableView<decltype(attrs)> { attrs };
counters[name]->Add(value, attrskv);
}
void addUpDownCounter(std::string_view name, int value, std::map<std::string, std::string> attrs = {}) {
std::scoped_lock lock(mutex_);
if (!getMeter()) {
return;
}
if (upDownCounters.find(name) == upDownCounters.end()) {
std::string nameStr(name);
upDownCounters[name] = getMeter()->CreateInt64UpDownCounter(nameStr);
}
auto attrskv = opentelemetry::common::KeyValueIterableView<decltype(attrs)> { attrs };
upDownCounters[name]->Add(value, attrskv);
}
friend class ScopedLatency;
protected:
opentelemetry::context::Context defaultContext {};
phmap::parallel_flat_hash_map<std::string, Histogram<double>> latencyHistograms;
phmap::flat_hash_map<std::string, UpDownCounter<int64_t>> upDownCounters;
phmap::flat_hash_map<std::string, Counter<double>> counters;
Meter getMeter() {
auto provider = metrics_api::Provider::GetMeterProvider();
if (provider == nullptr) {
return {};
}
return provider->GetMeter(meterName, otelVersion);
}
private:
std::mutex mutex_;
std::string meterName { "stats" };
std::string otelVersion { "1.2.0" };
std::string otelSchema { "https://opentelemetry.io/schemas/1.2.0" };
};
}
constexpr auto g_metrics
= metrics::Metrics::getInstance;
#else // FEATURE_METRICS
#include "lib/di/container.hpp"
struct Options {
bool enablePrometheusExporter;
bool enableOStreamExporter;
};
class ScopedLatency {
public:
explicit ScopedLatency([[maybe_unused]] std::string_view name, [[maybe_unused]] const std::string &histogramName, [[maybe_unused]] const std::string &scopeKey) {};
explicit ScopedLatency([[maybe_unused]] std::string_view name, [[maybe_unused]] std::set<double> &histogram, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}, [[maybe_unused]] const std::string &context = std::string()) {};
void stop() {};
~ScopedLatency() = default;
};
namespace metrics {
#define DEFINE_LATENCY_CLASS(class_name, histogram_name, category) \
class class_name##_latency final : public ScopedLatency { \
public: \
class_name##_latency(std::string_view name) : \
ScopedLatency(name, histogram_name "_latency", category) { } \
}
DEFINE_LATENCY_CLASS(method, "method", "method");
DEFINE_LATENCY_CLASS(lua, "lua", "scope");
DEFINE_LATENCY_CLASS(query, "query", "truncated_query");
DEFINE_LATENCY_CLASS(task, "task", "task");
DEFINE_LATENCY_CLASS(lock, "lock", "scope");
const std::vector<std::string> latencyNames {
"method_latency",
"lua_latency",
"query_latency",
"task_latency",
"lock_latency",
};
class Metrics final {
public:
Metrics() = default;
~Metrics() = default;
void init([[maybe_unused]] Options opts) {};
void initHistograms() {};
void shutdown() {};
static Metrics &getInstance() {
return inject<Metrics>();
};
void addCounter([[maybe_unused]] std::string_view name, [[maybe_unused]] double value, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}) { }
void addUpDownCounter([[maybe_unused]] std::string_view name, [[maybe_unused]] int value, [[maybe_unused]] const std::map<std::string, std::string> &attrs = {}) { }
friend class ScopedLatency;
};
}
constexpr auto g_metrics
= metrics::Metrics::getInstance;
#endif // FEATURE_METRICS