Skip to content

Commit

Permalink
Add label decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
jupp0r committed Sep 27, 2016
1 parent 13dcc98 commit 817f795
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 34 deletions.
90 changes: 60 additions & 30 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,64 @@ git_repository(
tag = "v3.0.0",
)

new_git_repository(
name = "civetweb",
remote = "https://github.com/civetweb/civetweb.git",
tag = "v1.8",
build_file_content = """
# new_git_repository(
# name = "civetweb",
# remote = "https://github.com/civetweb/civetweb.git",
# tag = "v1.8",
# build_file_content = """
# cc_library(
# name = "civetweb",
# srcs = [
# "src/civetweb.c",
# "src/CivetServer.cpp",
# ],
# hdrs = [
# "include/civetweb.h",
# "include/CivetServer.h",
# "src/md5.inl",
# "src/handle_form.inl",
# ],
# includes = [
# "include",
# ],
# copts = [
# "-DNDEBUG",
# "-DNO_CGI",
# "-DNO_CACHING",
# "-DNO_SSL",
# "-DNO_FILES",
# ],
# visibility = ["//visibility:public"],
# )
# """
# )
new_local_repository(
name = "civetweb",
path = "../civetweb",
build_file_content = """
cc_library(
name = "civetweb",
srcs = [
"src/civetweb.c",
"src/CivetServer.cpp",
],
hdrs = [
"include/civetweb.h",
"include/CivetServer.h",
"src/md5.inl",
"src/handle_form.inl",
],
includes = [
"include",
],
copts = [
"-DNDEBUG",
"-DNO_CGI",
"-DNO_CACHING",
"-DNO_SSL",
"-DNO_FILES",
],
visibility = ["//visibility:public"],
)
"""
)
name = "civetweb",
srcs = [
"src/civetweb.c",
"src/CivetServer.cpp",
],
hdrs = [
"include/civetweb.h",
"include/CivetServer.h",
"src/md5.inl",
"src/handle_form.inl",
],
includes = [
"include",
],
copts = [
"-DNDEBUG",
"-DNO_CGI",
"-DNO_CACHING",
"-DNO_SSL",
"-DNO_FILES",
],
visibility = ["//visibility:public"],
)
"""
)
7 changes: 5 additions & 2 deletions lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ cc_library(
name = "prometheus-cpp",
srcs = ["counter.cc",
"gauge.cc",
"exposer.cc"],
"exposer.cc",
"label_decorator.cc"],
hdrs = ["counter.h",
"gauge.h",
"exposer.h"],
"exposer.h",
"label_decorator.h",
"metric.h"],
visibility = ["//visibility:public"],
deps = ["@protobuf//:protobuf",
"@prometheus_client_model//:prometheus_client_model",
Expand Down
8 changes: 8 additions & 0 deletions lib/counter.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "counter.h"
#include "cpp/metrics.pb.h"

namespace prometheus {

Expand All @@ -7,4 +8,11 @@ void Counter::inc() { gauge_.inc(); }
void Counter::inc(double val) { gauge_.inc(val); }

double Counter::value() const { return gauge_.value(); }

io::prometheus::client::Metric Counter::collect() {
io::prometheus::client::Metric metric;
auto counter = metric.mutable_counter();
counter->set_value(value());
return metric;
}
}
5 changes: 4 additions & 1 deletion lib/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
#include <atomic>

#include "gauge.h"
#include "metric.h"

namespace prometheus {
class Counter {
class Counter : Metric {
public:
void inc();
void inc(double);
double value() const;

io::prometheus::client::Metric collect();

private:
Gauge gauge_;
};
Expand Down
22 changes: 22 additions & 0 deletions lib/label_decorator.cc
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;
}
}
31 changes: 31 additions & 0 deletions lib/label_decorator.h
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_;
};
}
18 changes: 18 additions & 0 deletions lib/metric.h
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;
};
}
2 changes: 1 addition & 1 deletion tests/BUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cc_test(
name = "prometheus_test",
srcs = ["counter_test.cc", "gauge_test.cc"],
srcs = ["counter_test.cc", "gauge_test.cc", "label_decorator_test.cc", "mock_metric.h"],
copts = ["-Iexternal/googletest/include"],
deps = ["@googletest//:main",
"//lib:prometheus-cpp"],
Expand Down
44 changes: 44 additions & 0 deletions tests/label_decorator_test.cc
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());
}
16 changes: 16 additions & 0 deletions tests/mock_metric.h
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());
};

0 comments on commit 817f795

Please sign in to comment.