Skip to content

Commit

Permalink
Merge 9fc89d4 into 82ecd18
Browse files Browse the repository at this point in the history
  • Loading branch information
gjasny committed May 25, 2020
2 parents 82ecd18 + 9fc89d4 commit d955f15
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 38 deletions.
2 changes: 2 additions & 0 deletions pull/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ if(ENABLE_COMPRESSION)
endif()

add_library(pull
src/endpoint.cc
src/endpoint.h
src/exposer.cc
src/handler.cc
src/handler.h
Expand Down
15 changes: 7 additions & 8 deletions pull/include/prometheus/exposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@ class CivetServer;
namespace prometheus {

namespace detail {
class Endpoint;
class MetricsHandler;
} // namespace detail

class PROMETHEUS_CPP_PULL_EXPORT Exposer {
public:
explicit Exposer(const std::string& bind_address,
const std::string& uri = std::string("/metrics"),
const std::size_t num_threads = 2);
explicit Exposer(std::vector<std::string> options,
const std::string& uri = std::string("/metrics"));
explicit Exposer(std::vector<std::string> options);
~Exposer();
void RegisterCollectable(const std::weak_ptr<Collectable>& collectable);
void RegisterCollectable(const std::weak_ptr<Collectable>& collectable,
const std::string& uri = std::string("/metrics"));

std::vector<int> GetListeningPorts() const;

private:
detail::Endpoint& GetEndpointForUri(const std::string& uri);

std::unique_ptr<CivetServer> server_;
std::vector<std::weak_ptr<Collectable>> collectables_;
std::shared_ptr<Registry> exposer_registry_;
std::unique_ptr<detail::MetricsHandler> metrics_handler_;
std::string uri_;
std::vector<std::unique_ptr<detail::Endpoint>> endpoints_;
};

} // namespace prometheus
29 changes: 29 additions & 0 deletions pull/src/endpoint.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "endpoint.h"

#include "handler.h"
#include "prometheus/detail/future_std.h"

namespace prometheus {
namespace detail {

Endpoint::Endpoint(CivetServer& server, std::string uri)
: server_(server),
uri_(std::move(uri)),
endpoint_registry_(std::make_shared<Registry>()),
metrics_handler_(
make_unique<MetricsHandler>(collectables_, *endpoint_registry_)) {
RegisterCollectable(endpoint_registry_);
server_.addHandler(uri_, metrics_handler_.get());
}

Endpoint::~Endpoint() { server_.removeHandler(uri_); }

void Endpoint::RegisterCollectable(
const std::weak_ptr<Collectable>& collectable) {
collectables_.push_back(collectable);
}

const std::string& Endpoint::getURI() const { return uri_; }

} // namespace detail
} // namespace prometheus
35 changes: 35 additions & 0 deletions pull/src/endpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <memory>
#include <string>
#include <vector>

#include "prometheus/collectable.h"
#include "prometheus/registry.h"

class CivetServer;

namespace prometheus {
namespace detail {
class MetricsHandler;

class Endpoint {
public:
explicit Endpoint(CivetServer& server, std::string uri);
~Endpoint();

void RegisterCollectable(const std::weak_ptr<Collectable>& collectable);

const std::string& getURI() const;

private:
CivetServer& server_;
const std::string uri_;
std::vector<std::weak_ptr<Collectable>> collectables_;
// registry for "meta" metrics about the endpoint itself
std::shared_ptr<Registry> endpoint_registry_;
std::unique_ptr<detail::MetricsHandler> metrics_handler_;
};

} // namespace detail
} // namespace prometheus
51 changes: 28 additions & 23 deletions pull/src/exposer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,45 @@
#include <string>
#include <thread>

#include "prometheus/client_metric.h"
#include "prometheus/detail/future_std.h"

#include "CivetServer.h"
#include "endpoint.h"
#include "handler.h"
#include "prometheus/client_metric.h"
#include "prometheus/detail/future_std.h"

namespace prometheus {

Exposer::Exposer(const std::string& bind_address, const std::string& uri,
const std::size_t num_threads)
: Exposer(
std::vector<std::string>{"listening_ports", bind_address,
"num_threads", std::to_string(num_threads)},
uri) {}

Exposer::Exposer(std::vector<std::string> options, const std::string& uri)
: server_(detail::make_unique<CivetServer>(std::move(options))),
exposer_registry_(std::make_shared<Registry>()),
metrics_handler_(
new detail::MetricsHandler{collectables_, *exposer_registry_}),
uri_(uri) {
RegisterCollectable(exposer_registry_);
server_->addHandler(uri, metrics_handler_.get());
}
Exposer::Exposer(const std::string& bind_address, const std::size_t num_threads)
: Exposer(std::vector<std::string>{"listening_ports", bind_address,
"num_threads",
std::to_string(num_threads)}) {}

Exposer::Exposer(std::vector<std::string> options)
: server_(detail::make_unique<CivetServer>(std::move(options))) {}

Exposer::~Exposer() { server_->removeHandler(uri_); }
Exposer::~Exposer() = default;

void Exposer::RegisterCollectable(
const std::weak_ptr<Collectable>& collectable) {
collectables_.push_back(collectable);
void Exposer::RegisterCollectable(const std::weak_ptr<Collectable>& collectable,
const std::string& uri) {
auto& endpoint = GetEndpointForUri(uri);
endpoint.RegisterCollectable(collectable);
}

std::vector<int> Exposer::GetListeningPorts() const {
return server_->getListeningPorts();
}

detail::Endpoint& Exposer::GetEndpointForUri(const std::string& uri) {
auto sameUri = [uri](const std::unique_ptr<detail::Endpoint>& endpoint) {
return endpoint->getURI() == uri;
};
auto it = std::find_if(std::begin(endpoints_), std::end(endpoints_), sameUri);
if (it != std::end(endpoints_)) {
return *it->get();
}

endpoints_.emplace_back(detail::make_unique<detail::Endpoint>(*server_, uri));
return *endpoints_.back().get();
}

} // namespace prometheus
5 changes: 3 additions & 2 deletions pull/src/handler.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "handler.h"
#include "prometheus/counter.h"
#include "prometheus/summary.h"

#include <cstring>

#include "prometheus/counter.h"
#include "prometheus/summary.h"

#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
Expand Down
6 changes: 6 additions & 0 deletions pull/tests/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ cc_binary(
deps = ["//pull"],
)

cc_binary(
name = "sample-server_multi",
srcs = ["sample_server_multi.cc"],
deps = ["//pull"],
)

sh_test(
name = "scrape-test",
size = "small",
Expand Down
9 changes: 9 additions & 0 deletions pull/tests/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ target_link_libraries(sample_server
PRIVATE
${PROJECT_NAME}::pull
)

add_executable(sample_server_multi
sample_server_multi.cc
)

target_link_libraries(sample_server_multi
PRIVATE
${PROJECT_NAME}::pull
)
10 changes: 5 additions & 5 deletions pull/tests/integration/sample_server.cc
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>

#include <chrono>
#include <map>
#include <memory>
#include <string>
#include <thread>

#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>

int main() {
using namespace prometheus;

// create an http server running on port 8080
Exposer exposer{"127.0.0.1:8080", "/metrics", 1};
Exposer exposer{"127.0.0.1:8080", 1};

// create a metrics registry with component=main labels applied to all its
// metrics
Expand Down
52 changes: 52 additions & 0 deletions pull/tests/integration/sample_server_multi.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/registry.h>

#include <chrono>
#include <memory>
#include <thread>

int main() {
using namespace prometheus;

// create an http server running on port 8080
Exposer exposer{"127.0.0.1:8080", 1};

auto registryA = std::make_shared<Registry>();

// add a new counter family to the registry (families combine values with the
// same name, but distinct label dimensions)
auto& counter_familyA = BuildCounter()
.Name("time_running_seconds_total")
.Help("How many seconds is this server running?")
.Register(*registryA);

// add a counter to the metric family
auto& seconds_counterA = counter_familyA.Add(
{{"another_label", "bar"}, {"yet_another_label", "baz"}});

// ask the exposer to scrape registryA on incoming scrapes for "/metricsA"
exposer.RegisterCollectable(registryA, "/metricsA");

auto registryB = std::make_shared<Registry>();

auto& counter_familyB =
BuildCounter()
.Name("other_time_running_seconds_total")
.Help("How many seconds has something else been running?")
.Register(*registryB);

auto& seconds_counterB = counter_familyB.Add(
{{"another_label", "not_bar"}, {"yet_another_label", "not_baz"}});

// This endpoint exposes registryB.
exposer.RegisterCollectable(registryB, "/metricsB");

for (;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
// increment the counters by one (second)
seconds_counterA.Increment(1.0);
seconds_counterB.Increment(1.5);
}
return 0;
}

0 comments on commit d955f15

Please sign in to comment.