-
Notifications
You must be signed in to change notification settings - Fork 11
Add instrumentation and expose measurement in prometheus format #210
base: master
Are you sure you want to change the base?
Changes from 5 commits
8ab1570
2427bce
7b3f07e
955a061
12b4a86
5c065da
c667388
cf5f833
b788df1
e49c64d
ec5a859
bbbd851
acac889
ac7c9ea
5ac289e
d8b3e37
93790ed
786245f
c22faac
cdfe9c1
7514b25
4f37882
96b268a
64929c8
88830d0
6ee8ab8
bee8a32
2687728
5c6526e
546958f
775737c
fc935e6
1fe4649
1099767
1e56d36
d6c6ac3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
init-submodules | ||
build-cpp-netlib | ||
build-yaml-cpp | ||
build-prometheus-cpp | ||
metadatad |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
#include "api_server.h" | ||
|
||
#include <boost/range/irange.hpp> | ||
#include <prometheus/text_serializer.h> | ||
|
||
#include "configuration.h" | ||
#include "health_checker.h" | ||
|
@@ -67,12 +68,16 @@ void MetadataApiServer::Dispatcher::log(const HttpServer::string_type& info) con | |
} | ||
|
||
|
||
MetadataApiServer::MetadataApiServer(const Configuration& config, | ||
const HealthChecker* health_checker, | ||
const MetadataStore& store, | ||
int server_threads, | ||
const std::string& host, int port) | ||
: config_(config), health_checker_(health_checker), store_(store), | ||
MetadataApiServer::MetadataApiServer( | ||
const Configuration& config, | ||
const HealthChecker* health_checker, | ||
const std::shared_ptr<prometheus::Collectable> collectable, | ||
const MetadataStore& store, | ||
int server_threads, | ||
const std::string& host, | ||
int port) | ||
: config_(config), health_checker_(health_checker), | ||
collectable_(collectable), store_(store), | ||
dispatcher_({ | ||
{{"GET", "/monitoredResource/"}, | ||
[=](const HttpServer::request& request, | ||
|
@@ -84,6 +89,11 @@ MetadataApiServer::MetadataApiServer(const Configuration& config, | |
std::shared_ptr<HttpServer::connection> conn) { | ||
HandleHealthz(request, conn); | ||
}}, | ||
{{"GET", "/metrics"}, | ||
[=](const HttpServer::request& request, | ||
std::shared_ptr<HttpServer::connection> conn) { | ||
HandleMetrics(request, conn); | ||
}}, | ||
}, config_.VerboseLogging()), | ||
server_( | ||
HttpServer::options(dispatcher_) | ||
|
@@ -200,4 +210,26 @@ void MetadataApiServer::HandleHealthz( | |
} | ||
} | ||
|
||
void MetadataApiServer::HandleMetrics( | ||
const HttpServer::request& request, | ||
std::shared_ptr<HttpServer::connection> conn) { | ||
std::string response = SerializeMetricsToPrometheusTextFormat(); | ||
conn->set_status(HttpServer::connection::ok); | ||
conn->set_headers(std::map<std::string, std::string>({ | ||
{"Connection", "close"}, | ||
{"Content-Length", std::to_string(response.size())}, | ||
{"Content-Type", "application/json"}, | ||
})); | ||
conn->write(response); | ||
} | ||
|
||
std::string MetadataApiServer::SerializeMetricsToPrometheusTextFormat() const { | ||
|
||
if (!collectable_) { | ||
return ""; | ||
} | ||
|
||
prometheus::TextSerializer text_serializer; | ||
return std::move(text_serializer.Serialize(collectable_->Collect())); | ||
StevenYCChou marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.