-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
209 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "label_decorator.h" | ||
|
||
#include "cpp/metrics.pb.h" | ||
#include "google/protobuf/repeated_field.h" | ||
|
||
namespace prometheus { | ||
|
||
LabelDecorator::LabelDecorator( | ||
std::vector<std::pair<std::string, std::string>> labels, | ||
std::unique_ptr<Metric> metric) | ||
: labels_(std::move(labels)), metric_(std::move(metric)) {} | ||
|
||
io::prometheus::client::Metric LabelDecorator::collect() { | ||
auto undecoratedMetric = metric_->collect(); | ||
for (auto&& labelPair : labels_) { | ||
auto newLabelPair = undecoratedMetric.add_label(); | ||
newLabelPair->set_name(labelPair.first); | ||
newLabelPair->set_value(labelPair.second); | ||
} | ||
return undecoratedMetric; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "metric.h" | ||
|
||
namespace io { | ||
namespace prometheus { | ||
namespace client { | ||
class Metric; | ||
class LabelPair; | ||
} | ||
} | ||
} | ||
|
||
namespace prometheus { | ||
|
||
class LabelDecorator : Metric { | ||
public: | ||
LabelDecorator(std::vector<std::pair<std::string, std::string>> labels, | ||
std::unique_ptr<Metric> metric); | ||
|
||
io::prometheus::client::Metric collect(); | ||
|
||
private: | ||
const std::vector<std::pair<std::string, std::string>> labels_; | ||
std::unique_ptr<Metric> metric_; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
namespace io { | ||
namespace prometheus { | ||
namespace client { | ||
class Metric; | ||
} | ||
} | ||
} | ||
|
||
namespace prometheus { | ||
|
||
class Metric { | ||
public: | ||
virtual ~Metric() = default; | ||
virtual io::prometheus::client::Metric collect() = 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <memory> | ||
|
||
#include <gmock/gmock.h> | ||
#include "cpp/metrics.pb.h" | ||
#include "lib/label_decorator.h" | ||
#include "mock_metric.h" | ||
|
||
using namespace testing; | ||
using namespace prometheus; | ||
|
||
class LabelDecoratorTest : public Test {}; | ||
|
||
TEST_F(LabelDecoratorTest, initialize_without_labels) { | ||
auto metricPtr = std::unique_ptr<MockMetric>(new NiceMock<MockMetric>()); | ||
auto metric = metricPtr.get(); | ||
ON_CALL(*metric, collect()) | ||
.WillByDefault(Return(io::prometheus::client::Metric{})); | ||
auto labelDecorator = LabelDecorator{{}, std::move(metricPtr)}; | ||
|
||
auto collected = labelDecorator.collect(); | ||
EXPECT_THAT(collected.label_size(), Eq(0)); | ||
} | ||
|
||
TEST_F(LabelDecoratorTest, initialize_with_labels) { | ||
auto metric = std::unique_ptr<MockMetric>(new NiceMock<MockMetric>()); | ||
auto metricWithLabels = io::prometheus::client::Metric{}; | ||
|
||
auto firstLabel = metricWithLabels.add_label(); | ||
firstLabel->set_name("foo"); | ||
firstLabel->set_value("bar"); | ||
auto secondLabel = metricWithLabels.add_label(); | ||
secondLabel->set_name("boo"); | ||
secondLabel->set_value("baz"); | ||
|
||
ON_CALL(*metric, collect()) | ||
.WillByDefault(Return(io::prometheus::client::Metric{})); | ||
auto labelDecorator = | ||
LabelDecorator{{{firstLabel->name(), firstLabel->value()}, | ||
{secondLabel->name(), secondLabel->value()}}, | ||
std::move(metric)}; | ||
|
||
auto collected = labelDecorator.collect(); | ||
EXPECT_EQ(collected.DebugString(), metricWithLabels.DebugString()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include <gmock/gmock.h> | ||
|
||
#include "lib/metric.h" | ||
|
||
namespace io { | ||
namespace prometheus { | ||
namespace client { | ||
class Metric; | ||
} | ||
} | ||
} | ||
|
||
class MockMetric : public prometheus::Metric { | ||
public: | ||
MOCK_METHOD0(collect, io::prometheus::client::Metric()); | ||
}; |