Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
[submodule "civetweb"]
path = 3rdparty/civetweb
url = https://github.com/civetweb/civetweb.git
[submodule "prometheus_client_model"]
path = 3rdparty/prometheus_client_model
url = https://github.com/prometheus/client_model.git
8 changes: 0 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ install:
- sudo apt-get update
- sudo apt-get install -y gcc-5 g++-5
- sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 1 --slave /usr/bin/g++ g++ /usr/bin/g++-5
- wget https://github.com/google/protobuf/archive/v3.1.0.tar.gz
- tar xzf v3.1.0.tar.gz
- cd protobuf-3.1.0
- ./autogen.sh
- ./configure --prefix=/usr
- make -j 4
- sudo make install
- cd ..
- sudo add-apt-repository -y ppa:webupd8team/java
- echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | sudo debconf-set-selections
- sudo apt-get install -o Dpkg::Options::="--force-confnew" -y oracle-java8-installer
Expand Down
1 change: 0 additions & 1 deletion 3rdparty/prometheus_client_model
Submodule prometheus_client_model deleted from fa8ad6
3 changes: 0 additions & 3 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ cc_library(
"lib/handler.h",
"lib/histogram.cc",
"lib/histogram_builder.cc",
"lib/protobuf_delimited_serializer.cc",
"lib/registry.cc",
"lib/summary.cc",
"lib/summary_builder.cc",
Expand All @@ -25,7 +24,5 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"@civetweb//:civetweb",
"@com_google_protobuf//:protobuf",
"@prometheus_client_model//:prometheus_client_model",
],
)
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ if(NOT DEFINED CMAKE_CXX_STANDARD AND UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads)

find_package(GoogleBenchmark)
find_package(Protobuf REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Telegraf)

Expand Down Expand Up @@ -64,6 +66,10 @@ target_include_directories(gmock_main
3rdparty/googletest/googlemock
)

target_link_libraries(gmock_main PRIVATE
${CMAKE_THREAD_LIBS_INIT}
)

# prometheus-cpp

add_subdirectory(lib)
Expand Down
19 changes: 1 addition & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ the [travis build script](.travis.yml) might help.

### via CMake

