Skip to content

Commit

Permalink
core: reject duplicate label names (#463)
Browse files Browse the repository at this point in the history
Issue: #459
  • Loading branch information
gjasny committed Mar 8, 2021
1 parent d095854 commit 77b06a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ T& Family<T>::Add(const std::map<std::string, std::string>& labels,
if (!CheckLabelName(label_name)) {
throw std::invalid_argument("Invalid label name");
}
if (constant_labels_.count(label_name)) {
throw std::invalid_argument("Duplicate label name");
}
}

auto metric = metrics_.insert(std::make_pair(hash, std::move(object)));
Expand Down
7 changes: 7 additions & 0 deletions core/tests/family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ TEST(FamilyTest, labels) {
::testing::ElementsAre(const_label, dynamic_label));
}

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

Family<Counter> family{"total_requests", "Counts all requests", labels};
EXPECT_ANY_THROW(family.Add(labels));
}

TEST(FamilyTest, counter_value) {
Family<Counter> family{"total_requests", "Counts all requests", {}};
auto& counter = family.Add({});
Expand Down

0 comments on commit 77b06a8

Please sign in to comment.