-
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
176 additions
and
38 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
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 |
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,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 |
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
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,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; | ||
} |