diff --git a/include/seastar/core/prometheus.hh b/include/seastar/core/prometheus.hh index 2505828051..8ae5a3f1ad 100644 --- a/include/seastar/core/prometheus.hh +++ b/include/seastar/core/prometheus.hh @@ -54,6 +54,16 @@ future<> start(httpd::http_server_control& http_server, config ctx); future<> add_prometheus_routes(distributed& server, config ctx); future<> add_prometheus_routes(httpd::http_server& server, config ctx); /// @} + +namespace details { +using filter_t = std::function; + +class test_access { + future<> write_body(config cfg, sstring metric_family_name, bool prefix, bool show_help, bool enable_aggregation, filter_t filter, output_stream&& s); + + friend struct metrics_perf_fixture; +}; +} SEASTAR_MODULE_EXPORT_END } } diff --git a/src/core/prometheus.cc b/src/core/prometheus.cc index 23c6ff557d..6be1e86be0 100644 --- a/src/core/prometheus.cc +++ b/src/core/prometheus.cc @@ -924,24 +924,33 @@ class metrics_handler : public httpd::handler_base { bool show_help = req->get_query_param("__help__") != "false"; bool enable_aggregation = req->get_query_param("__aggregate__") != "false"; std::function filter = make_filter(*req); - rep->write_body(is_protobuf_format ? "proto" : "txt", [this, is_protobuf_format, metric_family_name, prefix, show_help, enable_aggregation, filter] (output_stream&& s) { - return do_with(metrics_families_per_shard(), output_stream(std::move(s)), - [this, is_protobuf_format, prefix, &metric_family_name, show_help, enable_aggregation, filter] (metrics_families_per_shard& families, output_stream& s) mutable { - return get_map_value(families).then([&s, &families, this, is_protobuf_format, prefix, &metric_family_name, show_help, enable_aggregation, filter]() mutable { - return do_with(get_range(families, metric_family_name, prefix), - [&s, this, is_protobuf_format, show_help, enable_aggregation, filter](metric_family_range& m) { - return (is_protobuf_format) ? write_protobuf_representation(s, _ctx, m, enable_aggregation, filter) : - write_text_representation(s, _ctx, m, show_help, enable_aggregation, filter); - }); - }).finally([&s] () mutable { - return s.close(); - }); - }); + rep->write_body(is_protobuf_format ? "proto" : "txt", [this, is_protobuf_format, metric_family_name, prefix, show_help, enable_aggregation, filter](output_stream&& s) { + return write_body(is_protobuf_format, metric_family_name, prefix, show_help, enable_aggregation, filter, std::move(s)); }); return make_ready_future>(std::move(rep)); } + +private: + future<> write_body(bool is_protobuf_format, sstring metric_family_name, bool prefix, bool show_help, bool enable_aggregation, details::filter_t filter, output_stream&& out_stream) { + auto s = std::move(out_stream); + auto families = metrics_families_per_shard{}; + co_await get_map_value(families); + + auto m = get_range(families, metric_family_name, prefix); + + co_return co_await ((is_protobuf_format) ? write_protobuf_representation(s, _ctx, m, enable_aggregation, filter) : write_text_representation(s, _ctx, m, show_help, enable_aggregation, filter)).finally([&s] { return s.close(); }); + + } + + friend details::test_access; }; +future<> details::test_access::write_body(config cfg, sstring metric_family_name, bool prefix, bool show_help, bool enable_aggregation, filter_t filter, output_stream&& s) { + metrics_handler handler(std::move(cfg)); + + co_return co_await handler.write_body(cfg.allow_protobuf, metric_family_name, prefix, show_help, enable_aggregation, filter, std::move(s)); +} + std::function metrics_handler::_true_function = [](const mi::labels_type&) { return true; };