Loading...
Searching...
No Matches
prometheus_handler.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24namespace resdb {
25
26std::map<TableName, std::string> table_names = {
27 {GENERAL, "general"},
28 {WORKER_THREAD, "worker_thread"},
29 {IO_THREAD, "io_thread"},
30 {CLIENT, "client"},
31 {CONFIGURATION, "configuration"},
32 {SERVER, "server"},
33 {CONSENSUS, "consensus"}};
34
35std::map<MetricName, std::pair<TableName, std::string>> metric_names = {
36 {SERVER_CALL_NAME, {SERVER, "server_call"}},
37 {SERVER_PROCESS, {SERVER, "server_process"}},
38 {CLIENT_CALL, {CLIENT, "client_call"}},
39 {CLIENT_REQ, {CLIENT, "client_req"}},
40 {SOCKET_RECV, {IO_THREAD, "socket_recv"}},
41 {BROAD_CAST, {IO_THREAD, "broad_cast"}},
42 {PROPOSE, {CONSENSUS, "propose"}},
43 {PREPARE, {CONSENSUS, "prepare"}},
44 {COMMIT, {CONSENSUS, "commit"}},
45 {EXECUTE, {CONSENSUS, "execute"}},
46 {NUM_EXECUTE_TX, {CONSENSUS, "num_execute_tx"}}};
47
48PrometheusHandler::PrometheusHandler(const std::string& server_address) {
49 exposer_ =
50 prometheus::detail::make_unique<prometheus::Exposer>(server_address);
51
52 registry_ = std::make_shared<prometheus::Registry>();
53 exposer_->RegisterCollectable(registry_);
54
55 Register();
56}
57
59 if (exposer_) {
60 exposer_->RemoveCollectable(registry_);
61 }
62}
63
65 for (auto& table : table_names) {
66 RegisterTable(table.second);
67 }
68
69 for (auto& metric_pair : metric_names) {
70 RegisterMetric(table_names[metric_pair.second.first],
71 metric_pair.second.second);
72 }
73}
74
75void PrometheusHandler::RegisterTable(const std::string& name) {
76 gbuilder* gauge = &prometheus::BuildGauge()
77 .Name(name)
78 .Help(name + " metrics")
79 .Register(*registry_);
80 gauge_[name] = gauge;
81}
82
83void PrometheusHandler::RegisterMetric(const std::string& table_name,
84 const std::string& metric_name) {
85 if (gauge_.find(table_name) == gauge_.end()) {
86 return;
87 }
88 gmetric* metric = &gauge_[table_name]->Add({{"metrics", metric_name}});
89 metric_[metric_name] = metric;
90}
91
92void PrometheusHandler::Set(MetricName name, double value) {
93 std::string metric_name_str = metric_names[name].second;
94 if (metric_.find(metric_name_str) == metric_.end()) {
95 return;
96 }
97 metric_[metric_name_str]->Set(value);
98}
99
100void PrometheusHandler::Inc(MetricName name, double value) {
101 std::string metric_name_str = metric_names[name].second;
102 if (metric_.find(metric_name_str) == metric_.end()) {
103 return;
104 }
105 metric_[metric_name_str]->Increment(value);
106}
107
108} // namespace resdb
void Inc(MetricName name, double value)
PrometheusHandler(const std::string &server_address)
void RegisterMetric(const std::string &table_name, const std::string &metric_name)
void Set(MetricName name, double value)
void RegisterTable(const std::string &name)
std::map< MetricName, std::pair< TableName, std::string > > metric_names
std::map< TableName, std::string > table_names