Skip to content
Closed
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
14 changes: 14 additions & 0 deletions source/extensions/stat_sinks/common/statsd/statsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ void UdpStatsdSink::flush(Stats::Source& source) {
buildTagStr(gauge->tags())));
}
}

for (const Stats::BoolIndicatorSharedPtr& boolIndicator : source.cachedBoolIndicators()) {
if (boolIndicator->used()) {
writer.write(fmt::format("{}.{}:{}|g{}", prefix_, getName(*boolIndicator), boolIndicator->value(),
buildTagStr(boolIndicator->tags())));
}
}
}

void UdpStatsdSink::onHistogramComplete(const Stats::Histogram& histogram, uint64_t value) {
Expand Down Expand Up @@ -124,6 +131,13 @@ void TcpStatsdSink::flush(Stats::Source& source) {
tls_sink.flushGauge(gauge->name(), gauge->value());
}
}

for (const Stats::BoolIndicatorSharedPtr& boolIndicator : source.cachedBoolIndicators()) {
if (boolIndicator->used()) {
tls_sink.flushGauge(boolIndicator->name(), static_cast<uint64_t>(boolIndicator->value()));
}
}

tls_sink.endFlush(true);
}

Expand Down
27 changes: 25 additions & 2 deletions source/server/http/admin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,12 @@ Http::Code AdminImpl::handlerStats(absl::string_view url, Http::HeaderMap& respo
}
}

for (const Stats::BoolIndicatorSharedPtr& boolIndicator : server_.stats().boolIndicators()) {
if (shouldShowMetric(boolIndicator, used_only, regex)) {
all_stats.emplace(boolIndicator->name(), boolIndicator->value());
}
}