One prerequisite for performing the build using CMake is
having [Protocol Buffers](https://github.com/google/protobuf) >= 3.0
installed. See the [travis build script](.travis.yml) for how to build
it from source, or use your operating systems package manager to
install it.
For CMake builds don't forget to fetch the submodules first. Then build as usual.

``` shell
# fetch third-party dependencies
Expand Down Expand Up @@ -126,8 +122,6 @@ cc_binary(
When you call `prometheus_cpp_repositories()` in your `WORKSPACE` file,
you introduce the following dependencies to your project:

* `load_com_google_protobuf()` for Google protobuf
* `load_prometheus_client_model()` for Prometheus data model artifacts
* `load_civetweb()` for Civetweb
* `load_com_google_googletest()` for Google gtest
* `load_com_google_googlebenchmark()` for Googlebenchmark
Expand Down Expand Up @@ -211,17 +205,6 @@ Alpha

## FAQ

### Why do you not provide a `protobuf` version as a submodule in `3rdparty`?

We opted against the submodule solution for protobuf, because
otherwise ABI compatibiliy issues would force all consumers of
`prometheus-cpp` to use exactly the same protobuf version as the one
inside the submodule if they were using protobuf on its own.

To phrase it differently, this library should not control the exact
version, but the executable linking against it should determine a
version that other libraries also link against.

## License

MIT
80 changes: 80 additions & 0 deletions include/prometheus/client_metric.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <cstdint>
#include <string>
#include <tuple>
#include <vector>

#include "prometheus/metric_type.h"

namespace prometheus {

struct ClientMetric {

// Label

struct Label {
std::string name;
std::string value;

bool operator==(const Label& rhs) const {
return std::tie(name, value) == std::tie(rhs.name, rhs.value);
}
};
std::vector<Label> label;

// Counter

struct Counter {
double value = 0.0;
};
Counter counter;

// Gauge

struct Gauge {
double value = 0.0;
};
Gauge gauge;

// Summary

struct Quantile {
double quantile = 0.0;
double value = 0.0;
};

struct Summary {
std::uint64_t sample_count = 0;
double sample_sum = 0.0;
std::vector<Quantile> quantile;
};
Summary summary;

// Histogram

struct Bucket {
std::uint64_t cumulative_count = 0;
double upper_bound = 0.0;
};

struct Histogram {
std::uint64_t sample_count = 0;
double sample_sum = 0.0;
std::vector<Bucket> bucket;
};
Histogram histogram;

// Untyped

struct Untyped {
double value = 0;
};
Untyped untyped;

// Timestamp

std::int64_t timestamp_ms = 0;
};

} // namespace prometheus
8 changes: 2 additions & 6 deletions include/prometheus/collectable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

#include <vector>

namespace io {
namespace prometheus {
namespace client {
class MetricFamily;
}
}
struct MetricFamily;
}

namespace prometheus {

class Collectable {
public:
virtual ~Collectable() = default;
virtual std::vector<io::prometheus::client::MetricFamily> Collect() = 0;
virtual std::vector<MetricFamily> Collect() = 0;
};
}
10 changes: 4 additions & 6 deletions include/prometheus/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

#include <atomic>

#include "metrics.pb.h"

#include "prometheus/client_metric.h"
#include "prometheus/gauge.h"
#include "prometheus/metric.h"

namespace prometheus {
class Counter : Metric {
class Counter {
public:
static const io::prometheus::client::MetricType metric_type =
io::prometheus::client::COUNTER;
static const MetricType metric_type = MetricType::Counter;

void Increment();
void Increment(double);
double Value() const;

io::prometheus::client::Metric Collect();
ClientMetric Collect();

private:
Gauge gauge_;
Expand Down
28 changes: 15 additions & 13 deletions include/prometheus/family.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <algorithm>
#include <cassert>
#include <functional>
#include <map>
#include <memory>
Expand All @@ -15,6 +16,7 @@
#include "gauge_builder.h"
#include "histogram_builder.h"
#include "metric.h"
#include "metric_family.h"

namespace prometheus {

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

// Collectable
std::vector<io::prometheus::client::MetricFamily> Collect() override;
std::vector<MetricFamily> Collect() override;

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

io::prometheus::client::Metric CollectMetric(std::size_t hash, T* metric);
ClientMetric CollectMetric(std::size_t hash, T* metric);

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

template <typename T>
std::vector<io::prometheus::client::MetricFamily> Family<T>::Collect() {
std::vector<MetricFamily> Family<T>::Collect() {
std::lock_guard<std::mutex> lock{mutex_};
auto family = io::prometheus::client::MetricFamily{};
family.set_name(name_);
family.set_help(help_);
family.set_type(T::metric_type);
auto family = MetricFamily{};
family.name = name_;
family.help = help_;
family.type = T::metric_type;
for (const auto& m : metrics_) {
*family.add_metric() = std::move(CollectMetric(m.first, m.second.get()));
family.metric.push_back(std::move(CollectMetric(m.first, m.second.get())));
}
return {family};
}

template <typename T>
io::prometheus::client::Metric Family<T>::CollectMetric(std::size_t hash,
T* metric) {
ClientMetric Family<T>::CollectMetric(std::size_t hash, T* metric) {
auto collected = metric->Collect();
auto add_label =
[&collected](const std::pair<std::string, std::string>& label_pair) {
auto pair = collected.add_label();
pair->set_name(label_pair.first);
pair->set_value(label_pair.second);
auto label = ClientMetric::Label{};
label.name = label_pair.first;
label.value = label_pair.second;
collected.label.push_back(std::move(label));
};
std::for_each(constant_labels_.cbegin(), constant_labels_.cend(), add_label);
const auto& metric_labels = labels_.at(hash);
Expand Down
10 changes: 4 additions & 6 deletions include/prometheus/gauge.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

#include <atomic>

#include "prometheus/client_metric.h"
#include "prometheus/collectable.h"
#include "prometheus/metric.h"

#include "metrics.pb.h"

namespace prometheus {

class Gauge : public Metric {
class Gauge {
public:
static const io::prometheus::client::MetricType metric_type =
io::prometheus::client::GAUGE;
static const MetricType metric_type = MetricType::Gauge;

Gauge();
Gauge(double);
Expand All @@ -24,7 +22,7 @@ class Gauge : public Metric {
void SetToCurrentTime();
double Value() const;

io::prometheus::client::Metric Collect();
ClientMetric Collect();

private:
void Change(double);
Expand Down
10 changes: 4 additions & 6 deletions include/prometheus/histogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

#include <vector>

#include "prometheus/client_metric.h"
#include "prometheus/counter.h"

#include "metrics.pb.h"

namespace prometheus {
class Histogram : public Metric {
class Histogram {
public:
using BucketBoundaries = std::vector<double>;

static const io::prometheus::client::MetricType metric_type =
io::prometheus::client::HISTOGRAM;
static const MetricType metric_type = MetricType::Histogram;

Histogram(const BucketBoundaries& buckets);

void Observe(double value);

io::prometheus::client::Metric Collect();
ClientMetric Collect();

private:
const BucketBoundaries bucket_boundaries_;
Expand Down
4 changes: 2 additions & 2 deletions include/prometheus/metric.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include "metrics.pb.h"
#include "prometheus/client_metric.h"

namespace prometheus {

class Metric {
public:
virtual ~Metric() = default;
virtual io::prometheus::client::Metric Collect() = 0;
virtual ClientMetric Collect() = 0;
};
}
17 changes: 17 additions & 0 deletions include/prometheus/metric_family.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <string>
#include <vector>

#include "prometheus/client_metric.h"
#include "prometheus/metric_type.h"

namespace prometheus {

struct MetricFamily {
std::string name;
std::string help;
MetricType type = MetricType::Untyped;
std::vector<ClientMetric> metric;
};
} // namespace prometheus
Loading