Skip to content

Commit 8022fb5

Browse files
authored
Merge pull request #105 from jupp0r/replace-protobuf
Remove protobuf dependency This change removes the protobuf wire format, as it's no longer supported in prometheus 2.0. It uses this as an opportunity to remove protobuf as an external dependency all together, which will make integration into existing code bases that already use protobuf much more palatable.
2 parents a97127c + 151dcf8 commit 8022fb5

36 files changed

+319
-403
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
[submodule "civetweb"]
55
path = 3rdparty/civetweb
66
url = https://github.com/civetweb/civetweb.git
7-
[submodule "prometheus_client_model"]
8-
path = 3rdparty/prometheus_client_model
9-
url = https://github.com/prometheus/client_model.git

.travis.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ install:
1717
- sudo apt-get update
1818
- sudo apt-get install -y gcc-5 g++-5
1919
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 1 --slave /usr/bin/g++ g++ /usr/bin/g++-5
20-
- wget https://github.com/google/protobuf/archive/v3.1.0.tar.gz
21-
- tar xzf v3.1.0.tar.gz
22-
- cd protobuf-3.1.0
23-
- ./autogen.sh
24-
- ./configure --prefix=/usr
25-
- make -j 4
26-
- sudo make install
27-
- cd ..
2820
- sudo add-apt-repository -y ppa:webupd8team/java
2921
- echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
3022
- sudo apt-get install -o Dpkg::Options::="--force-confnew" -y oracle-java8-installer

3rdparty/prometheus_client_model

Lines changed: 0 additions & 1 deletion
This file was deleted.