if (has_format) {
const std::string format_value = params.at("format");
if (format_value == "json") {
Expand Down Expand Up @@ -725,7 +731,7 @@ Http::Code AdminImpl::handlerPrometheusStats(absl::string_view path_and_query, H
const Http::Utility::QueryParams params = Http::Utility::parseQueryString(path_and_query);
const bool used_only = params.find("usedonly") != params.end();
PrometheusStatsFormatter::statsAsPrometheus(server_.stats().counters(), server_.stats().gauges(),
server_.stats().histograms(), response, used_only);
server_.stats().histograms(), server_.stats().boolIndicators(), response, used_only);
return Http::Code::OK;
}

Expand Down Expand Up @@ -758,7 +764,9 @@ std::string PrometheusStatsFormatter::metricName(const std::string& extractedNam
uint64_t PrometheusStatsFormatter::statsAsPrometheus(
const std::vector<Stats::CounterSharedPtr>& counters,
const std::vector<Stats::GaugeSharedPtr>& gauges,
const std::vector<Stats::ParentHistogramSharedPtr>& histograms, Buffer::Instance& response,
const std::vector<Stats::ParentHistogramSharedPtr>& histograms,
const std::vector<Stats::BoolIndicatorSharedPtr>& boolIndicators,
Buffer::Instance& response,
const bool used_only) {
std::unordered_set<std::string> metric_type_tracker;
for (const auto& counter : counters) {
Expand Down Expand Up @@ -789,6 +797,21 @@ uint64_t PrometheusStatsFormatter::statsAsPrometheus(
response.add(fmt::format("{0}{{{1}}} {2}\n", metric_name, tags, gauge->value()));
}

for (const auto& boolIndicator : boolIndicators) {
if (!shouldShowMetric(boolIndicator, used_only)) {
continue;
}

const std::string tags = formattedTags(boolIndicator->tags());
const std::string metric_name = metricName(boolIndicator->tagExtractedName());
if (metric_type_tracker.find(metric_name) == metric_type_tracker.end()) {
metric_type_tracker.insert(metric_name);
response.add(fmt::format("# TYPE {0} gauge\n", metric_name));
}

response.add(fmt::format("{0}{{{1}}} {2}\n", metric_name, tags, static_cast<uint8_t>(boolIndicator->value())));
}

for (const auto& histogram : histograms) {
if (!shouldShowMetric(histogram, used_only)) {
continue;
Expand Down
1 change: 1 addition & 0 deletions source/server/http/admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class PrometheusStatsFormatter {
static uint64_t statsAsPrometheus(const std::vector<Stats::CounterSharedPtr>& counters,
const std::vector<Stats::GaugeSharedPtr>& gauges,
const std::vector<Stats::ParentHistogramSharedPtr>& histograms,
const std::vector<Stats::BoolIndicatorSharedPtr>& boolIndicators,
Buffer::Instance& response, const bool used_only);
/**
* Format the given tags, returning a string as a comma-separated list
Expand Down
20 changes: 13 additions & 7 deletions test/server/http/admin_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1274,11 +1274,17 @@ class PrometheusStatsFormatterTest : public testing::Test {
histograms_.push_back(histogram);
}

void addBoolIndicator(const std::string& name, std::vector<Stats::Tag> cluster_tags) {
std::string tname = std::string(name);
boolIndicators_.push_back(alloc_.makeBoolIndicator(name, std::move(tname), std::move(cluster_tags)));
}

Stats::StatsOptionsImpl stats_options_;
Stats::HeapStatDataAllocator alloc_;
std::vector<Stats::CounterSharedPtr> counters_;
std::vector<Stats::GaugeSharedPtr> gauges_;
std::vector<Stats::ParentHistogramSharedPtr> histograms_;
std::vector<Stats::BoolIndicatorSharedPtr> boolIndicators_;
};

TEST_F(PrometheusStatsFormatterTest, MetricName) {
Expand Down Expand Up @@ -1329,7 +1335,7 @@ TEST_F(PrometheusStatsFormatterTest, MetricNameCollison) {

Buffer::OwnedImpl response;
auto size =
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, response, false);
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, boolIndicators_, response, false);
EXPECT_EQ(2UL, size);
}

Expand All @@ -1349,7 +1355,7 @@ TEST_F(PrometheusStatsFormatterTest, UniqueMetricName) {

Buffer::OwnedImpl response;
auto size =
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, response, false);
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, boolIndicators_, response, false);
EXPECT_EQ(4UL, size);
}

Expand All @@ -1368,7 +1374,7 @@ TEST_F(PrometheusStatsFormatterTest, HistogramWithNoValuesAndNoTags) {

Buffer::OwnedImpl response;
auto size =
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, response, false);
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, boolIndicators_,response, false);
EXPECT_EQ(1UL, size);

const std::string expected_output = R"EOF(# TYPE envoy_histogram1 histogram
Expand Down Expand Up @@ -1420,7 +1426,7 @@ TEST_F(PrometheusStatsFormatterTest, OutputWithAllMetricTypes) {

Buffer::OwnedImpl response;
auto size =
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, response, false);
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, boolIndicators_, response, false);
EXPECT_EQ(5UL, size);

const std::string expected_output = R"EOF(# TYPE envoy_cluster_test_1_upstream_cx_total counter
Expand Down Expand Up @@ -1480,7 +1486,7 @@ TEST_F(PrometheusStatsFormatterTest, OutputWithUsedOnly) {

Buffer::OwnedImpl response;
auto size =
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, response, true);
PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_, boolIndicators_, response, true);
EXPECT_EQ(1UL, size);

const std::string expected_output = R"EOF(# TYPE envoy_cluster_test_1_upstream_rq_time histogram
Expand Down Expand Up @@ -1529,7 +1535,7 @@ TEST_F(PrometheusStatsFormatterTest, OutputWithUsedOnlyHistogram) {

Buffer::OwnedImpl response;
auto size = PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_,
response, used_only);
boolIndicators_, response, used_only);
EXPECT_EQ(0UL, size);
}

Expand All @@ -1540,7 +1546,7 @@ TEST_F(PrometheusStatsFormatterTest, OutputWithUsedOnlyHistogram) {

Buffer::OwnedImpl response;
auto size = PrometheusStatsFormatter::statsAsPrometheus(counters_, gauges_, histograms_,
response, used_only);
boolIndicators_, response, used_only);
EXPECT_EQ(1UL, size);
}
}
Expand Down