Skip to content

Commit

Permalink
Merge 62f0484 into 47cf983
Browse files Browse the repository at this point in the history
  • Loading branch information
jovany-wang committed Dec 2, 2018
2 parents 47cf983 + 62f0484 commit 40e0448
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(core
src/detail/histogram_builder.cc
src/detail/summary_builder.cc
src/detail/time_window_quantiles.cc
src/detail/utils.cc
src/gauge.cc
src/histogram.cc
src/registry.cc
Expand Down
31 changes: 31 additions & 0 deletions core/include/prometheus/detail/hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma

#include <functional>

namespace prometheus {

inline void hash_combine(std::size_t* seed) {

}

//TODO(qwang) should we provide an interface for user to
// provide their defined logic of computing hash value?
template <typename T>
inline void hash_combine(std::size_t* seed, const T& value) {
*seed ^= std::hash<T>{}(value) + 0x9e3779b9 + (*seed << 6) + (*seed >> 2);
}

template <typename T, typename ... Types>
inline void hash_combine(std::size_t* seed, const T& value, const Types&... args) {
hash_combine(seed, value);
hash_combine(seed, args...);
}

template <typename... Types>
inline std::size_t hash_value(const Types&... args) {
std::size_t seed = 0;
hash_combine(&seed, args...);
return seed;
}

} // prometheus
16 changes: 16 additions & 0 deletions core/include/prometheus/detail/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma onece

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

namespace prometheus {

namespace utils {

///TODO(qwang): doc and test this.
std::size_t hash_labels(const std::map<std::string, std::string>& labels);

} // utils

} // prometheus
17 changes: 2 additions & 15 deletions core/include/prometheus/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "prometheus/collectable.h"
#include "prometheus/detail/future_std.h"
#include "prometheus/metric_family.h"
#include "prometheus/detail/utils.h"

namespace prometheus {

Expand Down Expand Up @@ -133,8 +134,6 @@ class Family : public Collectable {

ClientMetric CollectMetric(std::size_t hash, T* metric);

static std::size_t hash_labels(
const std::map<std::string, std::string>& labels);
};

template <typename T>
Expand All @@ -155,7 +154,7 @@ T& Family<T>::Add(const std::map<std::string, std::string>& labels,
}
#endif

auto hash = hash_labels(labels);
auto hash = utils::hash_labels(labels);
std::lock_guard<std::mutex> lock{mutex_};
auto metrics_iter = metrics_.find(hash);

Expand All @@ -177,18 +176,6 @@ T& Family<T>::Add(const std::map<std::string, std::string>& labels,
}
}

template <typename T>
std::size_t Family<T>::hash_labels(
const std::map<std::string, std::string>& labels) {
auto combined = std::accumulate(
labels.begin(), labels.end(), std::string{},
[](const std::string& acc,
const std::pair<std::string, std::string>& label_pair) {
return acc + label_pair.first + label_pair.second;
});
return std::hash<std::string>{}(combined);
}

template <typename T>
void Family<T>::Remove(T* metric) {
std::lock_guard<std::mutex> lock{mutex_};
Expand Down
19 changes: 19 additions & 0 deletions core/src/detail/utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "prometheus/detail/utils.h"
#include "prometheus/detail/hash.h"

namespace prometheus {

namespace utils {

std::size_t hash_labels(const std::map<std::string, std::string>& labels) {
size_t seed = 0;
for (auto& label : labels) {
hash_combine(&seed, label.first, label.second);
}

return seed;
}

} // utils

} // prometheus
1 change: 1 addition & 0 deletions core/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_executable(prometheus_test
histogram_test.cc
registry_test.cc
summary_test.cc
utils_test.cc
)

target_link_libraries(prometheus_test
Expand Down
43 changes: 43 additions & 0 deletions core/tests/utils_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "prometheus/detail/utils.h"

#include <map>
#include <gmock/gmock.h>

namespace prometheus {

namespace {

void expect_not_equal(const std::map<std::string, std::string>& label1,
const std::map<std::string, std::string>& label2) {
EXPECT_TRUE(utils::hash_labels(label1) != utils::hash_labels(label2));
}

TEST(UtilsTest, hash_labels_1) {
std::map<std::string, std::string> labels;
labels.insert(std::make_pair<std::string, std::string>("key1", "value1"));
labels.insert(std::make_pair<std::string, std::string>("key2", "vaule2"));
auto value1 = utils::hash_labels(labels);
auto value2 = utils::hash_labels(labels);

EXPECT_EQ(value1, value2);
}


TEST(UtilsTest, hash_labels_2) {
std::map<std::string, std::string> labels1;
labels1.insert(std::make_pair<std::string, std::string>("aa", "bb"));
std::map<std::string, std::string> labels2;
labels2.insert(std::make_pair<std::string, std::string>("a", "abb"));
expect_not_equal(labels1, labels2);

std::map<std::string, std::string> labels3;
labels3.insert(std::make_pair<std::string, std::string>("a", "a"));
std::map<std::string, std::string> labels4;
labels4.insert(std::make_pair<std::string, std::string>("aa", ""));
expect_not_equal(labels3, labels4);
}


}

} //prometheus

0 comments on commit 40e0448

Please sign in to comment.