Skip to content

Commit

Permalink
chore(core): Introduce Labels type
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Nov 14, 2021
1 parent 1b01f36 commit d8140dd
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 92 deletions.
6 changes: 3 additions & 3 deletions core/benchmarks/benchmark_helpers.cc
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cstdlib>
#include <map>

std::string GenerateRandomString(std::size_t length) {
auto randchar = []() -> char {
Expand All @@ -14,10 +15,9 @@ std::string GenerateRandomString(std::size_t length) {
return str;
}

std::map<std::string, std::string> GenerateRandomLabels(
std::size_t number_of_pairs) {
prometheus::Labels GenerateRandomLabels(std::size_t number_of_pairs) {
const auto label_character_count = 10;
auto label_pairs = std::map<std::string, std::string>{};
auto label_pairs = prometheus::Labels{};
for (std::size_t i = 0; i < number_of_pairs; i++) {
label_pairs.insert({GenerateRandomString(label_character_count),
GenerateRandomString(label_character_count)});
Expand Down
6 changes: 3 additions & 3 deletions core/benchmarks/benchmark_helpers.h
@@ -1,9 +1,9 @@
#pragma once

#include <cstddef>
#include <map>
#include <string>

#include "prometheus/labels.h"

std::string GenerateRandomString(std::size_t length);
std::map<std::string, std::string> GenerateRandomLabels(
std::size_t number_of_labels);
prometheus::Labels GenerateRandomLabels(std::size_t number_of_labels);
2 changes: 0 additions & 2 deletions core/benchmarks/counter_bench.cc
@@ -1,7 +1,5 @@
#include <benchmark/benchmark.h>

#include <string>

#include "prometheus/counter.h"
#include "prometheus/family.h"
#include "prometheus/registry.h"
Expand Down
2 changes: 0 additions & 2 deletions core/benchmarks/gauge_bench.cc
@@ -1,7 +1,5 @@
#include <benchmark/benchmark.h>

#include <string>

#include "prometheus/family.h"
#include "prometheus/gauge.h"
#include "prometheus/registry.h"
Expand Down
1 change: 0 additions & 1 deletion core/benchmarks/histogram_bench.cc
Expand Up @@ -3,7 +3,6 @@
#include <chrono>
#include <cstdint>
#include <random>
#include <string>
#include <vector>

#include "prometheus/family.h"
Expand Down
1 change: 0 additions & 1 deletion core/benchmarks/summary_bench.cc
Expand Up @@ -4,7 +4,6 @@
#include <chrono>
#include <cmath>
#include <random>
#include <string>
#include <vector>

#include "prometheus/family.h"
Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/counter.h
Expand Up @@ -72,7 +72,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Counter {
///
/// - Name(const std::string&) to set the metric name,
/// - Help(const std::string&) to set an additional description.
/// - Label(const std::map<std::string, std::string>&) to assign a set of
/// - Labels(const Labels&) to assign a set of
/// key-value pairs (= labels) to the metric.
///
/// To finish the configuration of the Counter metric, register it with
Expand Down
7 changes: 4 additions & 3 deletions core/include/prometheus/detail/builder.h
@@ -1,8 +1,9 @@
#pragma once

#include <map>
#include <string>

#include "prometheus/labels.h"

// IWYU pragma: private
// IWYU pragma: no_include "prometheus/family.h"

Expand All @@ -17,13 +18,13 @@ namespace detail {
template <typename T>
class Builder {
public:
Builder& Labels(const std::map<std::string, std::string>& labels);
Builder& Labels(const ::prometheus::Labels& labels);
Builder& Name(const std::string&);
Builder& Help(const std::string&);
Family<T>& Register(Registry&);

private:
std::map<std::string, std::string> labels_;
::prometheus::Labels labels_;
std::string name_;
std::string help_;
};
Expand Down
6 changes: 2 additions & 4 deletions core/include/prometheus/detail/utils.h
@@ -1,10 +1,9 @@
#pragma once

#include <cstddef>
#include <map>
#include <string>

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

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

} // namespace detail
Expand Down
22 changes: 9 additions & 13 deletions core/include/prometheus/family.h
@@ -1,6 +1,5 @@
#pragma once

#include <map>
#include <memory>
#include <mutex>
#include <string>
Expand All @@ -12,6 +11,7 @@
#include "prometheus/detail/core_export.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/detail/utils.h"
#include "prometheus/labels.h"
#include "prometheus/metric_family.h"

// IWYU pragma: no_include "prometheus/counter.h"
Expand Down Expand Up @@ -89,7 +89,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
/// metric.
/// \throw std::runtime_exception on invalid metric or label names.
Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels);
const Labels& constant_labels);

/// \brief Add a new dimensional data.
///
Expand All @@ -109,7 +109,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
/// labels already exists - the already existing dimensional data.
/// \throw std::runtime_exception on invalid label names.
template <typename... Args>
T& Add(const std::map<std::string, std::string>& labels, Args&&... args) {
T& Add(const Labels& labels, Args&&... args) {
return Add(labels, detail::make_unique<T>(args...));
}

Expand All @@ -122,7 +122,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
/// \brief Returns true if the dimensional data with the given labels exist
///
/// \param labels A set of key-value pairs (= labels) of the dimensional data.
bool Has(const std::map<std::string, std::string>& labels) const;
bool Has(const Labels& labels) const;

/// \brief Returns the name for this family.
///
Expand All @@ -132,7 +132,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
/// \brief Returns the constant labels for this family.
///
/// \return All constant labels as key-value pairs.
const std::map<std::string, std::string> GetConstantLabels() const;
const Labels GetConstantLabels() const;

/// \brief Returns the current value of each dimensional data.
///
Expand All @@ -142,19 +142,15 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable {
std::vector<MetricFamily> Collect() const override;

private:
std::unordered_map<std::map<std::string, std::string>, std::unique_ptr<T>,
detail::LabelHasher>
metrics_;
std::unordered_map<Labels, std::unique_ptr<T>, detail::LabelHasher> metrics_;

const std::string name_;
const std::string help_;
const std::map<std::string, std::string> constant_labels_;
const Labels constant_labels_;
mutable std::mutex mutex_;

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

} // namespace prometheus
2 changes: 1 addition & 1 deletion core/include/prometheus/gauge.h
Expand Up @@ -84,7 +84,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Gauge {
///
/// - Name(const std::string&) to set the metric name,
/// - Help(const std::string&) to set an additional description.
/// - Label(const std::map<std::string, std::string>&) to assign a set of
/// - Labels(const Labels&) to assign a set of
/// key-value pairs (= labels) to the metric.
///
/// To finish the configuration of the Gauge metric register it with
Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/histogram.h
Expand Up @@ -96,7 +96,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Histogram {
///
/// - Name(const std::string&) to set the metric name,
/// - Help(const std::string&) to set an additional description.
/// - Label(const std::map<std::string, std::string>&) to assign a set of
/// - Labels(const Labels&) to assign a set of
/// key-value pairs (= labels) to the metric.
///
/// To finish the configuration of the Histogram metric register it with
Expand Down
11 changes: 11 additions & 0 deletions core/include/prometheus/labels.h
@@ -0,0 +1,11 @@
#pragma once

#include <map>
#include <string>

namespace prometheus {

/// \brief Multiple labels and their value.
using Labels = std::map<std::string, std::string>;

} // namespace prometheus
4 changes: 2 additions & 2 deletions core/include/prometheus/registry.h
@@ -1,6 +1,5 @@
#pragma once

#include <map>
#include <memory>
#include <mutex>
#include <string>
Expand All @@ -9,6 +8,7 @@
#include "prometheus/collectable.h"
#include "prometheus/detail/core_export.h"
#include "prometheus/family.h"
#include "prometheus/labels.h"
#include "prometheus/metric_family.h"

namespace prometheus {
Expand Down Expand Up @@ -96,7 +96,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Registry : public Collectable {

template <typename T>
Family<T>& Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
const Labels& labels);

const InsertBehavior insert_behavior_;
std::vector<std::unique_ptr<Family<Counter>>> counters_;
Expand Down
2 changes: 1 addition & 1 deletion core/include/prometheus/summary.h
Expand Up @@ -113,7 +113,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Summary {
///
/// - Name(const std::string&) to set the metric name,
/// - Help(const std::string&) to set an additional description.
/// - Label(const std::map<std::string, std::string>&) to assign a set of
/// - Labels(const Labels&) to assign a set of
/// key-value pairs (= labels) to the metric.
///
/// To finish the configuration of the Summary metric register it with
Expand Down
3 changes: 1 addition & 2 deletions core/src/detail/builder.cc
Expand Up @@ -12,8 +12,7 @@ namespace prometheus {
namespace detail {

template <typename T>
Builder<T>& Builder<T>::Labels(
const std::map<std::string, std::string>& labels) {
Builder<T>& Builder<T>::Labels(const ::prometheus::Labels& labels) {
labels_ = labels;
return *this;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/detail/utils.cc
@@ -1,5 +1,6 @@
#include "prometheus/detail/utils.h"

#include <map>
#include <utility>

#include "hash.h"
Expand All @@ -8,8 +9,7 @@ namespace prometheus {

namespace detail {

std::size_t LabelHasher::operator()(
const std::map<std::string, std::string>& labels) const {
std::size_t LabelHasher::operator()(const Labels& labels) const {
size_t seed = 0;
for (auto& label : labels) {
hash_combine(&seed, label.first, label.second);
Expand Down
14 changes: 7 additions & 7 deletions core/src/family.cc
Expand Up @@ -2,6 +2,7 @@

#include <algorithm>
#include <cassert>
#include <map>
#include <stdexcept>
#include <type_traits>
#include <utility>
Expand All @@ -16,7 +17,7 @@ namespace prometheus {

template <typename T>
Family<T>::Family(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& constant_labels)
const Labels& constant_labels)
: name_(name), help_(help), constant_labels_(constant_labels) {
if (!CheckMetricName(name_)) {
throw std::invalid_argument("Invalid metric name");
Expand All @@ -30,8 +31,7 @@ Family<T>::Family(const std::string& name, const std::string& help,
}

template <typename T>
T& Family<T>::Add(const std::map<std::string, std::string>& labels,
std::unique_ptr<T> object) {
T& Family<T>::Add(const Labels& labels, std::unique_ptr<T> object) {
std::lock_guard<std::mutex> lock{mutex_};

auto insert_result =
Expand Down Expand Up @@ -70,7 +70,7 @@ void Family<T>::Remove(T* metric) {
}

template <typename T>
bool Family<T>::Has(const std::map<std::string, std::string>& labels) const {
bool Family<T>::Has(const Labels& labels) const {
std::lock_guard<std::mutex> lock{mutex_};
return metrics_.count(labels) != 0u;
}
Expand All @@ -81,7 +81,7 @@ const std::string& Family<T>::GetName() const {
}

template <typename T>
const std::map<std::string, std::string> Family<T>::GetConstantLabels() const {
const Labels Family<T>::GetConstantLabels() const {
return constant_labels_;
}

Expand All @@ -105,8 +105,8 @@ std::vector<MetricFamily> Family<T>::Collect() const {
}

template <typename T>
ClientMetric Family<T>::CollectMetric(
const std::map<std::string, std::string>& metric_labels, T* metric) const {
ClientMetric Family<T>::CollectMetric(const Labels& metric_labels,
T* metric) const {
auto collected = metric->Collect();
collected.label.reserve(constant_labels_.size() + metric_labels.size());
const auto add_label =
Expand Down
26 changes: 13 additions & 13 deletions core/src/registry.cc
Expand Up @@ -96,7 +96,7 @@ bool Registry::NameExistsInOtherType<Summary>(const std::string& name) const {

template <typename T>
Family<T>& Registry::Add(const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels) {
const Labels& labels) {
std::lock_guard<std::mutex> lock{mutex_};

if (NameExistsInOtherType<T>(name)) {
Expand Down Expand Up @@ -137,20 +137,20 @@ Family<T>& Registry::Add(const std::string& name, const std::string& help,
return ref;
}

template Family<Counter>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Counter>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

template Family<Gauge>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Gauge>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

template Family<Summary>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Summary>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

template Family<Histogram>& Registry::Add(
const std::string& name, const std::string& help,
const std::map<std::string, std::string>& labels);
template Family<Histogram>& Registry::Add(const std::string& name,
const std::string& help,
const Labels& labels);

} // namespace prometheus

0 comments on commit d8140dd

Please sign in to comment.