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

fix the metrics exporter #282

Merged
merged 1 commit into from
Nov 3, 2023
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
80 changes: 51 additions & 29 deletions src/exporter_handler.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
/*
Copyright The Overlaybd Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "metrics.h"
#include <photon/common/conststr.h>
#include <photon/common/estring.h>
#include <photon/common/metric-meter/metrics.h>
#include <photon/net/http/server.h>
#include <photon/net/socket.h>
#include "textexporter.h"
#include <zlib.h>

#include "textexporter.h"

namespace ExposeMetrics {

Expand All @@ -23,51 +38,58 @@ namespace ExposeMetrics {
ret.append(name.help_str()).append("\n"); \
ret.append(name.type_str()).append("\n"); \
for (auto x : va_##name) { \
ret.append( \
name.render(x.second->val(), nodename.c_str(), x.first)) \
.append("\n"); \
ret.append(name.render(x.second->val(), x.first)).append("\n"); \
} \
ret.append("\n"); \
}


struct ExposeRender : public photon::net::http::HTTPHandler {
EXPOSE_PHOTON_METRICLIST(latency, Metric::AverageLatencyCounter);

std::string nodename;
photon::net::http::DelegateHTTPHandler handler;
EXPOSE_PHOTON_METRICLIST(throughput, Metric::QPSCounter);
EXPOSE_PHOTON_METRICLIST(qps, Metric::QPSCounter);
EXPOSE_PHOTON_METRICLIST(latency, Metric::MaxLatencyCounter);
EXPOSE_PHOTON_METRICLIST(count, Metric::AddCounter);
EXPOSE_PHOTON_METRICLIST(cache, Metric::ValueCounter);

template <typename... Args>
ExposeRender(Args&&... args)
: nodename(std::forward<Args>(args)...),
handler{this, &ExposeRender::handle_request} {}
ExposeRender(Args&&... args) {}

std::string render() {
EXPOSE_TEMPLATE(alive, is_alive : gauge{node});
EXPOSE_TEMPLATE(latency, blob_read_average_latency : gauge{node, type} #us);
EXPOSE_TEMPLATE(alive, OverlayBD_Alive : gauge{node});
EXPOSE_TEMPLATE(throughput, OverlayBD_Read_Throughtput
: gauge{node, type, mode} #Bytes / sec);
EXPOSE_TEMPLATE(qps, OverlayBD_QPS : gauge{node, type, mode});
EXPOSE_TEMPLATE(latency, OverlayBD_MaxLatency
: gauge{node, type, mode} #us);
EXPOSE_TEMPLATE(count, OverlayBD_Count : gauge{node, type} #Bytes);
std::string ret(alive.help_str());
ret.append("\n")
.append(alive.type_str())
.append("\n")
.append(alive.render(1, nodename.c_str()))
.append(alive.render(1))
.append("\n\n");
LOOP_APPEND_METRIC(ret, throughput);
LOOP_APPEND_METRIC(ret, qps);
LOOP_APPEND_METRIC(ret, latency);
LOOP_APPEND_METRIC(ret, count);
return ret;
}

int handle_request(photon::net::http::Request& req, photon::net::http::Response& resp, std::string_view) override {
std::string str = render();
auto cl = str.size();
if (cl > 4096) {
LOG_ERROR_RETURN(0, -1, "RetType failed test");
}
int handle_request(photon::net::http::Request& req,
photon::net::http::Response& resp,
std::string_view) override {
auto body = render();
resp.set_result(200);
resp.headers.content_length(cl);
resp.write((void*)str.data(), str.size());
return 0;
resp.keep_alive(true);
resp.headers.insert("Content-Type", "text/plain; version=0.0.4");
resp.headers.content_length(body.length());
ssize_t len = 0;
len = resp.write((void*)body.data(), body.length());
if (len == (ssize_t)body.length()) {
return 0;
} else {
LOG_ERRNO_RETURN(0, -1, "Failed to write exporter response");
}
}

photon::net::http::DelegateHTTPHandler get_handler() { return handler; }
};

#undef LOOP_APPEND_METRIC
Expand Down
100 changes: 47 additions & 53 deletions src/exporter_server.h
Original file line number Diff line number Diff line change
@@ -1,81 +1,75 @@
/*
Copyright The Overlaybd Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#pragma once

#include "config.h"
#include "exporter_handler.h"
#include "metrics_fs.h"
#include <photon/common/metric-meter/metrics.h>
#include <photon/net/http/server.h>
#include <photon/net/socket.h>
#include <photon/photon.h>
#include <photon/thread/thread.h>
#include <photon/thread/timer.h>
#include <photon/thread/workerpool.h>
#include <pthread.h>
#include <sys/prctl.h>

#include <thread>


#include "config.h"
#include "exporter_handler.h"
#include "metrics_fs.h"

class OverlayBDMetric {
public:
MetricMeta download;
ExposeMetrics::ExposeRender exporter;
MetricMeta pread, download;

photon::Timer timer;
uint64_t m_timeout = -1;
ExposeMetrics::ExposeRender exporter;

OverlayBDMetric() : timer(-1, {this, &OverlayBDMetric::on_timer}) {
OverlayBDMetric() {
exporter.add_throughput("pread", pread.throughput);
exporter.add_latency("pread", pread.latency);
exporter.add_qps("pread", pread.qps);
exporter.add_count("pread", pread.total);
exporter.add_throughput("download", download.throughput);
exporter.add_latency("download", download.latency);
exporter.add_qps("download", download.qps);
exporter.add_count("download", download.total);
}

~OverlayBDMetric() {
timer.cancel();
timer.stop();
}

uint64_t on_timer() {
return m_timeout;
}

uint64_t interval(uint64_t x) { return m_timeout = x; }
uint64_t interval() { return m_timeout; }
int start() { return timer.reset(0); }
};

struct ExporterServer {
photon::WorkPool wp;

photon::net::http::HTTPServer *httpserver = nullptr;
photon::net::ISocketServer *tcpserver = nullptr;

bool ready = false;

ExporterServer(ImageConfigNS::GlobalConfig &config,
OverlayBDMetric *metrics)
: wp(1, photon::INIT_EVENT_EPOLL, 0, 0) {
wp.call([&] {
prctl(PR_SET_THP_DISABLE, 1);
pthread_setname_np(pthread_self(), "overlaybd-exporter");
char buffer[64];
snprintf(buffer, 63, "localhost:%d", config.exporterConfig().port());
metrics->exporter.nodename = buffer;

tcpserver = photon::net::new_tcp_socket_server();
tcpserver->bind(config.exporterConfig().port(), photon::net::IPAddr("127.0.0.1"));

tcpserver->listen();
httpserver = photon::net::http::new_http_server();
httpserver->add_handler(&metrics->exporter);
tcpserver->set_handler(httpserver->get_connection_handler());
tcpserver->start_loop();
ready = true;
});
OverlayBDMetric *metrics) {
tcpserver = photon::net::new_tcp_socket_server();
tcpserver->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1);
if (tcpserver->bind(config.exporterConfig().port()) < 0)
LOG_ERRNO_RETURN(0, , "Failed to bind exporter port `",
config.exporterConfig().port());
if (tcpserver->listen() < 0)
LOG_ERRNO_RETURN(0, , "Failed to listen exporter port `",
config.exporterConfig().port());
httpserver = photon::net::http::new_http_server();
httpserver->add_handler(&metrics->exporter, false,
config.exporterConfig().uriPrefix());
tcpserver->set_handler(httpserver->get_connection_handler());
tcpserver->start_loop();
ready = true;
}

~ExporterServer() {
wp.call([&] {
delete tcpserver;
delete httpserver;
});
delete tcpserver;
delete httpserver;
}
};
6 changes: 4 additions & 2 deletions src/image_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,6 @@ int ImageService::init() {
}
if (global_conf.exporterConfig().enable()) {
metrics.reset(new OverlayBDMetric());
metrics->interval(global_conf.exporterConfig().updateInterval());
metrics->start();
global_fs.srcfs = new MetricFS(global_fs.underlay_registryfs, &metrics->download);
exporter = new ExporterServer(global_conf, metrics.get());
if (!exporter->ready)
Expand Down Expand Up @@ -410,6 +408,10 @@ int ImageService::init() {
LOG_ERRNO_RETURN(0, -1, "failed to create cached_fs");
}

if (global_conf.exporterConfig().enable()) {
global_fs.cached_fs = new MetricFS(global_fs.cached_fs, &metrics->pread);
}

if (global_conf.gzipCacheConfig().enable()) {
LOG_INFO("use gzip file cache");
cache_dir = global_conf.gzipCacheConfig().cacheDir();
Expand Down
Loading