From 9028f63d3548495039a9274ddbc0bd3890e16e23 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 8 Mar 2019 12:31:08 -0800 Subject: [PATCH] modules: Add preliminary prometheus metrics endpoint. --- modules/Makefile.am | 2 ++ modules/metrics.cc | 75 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 modules/metrics.cc diff --git a/modules/Makefile.am b/modules/Makefile.am index 75ce82598..0c6c295d6 100644 --- a/modules/Makefile.am +++ b/modules/Makefile.am @@ -58,12 +58,14 @@ webroot_la_SOURCES = webroot.cc webhook_la_SOURCES = webhook.cc console_la_SOURCES = console.cc vm_la_SOURCES = vm.cc +metrics_la_SOURCES = metrics.cc module_LTLIBRARIES = \ webroot.la \ webhook.la \ console.la \ vm.la \ + metrics.la \ ### ############################################################################### diff --git a/modules/metrics.cc b/modules/metrics.cc new file mode 100644 index 000000000..a82af8344 --- /dev/null +++ b/modules/metrics.cc @@ -0,0 +1,75 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +using namespace ircd; + +mapi::header +IRCD_MODULE +{ + "Prometheus Metrics" +}; + +resource +metrics_resource +{ + "/metrics", + { + "Prometheus Metrics" + } +}; + +static resource::response +get__metrics(client &, + const resource::request &); + +resource::method +metrics_get +{ + metrics_resource, "GET", get__metrics +}; + +resource::response +get__metrics(client &client, + const resource::request &request) +{ + static const size_t buf_max + { + 4096 + }; + + char buf[buf_max]; + std::stringstream out; + pubsetbuf(out, buf); + + const time_t ts + { + ircd::time() + }; + + out << "aio_requests_total" + << ' ' << fs::aio::stats.requests + << ' ' << ts + << '\n'; + + out << "aio_requests_bytes_total" + << ' ' << fs::aio::stats.bytes_requests + << ' ' << ts + << '\n'; + + const string_view output + { + view(out, buf) + }; + + return resource::response + { + client, output, "text/plain", http::OK + }; +}