Skip to content

Commit

Permalink
prometheus.cc: Aggregate before converting to text
Browse files Browse the repository at this point in the history
This patch uses the aggregation utilities introduced in the previous
patches in order to perform aggregation of counter and histogram
metrics. This step takes place before converting to text for reporting.
  • Loading branch information
Vlad Lazar committed Jun 23, 2022
1 parent b673414 commit 1729479
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions src/core/prometheus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Copyright (C) 2016 ScyllaDB
*/

#include <seastar/core/metrics.hh>
#include <seastar/core/prometheus.hh>
#include <sstream>

Expand Down Expand Up @@ -592,12 +593,10 @@ future<> write_text_representation(output_stream<char>& out, const config& ctx,
auto name = ctx.prefix + "_" + metric_family.name();
found = false;
histogram_aggregator histograms(metric_family.metadata().aggregate_labels);
counter_aggregator counters(metric_family.metadata().aggregate_labels);
bool should_aggregate = !metric_family.metadata().aggregate_labels.empty();
metric_family.foreach_metric([&out, &ctx, &found, &name, &metric_family, &histograms, should_aggregate](auto value, auto value_info) mutable {
metric_family.foreach_metric([&out, &ctx, &found, &name, &metric_family, &histograms, &counters, should_aggregate](auto value, auto value_info) mutable {
std::stringstream s;
if (value.is_empty()) {
return;
}
if (!found) {
if (metric_family.metadata().d.str() != "") {
s << "# HELP " << name << " " << metric_family.metadata().d.str() << "\n";
Expand All @@ -614,25 +613,36 @@ future<> write_text_representation(output_stream<char>& out, const config& ctx,
write_histogram(s, ctx, name, value.get_histogram(), value_info.id.labels());
}
} else {
write_counter(s, ctx, name, value, value_info.id.labels());
if (should_aggregate) {
counters.add_counter(value, value_info.id.labels());
} else {
write_counter(s, ctx, name, value, value_info.id.labels());
}
}
out.write(s.str()).get();
thread::maybe_yield();
});
if (!histograms.empty()) {
auto name = ctx.prefix + "_" + metric_family.name();
std::stringstream name_help;
if (metric_family.metadata().d.str() != "") {
name_help << "# HELP " << name << " " << metric_family.metadata().d.str() << "\n";
}
name_help << "# TYPE " << name << " histogram" << "\n";
out.write(name_help.str()).get();
for (auto&& h : histograms.get_histograms()) {
std::stringstream s;
write_histogram(s, ctx, name, h.second, h.first);
out.write(s.str()).get();
thread::maybe_yield();
}

std::stringstream aggregated_metrics_stream;

for (auto&& h : histograms.get_histograms()) {
write_histogram(aggregated_metrics_stream, ctx, name, h.second, h.first);
out.write(aggregated_metrics_stream.str()).get();

aggregated_metrics_stream.str({});
aggregated_metrics_stream.clear();

thread::maybe_yield();
}

for (auto&& h : counters.get_counters()) {
write_counter(aggregated_metrics_stream, ctx, name, h.second, h.first);
out.write(aggregated_metrics_stream.str()).get();

aggregated_metrics_stream.str({});
aggregated_metrics_stream.clear();

thread::maybe_yield();
}
}
});
Expand Down

0 comments on commit 1729479

Please sign in to comment.