BUILD

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ cc_library(
1111
"lib/handler.h",
1212
"lib/histogram.cc",
1313
"lib/histogram_builder.cc",
14-
"lib/protobuf_delimited_serializer.cc",
1514
"lib/registry.cc",
1615
"lib/summary.cc",
1716
"lib/summary_builder.cc",
@@ -25,7 +24,5 @@ cc_library(
2524
visibility = ["//visibility:public"],
2625
deps = [
2726
"@civetweb//:civetweb",
28-
"@com_google_protobuf//:protobuf",
29-
"@prometheus_client_model//:prometheus_client_model",
3027
],
3128
)

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ if(NOT DEFINED CMAKE_CXX_STANDARD AND UNIX)
1010
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
1111
endif()
1212

13+
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
14+
find_package(Threads)
15+
1316
find_package(GoogleBenchmark)
14-
find_package(Protobuf REQUIRED)
1517
find_package(ZLIB REQUIRED)
1618
find_package(Telegraf)
1719

@@ -64,6 +66,10 @@ target_include_directories(gmock_main
6466
3rdparty/googletest/googlemock
6567
)
6668

69+
target_link_libraries(gmock_main PRIVATE
70+
${CMAKE_THREAD_LIBS_INIT}
71+
)
72+
6773
# prometheus-cpp
6874

6975
add_subdirectory(lib)

README.md

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,7 @@ the [travis build script](.travis.yml) might help.
6666
6767
### via CMake
6868
69-
One prerequisite for performing the build using CMake is
70-
having [Protocol Buffers](https://github.com/google/protobuf) >= 3.0
71-
installed. See the [travis build script](.travis.yml) for how to build
72-
it from source, or use your operating systems package manager to
73-
install it.
69+
For CMake builds don't forget to fetch the submodules first. Then build as usual.
7470
7571
``` shell
7672
# fetch third-party dependencies
@@ -126,8 +122,6 @@ cc_binary(
126122
When you call `prometheus_cpp_repositories()` in your `WORKSPACE` file,
127123
you introduce the following dependencies to your project:
128124

129-
* `load_com_google_protobuf()` for Google protobuf
130-
* `load_prometheus_client_model()` for Prometheus data model artifacts
131125
* `load_civetweb()` for Civetweb
132126
* `load_com_google_googletest()` for Google gtest
133127
* `load_com_google_googlebenchmark()` for Googlebenchmark
@@ -211,17 +205,6 @@ Alpha
211205

212206
## FAQ
213207

214-
### Why do you not provide a `protobuf` version as a submodule in `3rdparty`?
215-
216-
We opted against the submodule solution for protobuf, because
217-
otherwise ABI compatibiliy issues would force all consumers of
218-
`prometheus-cpp` to use exactly the same protobuf version as the one
219-
inside the submodule if they were using protobuf on its own.
220-
221-
To phrase it differently, this library should not control the exact
222-
version, but the executable linking against it should determine a
223-
version that other libraries also link against.
224-
225208
## License
226209

227210
MIT

include/prometheus/client_metric.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <string>
5+
#include <tuple>
6+
#include <vector>
7+
8+
#include "prometheus/metric_type.h"
9+
10+
namespace prometheus {
11+
12+
struct ClientMetric {
13+
14+
// Label
15+
16+
struct Label {
17+
std::string name;
18+
std::string value;
19+
20+
bool operator==(const Label& rhs) const {
21+
return std::tie(name, value) == std::tie(rhs.name, rhs.value);
22+
}
23+
};
24+
std::vector<Label> label;
25+
26+
// Counter
27+
28+
struct Counter {
29+
double value = 0.0;
30+
};
31+
Counter counter;
32+
33+
// Gauge
34+
35+
struct Gauge {
36+
double value = 0.0;
37+
};
38+
Gauge gauge;
39+
40+
// Summary
41+
42+
struct Quantile {
43+
double quantile = 0.0;
44+
double value = 0.0;
45+
};
46+
47+
struct Summary {
48+
std::uint64_t sample_count = 0;
49+
double sample_sum = 0.0;
50+
std::vector<Quantile> quantile;
51+
};
52+
Summary summary;
53+
54+
// Histogram
55+
56+
struct Bucket {
57+
std::uint64_t cumulative_count = 0;
58+
double upper_bound = 0.0;
59+
};
60+
61+
struct Histogram {
62+
std::uint64_t sample_count = 0;
63+
double sample_sum = 0.0;
64+
std::vector<Bucket> bucket;
65+
};
66+
Histogram histogram;
67+
68+
// Untyped
69+
70+
struct Untyped {
71+
double value = 0;
72+
};
73+
Untyped untyped;
74+
75+
// Timestamp
76+
77+
std::int64_t timestamp_ms = 0;
78+
};
79+
80+
} // namespace prometheus

include/prometheus/collectable.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@
22

33
#include <vector>
44

5-
namespace io {
65
namespace prometheus {
7-
namespace client {
8-
class MetricFamily;
9-
}
10-
}
6+
struct MetricFamily;
117
}
128

139
namespace prometheus {
1410

1511
class Collectable {
1612
public:
1713
virtual ~Collectable() = default;
18-
virtual std::vector<io::prometheus::client::MetricFamily> Collect() = 0;
14+
virtual std::vector<MetricFamily> Collect() = 0;
1915
};
2016
}

include/prometheus/counter.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
#include <atomic>
44

5-
#include "metrics.pb.h"
6-
5+
#include "prometheus/client_metric.h"
76
#include "prometheus/gauge.h"
87
#include "prometheus/metric.h"
98

109
namespace prometheus {
11-
class Counter : Metric {
10+
class Counter {
1211
public:
13-
static const io::prometheus::client::MetricType metric_type =
14-
io::prometheus::client::COUNTER;
12+
static const MetricType metric_type = MetricType::Counter;
1513

1614
void Increment();
1715
void Increment(double);
1816
double Value() const;
1917

20-
io::prometheus::client::Metric Collect();
18+
ClientMetric Collect();
2119

2220
private:
2321
Gauge gauge_;

include/prometheus/family.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <algorithm>
4+
#include <cassert>
45
#include <functional>
56
#include <map>
67
#include <memory>
@@ -15,6 +16,7 @@
1516
#include "gauge_builder.h"
1617
#include "histogram_builder.h"
1718
#include "metric.h"
19+
#include "metric_family.h"
1820

1921
namespace prometheus {
2022

@@ -32,7 +34,7 @@ class Family : public Collectable {
3234
void Remove(T* metric);
3335

3436
// Collectable
35-
std::vector<io::prometheus::client::MetricFamily> Collect() override;
37+
std::vector<MetricFamily> Collect() override;
3638

3739
private:
3840
std::unordered_map<std::size_t, std::unique_ptr<T>> metrics_;
@@ -44,7 +46,7 @@ class Family : public Collectable {
4446
const std::map<std::string, std::string> constant_labels_;
4547
std::mutex mutex_;
4648

47-
io::prometheus::client::Metric CollectMetric(std::size_t hash, T* metric);
49+
ClientMetric CollectMetric(std::size_t hash, T* metric);
4850

4951
static std::size_t hash_labels(
5052
const std::map<std::string, std::string>& labels);
@@ -116,27 +118,27 @@ void Family<T>::Remove(T* metric) {
116118
}
117119

118120
template <typename T>
119-
std::vector<io::prometheus::client::MetricFamily> Family<T>::Collect() {
121+
std::vector<MetricFamily> Family<T>::Collect() {
120122
std::lock_guard<std::mutex> lock{mutex_};
121-
auto family = io::prometheus::client::MetricFamily{};
122-
family.set_name(name_);
123-
family.set_help(help_);
124-
family.set_type(T::metric_type);
123+
auto family = MetricFamily{};
124+
family.name = name_;
125+
family.help = help_;
126+
family.type = T::metric_type;
125127
for (const auto& m : metrics_) {
126-
*family.add_metric() = std::move(CollectMetric(m.first, m.second.get()));
128+
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
127129
}
128130
return {family};
129131
}
130132

131133
template <typename T>
132-
io::prometheus::client::Metric Family<T>::CollectMetric(std::size_t hash,
133-
T* metric) {
134+
ClientMetric Family<T>::CollectMetric(std::size_t hash, T* metric) {
134135
auto collected = metric->Collect();
135136
auto add_label =
136137
[&collected](const std::pair<std::string, std::string>& label_pair) {
137-
auto pair = collected.add_label();
138-
pair->set_name(label_pair.first);
139-
pair->set_value(label_pair.second);
138+
auto label = ClientMetric::Label{};
139+
label.name = label_pair.first;
140+
label.value = label_pair.second;
141+
collected.label.push_back(std::move(label));
140142
};
141143
std::for_each(constant_labels_.cbegin(), constant_labels_.cend(), add_label);
142144
const auto& metric_labels = labels_.at(hash);

0 commit comments

Comments
 (0)