Skip to content

Commit

Permalink
Merge f48dca9 into 1b01f36
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed Nov 14, 2021
2 parents 1b01f36 + f48dca9 commit 93434f1
Show file tree
Hide file tree
Showing 20 changed files with 73 additions and 73 deletions.
5 changes: 2 additions & 3 deletions core/benchmarks/benchmark_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,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
7 changes: 3 additions & 4 deletions core/benchmarks/benchmark_helpers.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#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: 1 addition & 1 deletion core/include/prometheus/counter.h
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
21 changes: 9 additions & 12 deletions core/include/prometheus/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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 +90,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 +110,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 +123,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 +133,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 +143,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
3 changes: 1 addition & 2 deletions core/src/detail/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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
13 changes: 6 additions & 7 deletions core/src/family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 +30,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 +69,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 +80,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 +104,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
Original file line number Diff line number Diff line change
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
4 changes: 2 additions & 2 deletions core/tests/builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class BuilderTest : public testing::Test {

const std::string name = "some_name";
const std::string help = "Additional description.";
const std::map<std::string, std::string> const_labels = {{"key", "value"}};
const std::map<std::string, std::string> more_labels = {{"name", "test"}};
const Labels const_labels = {{"key", "value"}};
const Labels more_labels = {{"name", "test"}};
const std::vector<ClientMetric::Label> expected_labels = getExpectedLabels();
};

Expand Down
6 changes: 3 additions & 3 deletions core/tests/family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "prometheus/counter.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/histogram.h"
#include "prometheus/labels.h"

namespace prometheus {
namespace {
Expand All @@ -29,7 +30,7 @@ TEST(FamilyTest, labels) {
}

TEST(FamilyTest, reject_same_label_keys) {
auto labels = std::map<std::string, std::string>{{"component", "test"}};
auto labels = Labels{{"component", "test"}};

Family<Counter> family{"total_requests", "Counts all requests", labels};
EXPECT_ANY_THROW(family.Add(labels));
Expand Down Expand Up @@ -80,8 +81,7 @@ TEST(FamilyTest, add_twice) {

TEST(FamilyTest, throw_on_invalid_metric_name) {
auto create_family_with_invalid_name = []() {
return detail::make_unique<Family<Counter>>(
"", "empty name", std::map<std::string, std::string>{});
return detail::make_unique<Family<Counter>>("", "empty name", Labels{});
};
EXPECT_ANY_THROW(create_family_with_invalid_name());
}
Expand Down
10 changes: 5 additions & 5 deletions core/tests/utils_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UtilsTest : public testing::Test {
};

TEST_F(UtilsTest, hash_labels_1) {
std::map<std::string, std::string> labels;
Labels labels;
labels.insert(std::make_pair<std::string, std::string>("key1", "value1"));
labels.insert(std::make_pair<std::string, std::string>("key2", "vaule2"));

Expand All @@ -26,14 +26,14 @@ TEST_F(UtilsTest, hash_labels_1) {
}

TEST_F(UtilsTest, hash_labels_2) {
std::map<std::string, std::string> labels1{{"aa", "bb"}};
std::map<std::string, std::string> labels2{{"a", "abb"}};
Labels labels1{{"aa", "bb"}};
Labels labels2{{"a", "abb"}};
EXPECT_NE(hasher(labels1), hasher(labels2));
}

TEST_F(UtilsTest, hash_label_3) {
std::map<std::string, std::string> labels1{{"a", "a"}};
std::map<std::string, std::string> labels2{{"aa", ""}};
Labels labels1{{"a", "a"}};
Labels labels2{{"aa", ""}};
EXPECT_NE(hasher(labels1), hasher(labels2));
}

Expand Down

0 comments on commit 93434f1

Please sign in to comment.