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
|
/*
* This file is part of PowerDNS or weakforced.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 3 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "prometheus.hh"
static std::shared_ptr<PrometheusMetrics> prom_metrics;
void initPrometheusMetrics(std::shared_ptr<PrometheusMetrics> pmp)
{
prom_metrics = pmp;
}
void PrometheusMetrics::addCommandMetric(const std::string& name)
{
Counter* c = &(commands_family->Add({{"cmd", name}}));
commands_metrics.insert(std::make_pair(name, c));
}
void PrometheusMetrics::incCommandMetric(const std::string& name)
{
auto i = commands_metrics.find(name);
if (i != commands_metrics.end()) {
i->second->Increment();
}
}
void PrometheusMetrics::addCustomMetric(const std::string& name)
{
Counter* c = &(custom_family->Add({{"metric", name}}));
custom_metrics.insert(std::make_pair(name, c));
}
void PrometheusMetrics::incCustomMetric(const std::string& name)
{
auto i = custom_metrics.find(name);
if (i != custom_metrics.end()) {
i->second->Increment();
}
}
void PrometheusMetrics::addDNSResolverMetric(const std::string& name)
{
Counter* c = &(dns_query_family->Add({{"resolver", name}}));
dns_query_metrics.insert(std::make_pair(name, c));
Histogram* h = &(dns_query_latency_family->Add({{"resolver", name}}, d_bb));
dns_query_latency_metrics.insert(std::make_pair(name, h));
}
void PrometheusMetrics::incDNSResolverMetric(const std::string& name)
{
auto i = dns_query_metrics.find(name);
if (i != dns_query_metrics.end()) {
i->second->Increment();
}
}
void PrometheusMetrics::observeDNSResolverLatency(const std::string& name, float duration)
{
auto i = dns_query_latency_metrics.find(name);
if (i != dns_query_latency_metrics.end()) {
i->second->Observe(duration);
}
}
void PrometheusMetrics::addWebhookMetric(unsigned int id, const std::string& url, bool custom)
{
std::string success_key = std::to_string(id) + "ok";
std::string error_key = std::to_string(id) + "error";
if (!custom) {
Counter* c = &(webhook_family->Add({{"id", std::to_string(id)}, {"url", url}, {"status", "ok"}}));
webhook_metrics.insert(std::make_pair(success_key, c));
c = &(webhook_family->Add({{"id", std::to_string(id)}, {"url", url}, {"status", "error"}}));
webhook_metrics.insert(std::make_pair(error_key, c));
}
else {
Counter* c = &(custom_webhook_family->Add({{"id", std::to_string(id)}, {"url", url}, {"status", "ok"}}));
custom_webhook_metrics.insert(std::make_pair(success_key, c));
c = &(custom_webhook_family->Add({{"id", std::to_string(id)}, {"url", url}, {"status", "error"}}));
custom_webhook_metrics.insert(std::make_pair(error_key, c));
}
}
void PrometheusMetrics::incWebhookMetric(unsigned int id, bool success, bool custom)
{
std::string key = std::to_string(id) + (success ? "ok" : "error");
if (!custom) {
auto i = webhook_metrics.find(key);
if (i != webhook_metrics.end()) {
i->second->Increment();
}
}
else {
auto i = custom_webhook_metrics.find(key);
if (i != custom_webhook_metrics.end()) {
i->second->Increment();
}
}
}
void setPrometheusActiveConns(size_t value)
{
if (prom_metrics)
prom_metrics->setActiveConns(value);
}
void addPrometheusCommandMetric(const std::string& name)
{
if (prom_metrics)
prom_metrics->addCommandMetric(name);
}
void incPrometheusCommandMetric(const std::string& name)
{
if (prom_metrics)
prom_metrics->incCommandMetric(name);
}
void addPrometheusCustomMetric(const std::string& name)
{
if (prom_metrics)
prom_metrics->addCustomMetric(name);
}
void incPrometheusCustomMetric(const std::string& name)
{
if (prom_metrics)
prom_metrics->incCustomMetric(name);
}
void observePrometheusWQD(float duration)
{
if (prom_metrics)
prom_metrics->observeWQD(duration);
}
void observePrometheusWRD(float duration)
{
if (prom_metrics)
prom_metrics->observeWRD(duration);
}
void addPrometheusDNSResolverMetric(const std::string& name)
{
if (prom_metrics) {
prom_metrics->addDNSResolverMetric(name);
}
}
void incPrometheusDNSResolverMetric(const std::string& name)
{
if (prom_metrics) {
prom_metrics->incDNSResolverMetric(name);
}
}
void observePrometheusDNSResolverLatency(const std::string& name, float duration)
{
if (prom_metrics) {
prom_metrics->observeDNSResolverLatency(name, duration);
}
}
void addPrometheusWebhookMetric(unsigned int id, const std::string& url, bool custom)
{
if (prom_metrics) {
prom_metrics->addWebhookMetric(id, url, custom);
}
}
void incPrometheusWebhookMetric(unsigned int id, bool success, bool custom)
{
if (prom_metrics) {
prom_metrics->incWebhookMetric(id, success, custom);
}
}
void setPrometheusWebQueueSize(int value)
{
if (prom_metrics) {
prom_metrics->setWebQueueSize(value);
}
}
void setPrometheusWebhookQueueSize(int value)
{
if (prom_metrics) {
prom_metrics->setWebhookQueueSize(value);
}
}
std::string serializePrometheusMetrics()
{
if (prom_metrics)
return prom_metrics->serialize();
else
return std::string("Prometheus Metrics not initialized");
}
|