Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug and json http responses #7

Merged
merged 2 commits into from
Oct 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 102 additions & 25 deletions lib/exposer.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include <chrono>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>

#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/message_differencer.h>

#include "exposer.h"

Expand All @@ -24,43 +27,99 @@ MetricsHandler::MetricsHandler(
{{"component", "exposer"}})),
numScrapes_(numScrapesFamily_->add({})) {}

bool MetricsHandler::handleGet(CivetServer* server,
struct mg_connection* conn) {
using namespace io::prometheus::client;

static std::string serializeToDelimitedProtobuf(
const std::vector<io::prometheus::client::MetricFamily>& metrics) {
std::ostringstream ss;
for (auto&& wcollectable : collectables_) {
auto collectable = wcollectable.lock();
if (!collectable) {
continue;
for (auto&& metric : metrics) {
{
google::protobuf::io::OstreamOutputStream rawOutput{&ss};
google::protobuf::io::CodedOutputStream output(&rawOutput);

const int size = metric.ByteSize();
output.WriteVarint32(size);
}

for (auto&& metricFamily : collectable->collect()) {
{
google::protobuf::io::OstreamOutputStream rawOutput{&ss};
google::protobuf::io::CodedOutputStream output(&rawOutput);
auto buffer = std::string{};
metric.SerializeToString(&buffer);
ss << buffer;
}
return ss.str();
}

const int size = metricFamily.ByteSize();
output.WriteVarint32(size);
}
static std::string serializeToJson(
const std::vector<io::prometheus::client::MetricFamily>& metrics) {
using google::protobuf::util::MessageDifferencer;

auto buffer = std::string{};
metricFamily.SerializeToString(&buffer);
ss << buffer;
std::stringstream ss;
ss << "[";

for (auto&& metric : metrics) {
std::string result;
google::protobuf::util::MessageToJsonString(
metric, &result, google::protobuf::util::JsonPrintOptions());
ss << result;
if (!MessageDifferencer::Equals(metric, metrics.back())) {
ss << ",";
}
}
ss << "]";
return ss.str();
}

static std::string serializeToHumanReadable(
const std::vector<io::prometheus::client::MetricFamily>& metrics) {
auto result = std::string{};
for (auto&& metric : metrics) {
result += metric.DebugString() + "\n";
}
return result;
}

static std::string getAcceptedEncoding(struct mg_connection* conn) {
auto request_info = mg_get_request_info(conn);
for (int i = 0; i < request_info->num_headers; i++) {
auto header = request_info->http_headers[i];
if (std::string{header.name} == "Accept") {
return {header.value};
}
}
return "";
}

bool MetricsHandler::handleGet(CivetServer* server,
struct mg_connection* conn) {
using namespace io::prometheus::client;

auto acceptedEncoding = getAcceptedEncoding(conn);
auto metrics = collectMetrics();

auto body = std::string{};
auto contentType = std::string{};

if (acceptedEncoding.find("application/vnd.google.protobuf") !=
std::string::npos) {
body = serializeToDelimitedProtobuf(metrics);
contentType =
"application/vnd.google.protobuf; "
"proto=io.prometheus.client.MetricFamily; "
"encoding=delimited";
} else if (acceptedEncoding.find("application/json") != std::string::npos) {
body = serializeToJson(metrics);
contentType = "application/json";
} else {
body = serializeToHumanReadable(metrics);
contentType = "text/plain";
}

auto body = ss.str();
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: "
"application/vnd.google.protobuf; "
"proto=io.prometheus.client.MetricFamily; "
"encoding=delimited\r\n"
"Content-Length: ");
mg_printf(conn, "%lu\r\n\r\n", body.size());
"Content-Type: %s\r\n",
contentType.c_str());
mg_printf(conn, "Content-Length: %lu\r\n\r\n", body.size());
mg_write(conn, body.data(), body.size());

bytesTransfered_->inc(body.size());

numScrapes_->inc();
return true;
}
Expand All @@ -78,4 +137,22 @@ void Exposer::registerCollectable(
const std::weak_ptr<Collectable>& collectable) {
collectables_.push_back(collectable);
}

std::vector<io::prometheus::client::MetricFamily>
MetricsHandler::collectMetrics() const {
auto collectedMetrics = std::vector<io::prometheus::client::MetricFamily>{};

for (auto&& wcollectable : collectables_) {
auto collectable = wcollectable.lock();
if (!collectable) {
continue;
}

for (auto metric : collectable->collect()) {
collectedMetrics.push_back(metric);
}
}

return collectedMetrics;
}
}
3 changes: 3 additions & 0 deletions lib/exposer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class MetricsHandler : public CivetHandler {

bool handleGet(CivetServer* server, struct mg_connection* conn);

private:
std::vector<io::prometheus::client::MetricFamily> collectMetrics() const;

const std::vector<std::weak_ptr<Collectable>>& collectables_;
Family<Counter>* bytesTransferedFamily_;
Counter* bytesTransfered_;
Expand Down