Skip to content

Commit

Permalink
Merge b6a1b4c into a0d7c52
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Aug 31, 2019
2 parents a0d7c52 + b6a1b4c commit dc0603c
Show file tree
Hide file tree
Showing 35 changed files with 308 additions and 145 deletions.
10 changes: 9 additions & 1 deletion CMakeLists.txt
@@ -1,13 +1,14 @@

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

project(prometheus-cpp)
project(prometheus-cpp VERSION 0.8.0)

include(GNUInstallDirs)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

option(BUILD_SHARED_LIBS "Build libraries as shared ones" OFF)
option(ENABLE_PULL "Build prometheus-cpp pull library" ON)
option(ENABLE_PUSH "Build prometheus-cpp push library" ON)
option(ENABLE_COMPRESSION "Enable gzip compression" ON)
Expand All @@ -20,6 +21,13 @@ if(OVERRIDE_CXX_STANDARD_FLAGS)
set(CMAKE_CXX_EXTENSIONS Off)
endif()

# Hide things by default for shared libraries
if(BUILD_SHARED_LIBS)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
endif()

set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Threads)

Expand Down
3 changes: 3 additions & 0 deletions core/BUILD.bazel
Expand Up @@ -7,6 +7,9 @@ cc_library(
hdrs = glob(
["include/**/*.h"],
),
copts = [
"-DPROMETHEUS_CPP_COMPILE_CORE",
],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
11 changes: 10 additions & 1 deletion core/CMakeLists.txt
Expand Up @@ -2,12 +2,15 @@
add_library(core
src/check_names.cc
src/counter.cc
src/detail/builder.impl.h
src/detail/ckms_quantiles.cc
src/detail/time_window_quantiles.cc
src/detail/utils.cc
src/gauge.cc
src/family.impl.h
src/histogram.cc
src/registry.cc
src/registry.impl.h
src/serializer.cc
src/summary.cc
src/text_serializer.cc
Expand All @@ -26,7 +29,13 @@ target_include_directories(core
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

set_target_properties(core PROPERTIES OUTPUT_NAME ${PROJECT_NAME}-core)
set_target_properties(core
PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}-core
DEFINE_SYMBOL PROMETHEUS_CPP_COMPILE_CORE
VERSION "${PROJECT_VERSION}"
SOVERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
)

install(
TARGETS core
Expand Down
6 changes: 4 additions & 2 deletions core/include/prometheus/check_names.h
Expand Up @@ -2,8 +2,10 @@

#include <string>

#include "prometheus/detail/core_export.h"

namespace prometheus {

bool CheckMetricName(const std::string& name);
bool CheckLabelName(const std::string& name);
PROMETHEUS_CPP_CORE_EXPORT bool CheckMetricName(const std::string& name);
PROMETHEUS_CPP_CORE_EXPORT bool CheckLabelName(const std::string& name);
} // namespace prometheus
4 changes: 3 additions & 1 deletion core/include/prometheus/client_metric.h
Expand Up @@ -5,9 +5,11 @@
#include <tuple>
#include <vector>

#include "prometheus/detail/core_export.h"

namespace prometheus {

struct ClientMetric {
struct PROMETHEUS_CPP_CORE_EXPORT ClientMetric {
// Label

struct Label {
Expand Down
4 changes: 3 additions & 1 deletion core/include/prometheus/collectable.h
Expand Up @@ -2,6 +2,8 @@

#include <vector>

#include "prometheus/detail/core_export.h"

namespace prometheus {
struct MetricFamily;
}
Expand All @@ -12,7 +14,7 @@ namespace prometheus {
/// collect metrics.
///
/// A Collectable has to be registered for collection. See Registry.
class Collectable {
class PROMETHEUS_CPP_CORE_EXPORT Collectable {
public:
virtual ~Collectable() = default;

Expand Down
5 changes: 3 additions & 2 deletions core/include/prometheus/counter.h
Expand Up @@ -2,6 +2,7 @@

#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/gauge.h"
#include "prometheus/metric_type.h"

Expand All @@ -22,7 +23,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class Counter {
class PROMETHEUS_CPP_CORE_EXPORT Counter {
public:
static const MetricType metric_type{MetricType::Counter};

Expand Down Expand Up @@ -76,6 +77,6 @@ class Counter {
///
/// To finish the configuration of the Counter metric, register it with
/// Register(Registry&).
detail::Builder<Counter> BuildCounter();
PROMETHEUS_CPP_CORE_EXPORT detail::Builder<Counter> BuildCounter();

} // namespace prometheus
26 changes: 1 addition & 25 deletions core/include/prometheus/detail/builder.h
Expand Up @@ -26,29 +26,5 @@ class Builder {
std::string help_;
};

template <typename T>
Builder<T>& Builder<T>::Labels(
const std::map<std::string, std::string>& labels) {
labels_ = labels;
return *this;
}

template <typename T>
Builder<T>& Builder<T>::Name(const std::string& name) {
name_ = name;
return *this;
}

template <typename T>
Builder<T>& Builder<T>::Help(const std::string& help) {
help_ = help;
return *this;
}

template <typename T>
Family<T>& Builder<T>::Register(Registry& registry) {
return registry.Add<T>(name_, help_, labels_);
}

} // namespace detail
} // namespace prometheus
} // namespace prometheus
6 changes: 4 additions & 2 deletions core/include/prometheus/detail/ckms_quantiles.h
Expand Up @@ -5,12 +5,14 @@
#include <functional>
#include <vector>

#include "prometheus/detail/core_export.h"

namespace prometheus {
namespace detail {

class CKMSQuantiles {
class PROMETHEUS_CPP_CORE_EXPORT CKMSQuantiles {
public:
struct Quantile {
struct PROMETHEUS_CPP_CORE_EXPORT Quantile {
const double quantile;
const double error;
const double u;
Expand Down
11 changes: 11 additions & 0 deletions core/include/prometheus/detail/core_export.h
@@ -0,0 +1,11 @@
#pragma once

#if defined(_WIN32)
# ifdef PROMETHEUS_CPP_COMPILE_CORE
# define PROMETHEUS_CPP_CORE_EXPORT __declspec(dllexport)
# else
# define PROMETHEUS_CPP_CORE_EXPORT __declspec(dllimport)
# endif
#else
# define PROMETHEUS_CPP_CORE_EXPORT __attribute__((visibility("default")))
#endif
3 changes: 2 additions & 1 deletion core/include/prometheus/detail/time_window_quantiles.h
Expand Up @@ -5,11 +5,12 @@
#include <vector>

#include "prometheus/detail/ckms_quantiles.h"
#include "prometheus/detail/core_export.h"

namespace prometheus {
namespace detail {

class TimeWindowQuantiles {
class PROMETHEUS_CPP_CORE_EXPORT TimeWindowQuantiles {
using Clock = std::chrono::steady_clock;

public:
Expand Down
5 changes: 4 additions & 1 deletion core/include/prometheus/detail/utils.h
Expand Up @@ -4,6 +4,8 @@
#include <map>
#include <string>

#include "prometheus/detail/core_export.h"

namespace prometheus {

namespace detail {
Expand All @@ -13,7 +15,8 @@ namespace detail {
/// \param labels The map that will be computed the hash value.
///
/// \returns The hash value of the given labels.
std::size_t hash_labels(const std::map<std::string, std::string>& labels);
PROMETHEUS_CPP_CORE_EXPORT std::size_t hash_labels(
const std::map<std::string, std::string>& labels);

} // namespace detail

Expand Down
91 changes: 7 additions & 84 deletions core/include/prometheus/family.h
Expand Up @@ -15,6 +15,7 @@
#include "prometheus/check_names.h"
#include "prometheus/client_metric.h"
#include "prometheus/collectable.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/detail/utils.h"
#include "prometheus/metric_family.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ namespace prometheus {
///
/// \tparam T One of the metric types Counter, Gauge, Histogram or Summary.
template <typename T>
class Family : public Collectable {
class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
public:
/// \brief Create a new metric.
///
Expand Down Expand Up @@ -107,7 +108,9 @@ class Family : public Collectable {
/// \return Return the newly created dimensional data or - if a same set of
/// labels already exists - the already existing dimensional data.
template <typename... Args>
T& Add(const std::map<std::string, std::string>& labels, Args&&... args);
T& Add(const std::map<std::string, std::string>& labels, Args&&... args) {
return Add(labels, detail::make_unique<T>(args...));
}

/// \brief Remove the given dimensional data.
///
Expand All @@ -133,88 +136,8 @@ class Family : public Collectable {
std::mutex mutex_;

ClientMetric CollectMetric(std::size_t hash, T* metric);
T& Add(const std::map<std::string, std::string>& labels,
std::unique_ptr<T> object);
};

template <typename T>
Family<T>::Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels)
: name_(name), help_(help), constant_labels_(constant_labels) {
assert(CheckMetricName(name_));
}

template <typename T>
template <typename... Args>
T& Family<T>::Add(const std::map<std::string, std::string>& labels,
Args&&... args) {
auto hash = detail::hash_labels(labels);
std::lock_guard<std::mutex> lock{mutex_};
auto metrics_iter = metrics_.find(hash);

if (metrics_iter != metrics_.end()) {
#ifndef NDEBUG
auto labels_iter = labels_.find(hash);
assert(labels_iter != labels_.end());
const auto& old_labels = labels_iter->second;
assert(labels == old_labels);
#endif
return *metrics_iter->second;
} else {
#ifndef NDEBUG
for (auto& label_pair : labels) {
auto& label_name = label_pair.first;
assert(CheckLabelName(label_name));
}
#endif

auto metric =
metrics_.insert(std::make_pair(hash, detail::make_unique<T>(args...)));
assert(metric.second);
labels_.insert({hash, labels});
labels_reverse_lookup_.insert({metric.first->second.get(), hash});
return *(metric.first->second);
}
}

template <typename T>
void Family<T>::Remove(T* metric) {
std::lock_guard<std::mutex> lock{mutex_};
if (labels_reverse_lookup_.count(metric) == 0) {
return;
}

auto hash = labels_reverse_lookup_.at(metric);
metrics_.erase(hash);
labels_.erase(hash);
labels_reverse_lookup_.erase(metric);
}

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

template <typename T>
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 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);
std::for_each(metric_labels.cbegin(), metric_labels.cend(), add_label);
return collected;
}

} // namespace prometheus
5 changes: 3 additions & 2 deletions core/include/prometheus/gauge.h
Expand Up @@ -4,6 +4,7 @@

#include "prometheus/client_metric.h"
#include "prometheus/detail/builder.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/metric_type.h"

namespace prometheus {
Expand All @@ -20,7 +21,7 @@ namespace prometheus {
///
/// The class is thread-safe. No concurrent call to any API of this type causes
/// a data race.
class Gauge {
class PROMETHEUS_CPP_CORE_EXPORT Gauge {
public:
static const MetricType metric_type{MetricType::Gauge};

Expand Down Expand Up @@ -88,6 +89,6 @@ class Gauge {
///
/// To finish the configuration of the Gauge metric register it with
/// Register(Registry&).
detail::Builder<Gauge> BuildGauge();
PROMETHEUS_CPP_CORE_EXPORT detail::Builder<Gauge> BuildGauge();

} // namespace prometheus

0 comments on commit dc0603c

Please sign in to comment.