Skip to content

Commit

Permalink
Merge c87cc7d 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 + c87cc7d commit 41af5fe
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 15 deletions.
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 {

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>
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>
void hash_combine(std::size_t* seed, const T& value, const Types&... args) {
hash_combine(seed, value);
hash_combine(seed, args...);
}

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

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

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
16 changes: 1 addition & 15 deletions core/include/prometheus/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,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 +153,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 +175,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
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/utils/util.h"

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

namespace promethues {

namespace {

void expect_not_equal(const std::map<std::string, std::string>& label1,
const std::map<std::string, std::string>& label2) {
EXPECT_NOT_EQ(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 41af5fe

Please sign in to comment.